473,498 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing Objects with Option Strict On

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
5 1791
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2119
by: Microsoft News | last post by:
I have a project that was created all with Option Strict OFF. Works great, not a problem with it. But if I turn Option Strict ON then I get a LOT of errors. My question, should I even care...
5
1858
by: Plat | last post by:
Summary: I've got some *.ASPX pages that still use COM objects. I'd like to enable Option Strict, but I get "error BC30574: Option Strict On disallows late binding" errors. How can I bypass this...
11
2096
by: Daylor | last post by:
hi. im using option strict on. im doing in ,from the simple reason ,to be warn when there are implict conversion like string to int ,int to string. BUT. the price ,(now i see ), is very bad....
1
1826
by: JKM | last post by:
I'm a newbie to VB.NET and ASP.NET (as you'll see by my code). However, I'm not able to get a simple process to work. I'm trying to retrieve a record and then check to see if the 'accountstat'...
3
3969
by: Rich | last post by:
Hello, I am converting an app from VB6 to VB.Net and have encountered the following problem. I have the following loop which retrieves objects from a collection of objects. Dim entry As...
19
2616
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
5
5644
by: JL | last post by:
I need to compare two times. The problem I have is this: In my code I create a time variable using the format statement below: dim firstTime as DateTime fistTime = Format("12:00:00 AM", "T") ...
1
1335
by: Erick | last post by:
I have a generic comparer class that i use to help me sort collections of any object in any order. It worked with Option strict off but now i need it to work with option strict on. Here is...
8
2410
by: Rory Becker | last post by:
A wise man once said: "Never put off until runtime what you can fix at compile time." Actually I think he said it about 10 minutes before I started this post. I am a firm believer, like the...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.