Results 1 to 7 of 7

Thread: [RESOLVED] Object property gets unwnated change?

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [RESOLVED] Object property gets unwnated change?

    I'm creating an new class object with a property that holds a generic List(of Singles). After creating this object, I change this generic list. Since I didn't pass that list as ByRef in the New-Procedure, I would expect to already created object instance NOT to change, but that not the case. Where am I wrong?

    Code:
    'Class
    Public Class clVessel
    
        Public Sub New(ByVal ID As Integer, ByVal Frequencies As List(Of Single))
            With Me
                .ID = ID
                .Frequenzen = Frequenzen
            End With
        End Sub
    
       Private _ID As Integer
        Public Property ID() As Integer
            Get
                Return _ID
            End Get
            Set(ByVal value As Integer)
                _ID = value
            End Set
        End Property
    
    End Class 
    
    'in the MainForm
    Public AllVessels As List(Of clVessel) 
    Dim Frequencies As New List(Of Single)
    Frequencies.Add(300)
    
    For j As Integer = 1 To 100
                    Frequencies(0) = 300 + j
                    AllVessels.Add(New clVessel(1, Frequencies))
    Next
    I would have expected that all instances of clVessel in the List "AllVessels" to have different Frequencies, however all have it at the value of 400??
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    351

    Re: Object property gets unwnated change?

    Not sure, but my first thought is that the value (as in byval) is the list of singles not the actual value of the singles themselves. Make sense?

    Also why are you using a list of frequencies when you are only using 1 value?
    Rico

    Using: VB.net & MS SQL

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

    Re: Object property gets unwnated change?

    List(Of T) is a class. In order to create an instance of a class you MUST invoke a constructor using the New keyword. How many times in that code do you do that for the List(Of Single)? Once, therefore there is only one object created. If there's only one List(Of Single) object then obviously every clVessel object must refer to that same object.

    Passing a parameter ByVal means that whatever variable you pass is copied and the copy passed to the method. List(Of T) is a class, i.e. a reference type, so the variable contains a reference to an object. When you pass the List(Of Single) to the constructor a copy of that reference is created. As such, you have two references that both refer to the same object, but still only one object. It's always that way for reference type objects. It's exactly the same as assigning one variable to another. The second variable contains a reference to the same object. The only way to create a copy of a reference type object is to actually create a copy, which is always going to involve invoking a constructor with the New keyword again somewhere, either in your code or in the code you call.
    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
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Object property gets unwnated change?

    The only difference when you pass a reference object to a sub ByVal instead of ByRef is that you can't change the original reference object. In other words you can't get a list(of single) and change that to a new list(of single).

    In your case what you probably want to do is something like this:
    Code:
        Public Sub New(ByVal ID As Integer, ByVal Frequencies As List(Of Single))
            With Me
                .ID = ID
                .Frequenzen = New List(Of Single)
                .Frequenzen.AddRange(Frequencies)
            End With
        End Sub

  5. #5

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Object property gets unwnated change?

    I didn't realize that List(of T) is an object! That was the reason for not using "New".

    Thanks for the insight.

    @enrico1982: The code posted was only a porting of the real code (also I tried to change the variable-names to English, but I failed!). I'm using a List in order to accept variable numbers of frequencies. That would have worked with an array, but I'm trying to use more and more of the ".Net"-Stuff!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [RESOLVED] Object property gets unwnated change?

    Quote Originally Posted by opus View Post
    I didn't realize that List(of T) is an object! That was the reason for not using "New".
    But you did use New:
    Quote Originally Posted by opus View Post
    Code:
    Dim Frequencies As New List(Of Single)
    Frequencies.Add(300)

  7. #7

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [RESOLVED] Object property gets unwnated change?

    You got me
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

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