0

Okay so basically I need to create a grade book that allows the user to input their name, and the grades they received (say 5 test scores for example, some set amount). It'll be inputted through a text box and displayed on a list box. (And basically that's all it does.)

What I want to do now is, instead of overwriting the values each time when there's a new student, I want to store them inside an array. Problem is, if I were do something like

Dim studentArray() As Integer

After an event, say a button click, it'll still overwrite it each time, once I click the button, right?

I was wondering if there was anyway around this? My professor suggested that I use the inputted name as the array name, so that each time the button is clicked, the variable name would change so it won't constantly overwrite.

But I don't know how to do this.

Thank you in advance

EDIT: I was thinking about putting everything into a 2D array instead. Where the first column would be the names of the students followed by everything else? Would that be a good idea? (At the moment, we can assume the number of test is no more than 5, so a set amount is okay. Likewise for amount of students.)

12
  • I don't know if i've understood your requirenent but couldn't you use a List(Of Integer) instead and always add the new value to the end? However, i would suggest to use a class instead which holds all relevant informations of a student. You can store them in a List(Of Student). Grades would be a property of a Student that is a List(Of Integer) or Integer(). Commented May 9, 2014 at 23:50
  • I'm sorry, I'm sort of new to VB. List(Of Integer)? And do you mean a Structure? Commented May 9, 2014 at 23:54
  • No. There is almost no reason in .NET that you should ever need to use a struct/Structure. See msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx Commented May 10, 2014 at 0:38
  • Okay, say I made a class, with name, grade, etc. I click a button which makes it declare a new variable of that type, say Student. But won't it still overwrite each time I click that button? Dim name As New Student As long as the variable (name in this case) doesn't change, it's still just overwriting the same data, right? Commented May 10, 2014 at 1:01
  • If I had an event, btnNewStudent_click. And within that private sub, I would have Dim name As New Student If I were to click that button again for another input, wouldn't that overwrite it? (Because the declaration is being called again.) Commented May 10, 2014 at 1:36

1 Answer 1

0

you can use a Dictionary(Of string, integer()). And add the array to the dictionary. For example, to create the dictionary,

Dim dicStudentArrays As New Dictionay(Of String, Integer())

to add the array, being sName the inputted name

dicStudentArrays.Add(sName, studentArray)

to retrieve the array

studentArray = dicStudentArrays.Item(sName)

what I don't see is changing the name of a variable in the code while in execution

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to add more values to one specific array, or edit it? Can I do something like studentArray(index) = 90?
I'm not sure if I understand the question, but you can add as many elements (in this case arrays) to the dictionary as you want. To add more elements to the array you always can do ReDim Preserve studentArray(maxNumber).
and yes, the array is stored in the dictionary by reference, of course you can change values (without having to remove the array and add it after modified)