|
-
May 25th, 2012, 04:52 AM
#1
[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!
-
May 25th, 2012, 05:07 AM
#2
Hyperactive Member
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
-
May 25th, 2012, 05:08 AM
#3
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.
-
May 25th, 2012, 09:18 AM
#4
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
-
May 25th, 2012, 02:41 PM
#5
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!
-
May 25th, 2012, 02:45 PM
#6
Re: [RESOLVED] Object property gets unwnated change?
 Originally Posted by opus
I didn't realize that List(of T) is an object! That was the reason for not using "New".
But you did use New: 
 Originally Posted by opus
Code:
Dim Frequencies As New List(Of Single)
Frequencies.Add(300)
-
May 25th, 2012, 02:47 PM
#7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|