Compare variables: Object?

I have two variables, v1 and v2. I need to know if they are both instances of the same object (not necessarily pointing to the same instance, just being instances of the same object is enough). Brain freeze on my end…

dim c1 as new Class1
dim c2 as new Class1

if c1 isA Class1 AND c2 isA Class1 then
msgBox “c1 = c2”
end if

Does IsA get you somewhere if you know the class type in advance? If not then look at Introspection

dim t as Introspection.TypeInfo = Introspection.GetType(someObject)

Now t will have some useful properties including the FullName which you could compare to another object’s

You can get the class name wth Introspection. Something like:

Dim ti1 as Introspection.Typeinfo = Introspection.GetType(obj1) Dim ti2 as Introspection.Typeinfo = Introspection.GetType(obj2) If ti1.FullName = ti2.Fullname Then ...
You can also walk up the inheritance hierarchy by using:

Dim superclassTypeInfo As Introspection.Typeinfo = ti1.BaseType

To check if the two variables are the same instance…

Dim v1 As new MemoryBlock(10)
Dim v2 As MemoryBlock = v1

if v1 is v2 then
MsgBox “Awesome”
end if[/code]

isA won’t work since I don’t know the super class in advance. Introspection neither as I am comparing WebContainerControls (should have mentioned, sorry!)

I habe a couple of web container controls that are dynamically exchanged/placed based upon the action of a WebToolbar. I store a reference of the currently loaded CC in a property of the webpage. When the user clicks on a toolbar button, I close the old CC, embed the new one and set the property accordingly. However, if the user clicks on the same button again, it shouldn’t go through the whole process again but just do nothing…

If you have not implemented operator_compare then

if v1 = v2

just checks if they are the same reference

Norman, they are both different instances…

So I see (still need more coffee)

if v1 isa v2 and v2 isa v1

require they be the SAME class and not super’s / subs

if they can be supers / subs then you have a much harder problem because the simple line above wont work
You might actually need introspection to walk up the hierarchy

Can this do the job?

  1. Create a Classproperty Name as string
  2. Constrcutor: me.Name = Introspection.GetType(self).Name
  3. if v1.Name = v2.Name then

[quote=28952:@Joachim Kuno]Can this do the job?

  1. Create a Classproperty Name as string
  2. Constrcutor: me.Name = Introspection.GetType(self).Name
  3. if v1.Name = v2.Name then[/quote]

This does the same as

if v1 isa v2

dim v1 as new class1
dim v2 as new class1

if v1 IsA v2 then
msgBox “v1 = v2”
end if

Result: Compiler Error

Can you do IsA on two instances? When I read the docs it didn’t look like you could.

I thought you would need to have specialised WebContainerControls for this to work otherwise a WebContainerControl class is a WebContainerControl class. If your classes are different classes then testing the instances with

if Introspection.GetType(obj1).Name = Introspection.GetType(obj2).Name then…

should test ok.

If they are both the same (container) class then you will need to do something like look for a specific control within them to see if they are the same or not.

Since you know the “base” class explicitly, you can use ISA to rule out the second click. Don’t try to make this too generic.