Understanding Get/Set With Properties That Are Arrays

A

ags5406

I'm having a little trouble understanding what is going on in the
following example. It's a simple console app that I built to
illustrate a problem I'm having in a bigger application. I have
sample class Car with property TirePressure() and propery Color. All
four tire pressures are set to 23.0 in the class constructor and the
color is set to Green. But then when I try to change the value of a
particular element of the TirePressure() array in Sub Main(), the code
in Set doesn't seem to execute, but somehow the value in TirePressure
(2) does get changed. However, when I change the value of Color the
code in Set does seem to execute.

In the output window I'd expect to get:

20.0
Oooh_I_Know_I_Know
Blue
Oooh_I_Know_I_Know

But in fact I get:

20.0
I_Dunno
Blue
Oooh_I_Know_I_Know

I put a breakpoint at _TirePressure = value in the Set statement for
TirePressure but the debugger doesn't stop there. I put a breakpoint
at _Color = value in the Set statement for Color but the debugger does
stop there.

Can anyone please explain to me what is going on? Thanks in advance
and my apologies if I've been verbose.





Module Module1
Sub Main()
Dim oCar As New Car
oCar.TirePressure(2) = 20.0
Debug.WriteLine(oCar.TirePressure(2).ToString("0.0"))
Debug.WriteLine(oCar.WasTirePressureChanged.ToString)
oCar.Color = "Blue"
Debug.WriteLine(oCar.Color)
Debug.WriteLine(oCar.WasColorChanged.ToString)
End Sub
End Module

Class Car

Private _TirePressure(3) As Single
Public Property TirePressure() As Single()
Get
Return _TirePressure
End Get
Set(ByVal value As Single())
_TirePressure = value
_WasTirePressureChanged =
PossibleAnswers.Oooh_I_Know_I_Know
End Set
End Property

Private _WasTirePressureChanged As PossibleAnswers
Public ReadOnly Property WasTirePressureChanged() As
PossibleAnswers
Get
Return _WasTirePressureChanged
End Get
End Property

Private _Color As String
Public Property Color() As String
Get
Return _Color
End Get
Set(ByVal value As String)
_Color = value
_WasColorChanged = PossibleAnswers.Oooh_I_Know_I_Know
End Set
End Property

Private _WasColorChanged As PossibleAnswers
Public ReadOnly Property WasColorChanged() As PossibleAnswers
Get
Return _WasColorChanged
End Get
End Property

Public Sub New()
_TirePressure(0) = 23.0
_TirePressure(1) = 23.0
_TirePressure(2) = 23.0
_TirePressure(3) = 23.0
_WasTirePressureChanged = PossibleAnswers.I_Dunno
_Color = "Green"
_WasColorChanged = PossibleAnswers.I_Dunno
End Sub

Public Enum PossibleAnswers
I_Dunno
Oooh_I_Know_I_Know
End Enum

End Class
 
T

Tom Shelton

I'm having a little trouble understanding what is going on in the
following example. It's a simple console app that I built to
illustrate a problem I'm having in a bigger application. I have
sample class Car with property TirePressure() and propery Color. All
four tire pressures are set to 23.0 in the class constructor and the
color is set to Green. But then when I try to change the value of a
particular element of the TirePressure() array in Sub Main(), the code
in Set doesn't seem to execute, but somehow the value in TirePressure
(2) does get changed. However, when I change the value of Color the
code in Set does seem to execute.

In the output window I'd expect to get:

20.0
Oooh_I_Know_I_Know
Blue
Oooh_I_Know_I_Know

But in fact I get:

20.0
I_Dunno
Blue
Oooh_I_Know_I_Know

I put a breakpoint at _TirePressure = value in the Set statement for
TirePressure but the debugger doesn't stop there. I put a breakpoint
at _Color = value in the Set statement for Color but the debugger does
stop there.

Can anyone please explain to me what is going on? Thanks in advance
and my apologies if I've been verbose.

Your property is returning a reference to the array - remember, arrays are
reference types. So, when you say:

oCar.TirePressure(2) = 20.0

the set is not called, because you are simply changin an element in the array.
If your were do do something like:

oCar.TirePressure = new Single(){20.0, 23.0, 21.0, 25.0}

Then your setter would be called. Basically, it's the reference to the array
that is get/set - not the individual items.

I think, you want your tire pressure property to look more like:

Public Property TirePressure(ByVal index As Integer) As Single
Get
Console.WriteLine("returning value")
Return _tirePressure(index)
End Get
Set(ByVal value As Single)
Console.WriteLine("setting value")
_tirePressure(index) = value
End Set
End Property


Of course, I'm not doing any bounds checking or anything here :) but, that
should give you the idea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top