Is Operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonia.sardana
    New Member
    • Jul 2006
    • 95

    Is Operator

    The Is operator determines if two object references refer to the same object.
    If object1 and object2 both refer to the same object, result is True; if they do not, result is False.

    Check the code below , both objects refer to the same object,but the answer is coming false.Example
    Module Module1
    Public Class base

    End Class
    Sub main()
    Dim obj As New base
    Dim obj1 As New base
    Dim check As Boolean
    check = obj Is obj1
    Console.WriteLi ne(check)
    Console.ReadLin e()
    End Sub
    End Module
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Originally posted by sonia.sardana
    Dim obj As New base
    Dim obj1 As New base
    Dim check As Boolean
    check = obj Is obj1
    Console.WriteLi ne(check)
    Console.ReadLin e()
    You're missing the point obj and obj1 are both of type "Base" but they're not referring to the same object.

    Dim obj1 As New Base
    Dim obj2 As New Base

    is not the same as

    Dim obj1 As New Base
    Dim obj2 As Base = obj1

    What you're trying to do is say are both my objects of the type Base and what you're actually asking is are both my objects the same - i.e. identical.

    Assume (for argument's sake) I have an identical twin called Bob.
    Bob != Ben; Bob might look like Ben, sound like Ben, act like Ben, but we're not the same person. Does that make sense? You are referring to two different entities that for the purpose of example may look identitcal, feel identical and act identically, but they're not the same entity.

    You would need some kind of compare function to see if the two objects are equal - when I say equal, I mean logically equal - i.e. do all the properties of the two objects hold matching values?

    Comment

    Working...