|
Answer» I have this piece of code, as an example and I'm totally confused why it's not working properly. Code: [Select]Dim p(0) As Planet Dim m(0) As Planet p(0) = NEW Planet m(0) = New Planet p(0).IsMoon = False m = p m(0).IsMoon = True MsgBox("P: " & p(0).IsMoon & " M: " & m(0).IsMoon)And here's the planet class: Code: [Select]Public Class Planet Private _radius As Integer Private _Inhabited As Boolean Private _NaturalSatellite As Boolean Private _Population As Integer Private _NumberOfMoons As Integer Private av_temp As Integer Private av_temp_variation As Integer Private satellites() As Planet Private _ismoon As Boolean Private _res() As Resources Private _Neutral As Boolean Private _GoodGuys As Boolean Private _HelpingEramind As Boolean Private _ContainerCSV As String Public Property ContainerCSV As String Get Return _ContainerCSV End Get SET(value As String) _ContainerCSV = value End Set End Property Public Property Radius() As Integer Get Return _radius End Get Set(value As Integer) _radius = value End Set End Property Public Property Inhabited() As Boolean Get Return _Inhabited End Get Set(value As Boolean) _Inhabited = value End Set End Property Public Property HasMoon() As Boolean Get Return _NaturalSatellite End Get Set(value As Boolean) _NaturalSatellite = value End Set End Property Public Property POPULATION() As Integer Get Return _Population End Get Set(value As Integer) _Population = value End Set End Property Public Property AverageTemp() As Integer Get Return av_temp End Get Set(value As Integer) av_temp = value End Set End Property Public Property TempVariation() As Integer Get Return av_temp_variation End Get Set(value As Integer) av_temp_variation = value End Set End Property Public Property NumberOfMoons As Integer Get Return _NumberOfMoons End Get Set(value As Integer) _NumberOfMoons = value End Set End Property Public Property Moons As Planet() Get Return satellites End Get Set(value As Planet()) satellites = value End Set End Property Public Property IsMoon As Boolean Get Return _ismoon End Get Set(ByVal value As Boolean) _ismoon = value End Set End Property Public Property PlanetResources As Resources() Get Return _res End Get Set(ByVal value As Resources()) _res = value End Set End Property Public Property Neutral() As Boolean Get Return _Neutral End Get Set(value As Boolean) _Neutral = value End Set End Property Public Property GoodGuys() As Boolean Get Return _GoodGuys End Get Set(value As Boolean) _GoodGuys = value End Set End Property Public Property HelpingEramind() As Boolean Get 'okay Return _HelpingEramind End Get Set(value As Boolean) _HelpingEramind = value End Set End Property End Class Now, the weird thing is that when the MsgBox comes up, it prints "P: True M: True". Shouldn't it be "P: False M: True"? Why is vb.net, a language completely devoid of pointers, suddenly behaving as if it has them all of a sudden?Quote from: glaba on August 01, 2012, 11:45:25 AM I have this piece of code, as an example and I'm totally confused why it's not working properly.
It is working 'properly'. Actually, it would work similarly in VB4, VB5, or VB6. Code: [Select]Dim p(0) As Planet Dim m(0) As Planet p(0) = New Planet m(0) = New Planet p(0).IsMoon = False m = p m(0).IsMoon = True MsgBox("P: " & p(0).IsMoon & " M: " & m(0).IsMoon) the assignment between arrays copies the array p to the array m.
However, the array p is a 1-item array of "Planet". A Object VARIABLE is actually a REFERENCE to the actual object, not the object itself. Assigning Objects does not copy them. You assigned them both to new instances, but then set the m(0) to the same object as p(0).
QuoteNow, the weird thing is that when the MsgBox comes up, it prints "P: True M: True". Shouldn't it be "P: False M: True"? Why is vb.net, a language completely devoid of pointers, suddenly behaving as if it has them all of a sudden?
VB.NET has never been devoid of pointers in the form of references. A Object variable is a reference(pointer) to a instance of that object (or Nothing). If you really want them to be separate, you will need to implement a method within the class to clone them. Alternatively, you can make the class a struct.oh, i didn't realize that assigning objects didn't copy them thanks. i still find it odd that vb should copy the reference, though. I thought it was only C-based languages that did this.Quote from: glaba on August 01, 2012, 02:57:58 PMoh, i didn't realize that assigning objects didn't copy them thanks. i still find it odd that vb should copy the reference, though. I thought it was only C-based languages that did this.
I don't know of any language that doesn't.
|