Help with Building an Array?
Hi everybody, I’m relatively new at using Visual Basic, and I'm trying to make a decathlon calculator. My question is how do I make a blank array, and after each person is done calculating their decathlon score it adds the persons name, school,(which are both in text boxes) and score(which is calculated into a textbox) to the blank array. Anything would truly help and I appreciate your time. Thanks
Re: Help with Building an Array?
Don't use an array, to begin with. If you are going to be dynamically adding things, using an array would just be tedious. What you want to use, in that case, is a List (of T), where T is the datatype. So, what is the datatype? Since you have several different properties that you want to save, the easiest thing to do would be to create a class that has members for Name, School, and Score. These can either be Public members, or Private members accessed through public properties. The latter solution takes a little more work, but is the recommended design for a variety of reasons. The reason to use properties that seems most likely to apply, in your case, is that you can then databind to those properties, where you can't databind to public member variables.
Re: Help with Building an Array?
Shaggy says that using properties is a little more work but it may not be. If you're using VB 2010 then you can use auto properties. With public fields:
vb.net Code:
Public Class Thing
Public Name As String
Public Score As Integer
End Class
With auto properties:
vb.net Code:
Public Class Thing
Public Property Name As String
Public Property Score As Integer
End Class
If you're using an earlier version then you can make use of a code snippet to help you out. Just type prop and then hit Enter twice. The code for a public property with private backing field will be added and you can just Tab to enter the names and data types.
Re: Help with Building an Array?
I disagree that "score" is an integer.
Sounds to me like Score is a class, which contains the logic for calculating the Total Score, as well as metods for supplying the scores for individual events.
To include the Name and School values in this class would be an SRP violation. Those would be encapsulated in their own class of Competitor - possibly that includes an instance of Score?
Re: Help with Building an Array?
Though you do have the means to create auto properties, I haven't really taken to them. They organize things in a way that I feel is distasteful, but that is ENTIRELY because of habits I have formed. Any other person can do as they prefer.