Is Operator question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm a beginner to programming & have a Question with the Is operator
when used in a class; sorry for the elementary question.

I have created a Class "Person3", in my Module I
create three different objects from this class however when I call the
"Spouse" Property and pass one of my objects and test if they are of
the same reference I get a True value. My questions is why? I'm
passing a different instanced object? When I test the following in the
module I get false, why do I get a diffrent result in the object?


bResult = joe Is joe ' true
bResult = joe Is ann ' false
bResult = joe Is Ann2 ' false


Any Clarifications would greatly be appreciated. Thanks


Module Module1
Sub Main()
Dim Joe as New Person3("joe", "Smith")
Dim ann As New Person3("Ann", "Smith")
Dim Ann2 As New Person3("Ann2", "Smith")


' this is where my issue begins
joe.Spouse = ann
joe.Spouse = Ann2
End Sub
End Module


Public Class Person3


Dim m_Spouse As Person3
Public m_FirstName As String
Public m_LastName As String


Public Sub New(ByVal firstName As String, _
ByVal lastName As String)
Me.m_FirstName = firstName
Me.m_LastName = lastName
End Sub


End Sub Public WriteOnly Property Spouse() As Person3
Set(ByVal Value As Person3)
Dim b As Boolean
m_Spouse = Value
b = m_Spouse Is Value
Console.WriteLine("the boolean value is: " & b)
End Set
End Property
End Class
 
rSmtih said:
I'm a beginner to programming & have a Question with the Is operator
when used in a class; sorry for the elementary question.

I have created a Class "Person3", in my Module I
create three different objects from this class however when I call the
"Spouse" Property and pass one of my objects and test if they are of
the same reference I get a True value. My questions is why? I'm
passing a different instanced object? When I test the following in the
module I get false, why do I get a diffrent result in the object?

You're testing whether or not the spouse is the value which has just
been set:
End Sub Public WriteOnly Property Spouse() As Person3
Set(ByVal Value As Person3)
Dim b As Boolean
m_Spouse = Value
b = m_Spouse Is Value

Why would m_Spouse be different from Value, when you've just set
m_Spouse to be the same as Value?
 
rSmith,

The IS operatar in VBNet is to check if an object is the same
dim a as bclass
dim c as bclass

If a IS c 'now equal because they are both empty (nothing)

a = new bclass
c = a
If a IS c 'they are still equal because they reference too the same object

c = new bclass
If a Is c 'they are now unequal because it are two different objects

I hope this helps?

Cor
 
Back
Top