PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS Code Trying to create a List of Objects-VBForums
Results 1 to 13 of 13

Thread: Trying to create a List of Objects

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    10

    Trying to create a List of Objects

    I'm new to the concept of Classes and have a simple (I think) question:

    I'm trying to create a List containing a group of Objects which are created from a Class called Person.
    The Class called Person contains two elements: name and age.
    In the main program I define a list called PeopleList of type Person.
    I add three names and ages to the list.
    However, at the end I 'print' the names in the list but what is printed is:
    Gary
    Gary
    Gary
    -----------------------------------------------
    Here's the program:
    Code:
    Public Class Person
        Private mstrName As String
        Private mstrAge As Integer
    
        Property name() As String
            Get
                Return mstrName
            End Get
            Set(ByVal value As String)
                mstrName = value
            End Set
        End Property
        Property age() As String
            Get
                Return mstrAge
            End Get
            Set(ByVal value As String)
                mstrAge = value
            End Set
        End Property
    End Class
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
            Dim PeopleList As New List(Of Person)
            Dim tempPerson As New Person
    
            tempPerson.name = "Chris"
            tempPerson.age = 53
            PeopleList.Add(tempPerson)
    
            tempPerson.name = "Sally"
            tempPerson.age = 25
            PeopleList.Add(tempPerson)
    
            tempPerson.name = "Gary"
            tempPerson.age = 68
            PeopleList.Add(tempPerson)
    
            For Each item In PeopleList
                tempPerson = item
                Console.WriteLine(tempPerson.name)
            Next
    
        End Sub
    
    End Class
    Last edited by Shaggy Hiker; Mar 10th, 2018 at 02:00 PM. Reason: Added CODE tags.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,775

    Re: Trying to create a List of Objects

    First things first, please use formatting tags when posting code snippets for the sake of readability. There are buttons on the editor toolbar for the purpose.
    vb.net Code:
    1. Public Class Person
    2.     Private mstrName As String
    3.     Private mstrAge As Integer
    4.  
    5.     Property name() As String
    6.         Get
    7.             Return mstrName
    8.         End Get
    9.         Set(ByVal value As String)
    10.             mstrName = value
    11.         End Set
    12.     End Property
    13.     Property age() As String
    14.         Get
    15.             Return mstrAge
    16.         End Get
    17.         Set(ByVal value As String)
    18.             mstrAge = value
    19.         End Set
    20.     End Property
    21. End Class
    22.  
    23. Public Class Form1
    24.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    25.  
    26.         Dim PeopleList As New List(Of Person)
    27.         Dim tempPerson As New Person
    28.  
    29.         tempPerson.name = "Chris"
    30.         tempPerson.age = 53
    31.         PeopleList.Add(tempPerson)
    32.  
    33.         tempPerson.name = "Sally"
    34.         tempPerson.age = 25
    35.         PeopleList.Add(tempPerson)
    36.  
    37.         tempPerson.name = "Gary"
    38.         tempPerson.age = 68
    39.         PeopleList.Add(tempPerson)
    40.  
    41.         For Each item In PeopleList
    42.             tempPerson = item
    43.             Console.WriteLine(tempPerson.name)
    44.         Next
    45.  
    46.     End Sub
    47.  
    48. End Class
    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

  3. #3
    Lively Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    77

    Re: Trying to create a List of Objects

    for each person you add to you list, use tmpperson = new person, not just add the beginning....
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  4. #4
    Lively Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    77

    Re: Trying to create a List of Objects

    Personly i would likely write a constructor in the person class that would take a name and a age, and then populate the list like so :
    Code:
    PeopleList.Add(New Person(Name,Age))
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,775

    Re: Trying to create a List of Objects

    As for the issue, your intent is obviously to add three Person objects to your collection but that is not what you are doing. What you are actually doing is adding the same Person object three times. As there is only one Person object, it can only have one set of property values so of course you see the same values no matter which item you get that one Person object from. This is the code that creates the Person object:
    vb.net Code:
    1. New Person
    As you can see, that code only appears once in your code, so you're only creating one object. After you add the first Person to the collection, you need to create another new Person object and set its properties, not set the properties of the Person object that you already added to the collection. After you add the second Person, you need to create a third Person object. Each Person object you want to add to the collection needs to be created and you do that with the New keyword.
    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

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,775

    Re: Trying to create a List of Objects

    Quote Originally Posted by Goggy View Post
    Personly i would likely write a constructor in the person class that would take a name and a age, and then populate the list like so :
    Code:
    PeopleList.Add(New Person(Name,Age))
    In this age of object initialisers, there's no point to writing a constructor unless it does some extra work over just setting properties or it sets mandatory properties. In this case, just use an object initialiser:
    vb.net Code:
    1. PeopleList.Add(New Person With {.Name = Name, .Age = Age))
    If I remember correctly, that is Microsoft's recommendation on the subject.
    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    10

    Re: Trying to create a List of Objects

    Sorry about the formatting problem. Hope I did it correctly this time.

    Okay, I think I understand. So I modified the code to create a new 'tempPerson' each time I'm adding a person to the list (see the code below). And it works. But how do I do it if I need to add many people to the list?

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Dim PeopleList As New List(Of Person)

    Dim temp1Person As New Person
    temp1Person.name = "Chris"
    temp1Person.age = 53
    PeopleList.Add(temp1Person)

    Dim temp2Person As New Person
    temp2Person.name = "Sally"
    temp2Person.age = 25
    PeopleList.Add(temp2Person)

    Dim temp3Person As New Person
    temp3Person.name = "Gary"
    temp3Person.age = 68
    PeopleList.Add(temp3Person)

    For Each item In PeopleList
    'tempPerson = item
    Console.WriteLine(item.name)
    Next

    End Sub

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    10

    Re: Trying to create a List of Objects

    I see I failed to format it again? Sorry - I thought I had it this time! (

  9. #9
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,735

    Re: Trying to create a List of Objects

    Just click on the "#" then insert your code between the tags

  10. #10
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,735

    Re: Trying to create a List of Objects

    But how do I do it if I need to add many people to the list?
    Where is this information going to come from? A database, someone entering manually, .....

  11. #11

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    10

    Re: Trying to create a List of Objects

    The input will come from someone entering the information manually.

  12. #12
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,735

    Re: Trying to create a List of Objects

    There is more than one way to do this but a simple way would be to add a Button to the form, call "Save". Then after they enter a persons information they click Save and you add the data to the list.

    You will need to move this out of the Form Load and make it a form level object.
    Code:
    Dim PeopleList As New List(Of Person)
    Code:
    Public Class Form5
        Private PeopleList As New List(Of Person)
    
        Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
            'add data to list
            'clear the controls so another person can be entered
        End Sub
    End Class

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,775

    Re: Trying to create a List of Objects

    Quote Originally Posted by BillMc View Post
    Sorry about the formatting problem. Hope I did it correctly this time.

    Okay, I think I understand. So I modified the code to create a new 'tempPerson' each time I'm adding a person to the list (see the code below). And it works. But how do I do it if I need to add many people to the list?

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Dim PeopleList As New List(Of Person)

    Dim temp1Person As New Person
    temp1Person.name = "Chris"
    temp1Person.age = 53
    PeopleList.Add(temp1Person)

    Dim temp2Person As New Person
    temp2Person.name = "Sally"
    temp2Person.age = 25
    PeopleList.Add(temp2Person)

    Dim temp3Person As New Person
    temp3Person.name = "Gary"
    temp3Person.age = 68
    PeopleList.Add(temp3Person)

    For Each item In PeopleList
    'tempPerson = item
    Console.WriteLine(item.name)
    Next

    End Sub
    You need to create a new Person object each time but that doesn't mean you need a new variable each time. You need to understand the difference between a variable and an object. One variable can refer to multiple different objects and one object can be assigned to multiple variables at the same time. You only need one variable. You just assign a new Person object to it each time. That said, it sounds like you won't be adding more than one item at a time. The user can only enter one set of data at a time, can't they?
    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

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width