Results 1 to 5 of 5

Thread: Help with Building an Array?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    1

    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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class Thing
    2.     Public Name As String
    3.     Public Score As Integer
    4. End Class
    With auto properties:
    vb.net Code:
    1. Public Class Thing
    2.     Public Property Name As String
    3.     Public Property Score As Integer
    4. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width