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.)

Charles
  • 50,943
  • 13
  • 104
  • 142
  • 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()`. – Tim Schmelter May 09 '14 at 23:50
  • I'm sorry, I'm sort of new to VB. List(Of Integer)? And do you mean a Structure? –  May 09 '14 at 23:54
  • No. There is almost no reason in .NET that you should ever need to use a struct/Structure. See http://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx – Douglas Barbin May 10 '14 at 00: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? –  May 10 '14 at 01: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.) –  May 10 '14 at 01:36
  • I think you should read about variable scope. This is basic stuff you'll have to master. – The_Black_Smurf May 10 '14 at 01:44
  • What I have at the moment is that the variable basically dies once it hits the end sub. And once I start it again, a new one is made. However, I need to recall previous data. Plus, apologies for all the trouble I'm causing. I just can't get my head around this at the moment. –  May 10 '14 at 01:51
  • Since your Grades are associated with each Student I would add a property to your Class that would Expose a List(of Integer) you could then add the grades to each Student and when you were finished you could store your Students into a List(of Student) variable. As you have said in your comments Arrays can be set to what you need now, but to increase their size will cause you to need to use Redim Preserve to resize them which to say the least is not very performant. – Mark Hall May 10 '14 at 02:05
  • If you want to keep your data between each event, you need to cast your variable at an upper level. (in your case, I think it would be at the form level) – The_Black_Smurf May 10 '14 at 02:05
  • Okay, I'll give that a shot now. Though, I think the program might be working now. It took me a while to get the new Dictionary thing working. But I still give these a try. Again thanks. Lemme try that out now. –  May 10 '14 at 02:08
  • Okay, thanks every one for helping, I finally got it working. I had to rearrange some things and used Dictionary to finish it off. Thank you all for all the help! –  May 10 '14 at 02:34
  • Always be sure to read the descriptions that appear when selecting tags! – Charles May 10 '14 at 03:43

1 Answers1

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

xavigonza
  • 170
  • 1
  • 7
  • Is there a way to add more values to one specific array, or edit it? Can I do something like studentArray(index) = 90? –  May 10 '14 at 01:03
  • 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). – xavigonza May 10 '14 at 12:26
  • 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) – xavigonza May 10 '14 at 12:31