By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,728 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,728 IT Pros & Developers. It's quick & easy.

Comparing Objects with Option Strict On

P: n/a
How do I compare the values of two objects when Option Strict is On?

One of the objects is the Tag property from some control (declared as object
but holding a value, e.g. an Integer or a String or a DateTime or ...).
The other object is the Item property from a DataRow (declared as object but
holding a value, e.g. an Integer or a String or a DateTime or ...).

I can find out that they hold the same type using "If
objFirst.GetType.Equals(objSecond).GetType) Then" but I cannot do "If
objFirst = objSecond Then".

Also I cannot convert them to their real types for comparison because
"CType(objFirst, objFirst.GetType)" does not work.

The Is operator only compares the pointers not the values so is not useful
here. To illustrate the following outputs False even though 2 = 2.

Dim o1 as Object, o2 as Object
o1 = 2
o2 = 2
If o1 Is o2 Then
MsgBox("True")
Else
MsgBox("False")
End If

Any ideas?

George.
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
George,

Take forever the highest class where the property is still in so probably in
your situation.

If directcast(wathever,control).tag =
directcast(whatever,datarow).item(y).tostring

I hope this helps?

Cor

"George" <ge****@george.com> ..
How do I compare the values of two objects when Option Strict is On?

One of the objects is the Tag property from some control (declared as object but holding a value, e.g. an Integer or a String or a DateTime or ...).
The other object is the Item property from a DataRow (declared as object but holding a value, e.g. an Integer or a String or a DateTime or ...).

I can find out that they hold the same type using "If
objFirst.GetType.Equals(objSecond).GetType) Then" but I cannot do "If
objFirst = objSecond Then".

Also I cannot convert them to their real types for comparison because
"CType(objFirst, objFirst.GetType)" does not work.

The Is operator only compares the pointers not the values so is not useful
here. To illustrate the following outputs False even though 2 = 2.

Dim o1 as Object, o2 as Object
o1 = 2
o2 = 2
If o1 Is o2 Then
MsgBox("True")
Else
MsgBox("False")
End If

Any ideas?

George.

Nov 21 '05 #2

P: n/a
I think you need to ask yourself the question, why do I want to compare
things I dont know the type of? It would seem odd that you would not know
the types and yet want to compare them. At the very least, one would expect
that you would know of perhaps a few types which may be boxed as an object.
In thise case one would perhaps expect you to use a Select Case construct
and take appropriate action resulting from such a test.

Also when you compare objects be carefull of what you are comparing, you may
get unpredictable results in terms of what you are trying to acheive.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"George" <ge****@george.com> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
How do I compare the values of two objects when Option Strict is On?

One of the objects is the Tag property from some control (declared as object but holding a value, e.g. an Integer or a String or a DateTime or ...).
The other object is the Item property from a DataRow (declared as object but holding a value, e.g. an Integer or a String or a DateTime or ...).

I can find out that they hold the same type using "If
objFirst.GetType.Equals(objSecond).GetType) Then" but I cannot do "If
objFirst = objSecond Then".

Also I cannot convert them to their real types for comparison because
"CType(objFirst, objFirst.GetType)" does not work.

The Is operator only compares the pointers not the values so is not useful
here. To illustrate the following outputs False even though 2 = 2.

Dim o1 as Object, o2 as Object
o1 = 2
o2 = 2
If o1 Is o2 Then
MsgBox("True")
Else
MsgBox("False")
End If

Any ideas?

George.

Nov 21 '05 #3

P: n/a
Your code will not compile with Option Strict On because
directcast(wathever,control).tag is of type Object and the operator = is not
allowed with operands of type Object.

However directcast(whatever,datarow).item(y).tostring is interesting.
Intellisense does not tell me that ToString is a method of Object but the
help files do. This means that I can write "If objFirst.ToString =
objSecond.ToString Then". As I have already verified they are the same type,
e.g. both Integer or both String, then if their values are the same, their
string representation should be the same. Conversely if their string
representation is different then they must have started out with different
values.

Thus,

Option Strict On

Public Shared Function ObjectValuesAreEqual(ByVal FirstObject As Object,
ByVal SecondObject As Object) As Boolean
If FirstObject.GetType.Equals(SecondObject.GetType) Then
If FirstObject.ToString = SecondObject.ToString Then
Return True
End If
End If
Return False
End Function

Thanks,

George.

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:OU**************@TK2MSFTNGP12.phx.gbl...
George,

Take forever the highest class where the property is still in so probably in your situation.

If directcast(wathever,control).tag =
directcast(whatever,datarow).item(y).tostring

I hope this helps?

Cor

"George" <ge****@george.com> ..
How do I compare the values of two objects when Option Strict is On?

One of the objects is the Tag property from some control (declared as

object
but holding a value, e.g. an Integer or a String or a DateTime or ...).
The other object is the Item property from a DataRow (declared as object

but
holding a value, e.g. an Integer or a String or a DateTime or ...).

I can find out that they hold the same type using "If
objFirst.GetType.Equals(objSecond).GetType) Then" but I cannot do "If
objFirst = objSecond Then".

Also I cannot convert them to their real types for comparison because
"CType(objFirst, objFirst.GetType)" does not work.

The Is operator only compares the pointers not the values so is not useful here. To illustrate the following outputs False even though 2 = 2.

Dim o1 as Object, o2 as Object
o1 = 2
o2 = 2
If o1 Is o2 Then
MsgBox("True")
Else
MsgBox("False")
End If

Any ideas?

George.


Nov 21 '05 #4

P: n/a
George,
Your code will not compile with Option Strict On because
directcast(wathever,control).tag is of type Object and the operator = is not allowed with operands of type Object.

No and whathever is not instanced and whatever can never be two times from
the same type, it is only a sample about the casting.

May I know why this tone of critique in you message?

Cor
Nov 21 '05 #5

P: n/a
George,
You almost answered your own question ;-)

Try:

If objFirst.Equals(objSecond) Then

Most members of System.Object are considered Advanced Members, which means
they are normally hidden from Intellisense.

You can use 'Tools - Options - Text Editor - Basic - General - Hide advance
members' to control whether the advanced members of types are shown or
hidden in Intellisense.

Hope this helps
Jay

"George" <ge****@george.com> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
How do I compare the values of two objects when Option Strict is On?

One of the objects is the Tag property from some control (declared as
object
but holding a value, e.g. an Integer or a String or a DateTime or ...).
The other object is the Item property from a DataRow (declared as object
but
holding a value, e.g. an Integer or a String or a DateTime or ...).

I can find out that they hold the same type using "If
objFirst.GetType.Equals(objSecond).GetType) Then" but I cannot do "If
objFirst = objSecond Then".

Also I cannot convert them to their real types for comparison because
"CType(objFirst, objFirst.GetType)" does not work.

The Is operator only compares the pointers not the values so is not useful
here. To illustrate the following outputs False even though 2 = 2.

Dim o1 as Object, o2 as Object
o1 = 2
o2 = 2
If o1 Is o2 Then
MsgBox("True")
Else
MsgBox("False")
End If

Any ideas?

George.

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.