473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Comparing object types

What is the best way to determine if two objects are of the same type? The
'is' operator is not precise enough. Currently I am using
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must be
a better way. The following code gives a better idea of the situation:

abstract class A
{
//the arguments could be instances of any of the classes defined
static void TestFunc(A obj1, A obj2)
{
if(obj1.GetType().Name == obj2.GetType().Name)
{ ... //some code}
else
{ ... // do something else }
}
}
class B {} : A
class B2 {} : A
class C {} : B

Just a thought: would (Obj1 is Obj2 && Obj2 is Obj1) work better and be more
efficient?

--Ken
Nov 15 '05 #1
9 3707
Kenneth Baltrinic wrote:
Just a thought: would (Obj1 is Obj2 && Obj2 is Obj1) work better and
be more efficient?


I'm not following you. Why two tests? If either is true so is the other.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
100
Hi Kenneth,
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must

be

Obj1.GetType() == Obj2GetType()

There is only one Type-class instance per type. Thus, if the objects are of
the same type GetType method will return the same reference regardless of
the object which GetType method is called.

HTH
B\rgds
100
Nov 15 '05 #3
cs

"Kenneth Baltrinic" <ke*****@baltrinic.com> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
What is the best way to determine if two objects are of the same type? The 'is' operator is not precise enough. Currently I am using
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must be a better way. The following code gives a better idea of the situation:

abstract class A
{
//the arguments could be instances of any of the classes defined
static void TestFunc(A obj1, A obj2)
{
if(obj1.GetType().Name == obj2.GetType().Name)
{ ... //some code}
else
{ ... // do something else }
}
}
class B {} : A
class B2 {} : A
class C {} : B

Just a thought: would (Obj1 is Obj2 && Obj2 is Obj1) work better and be more efficient?

--Ken


in java there is only one object Class created for each different class, in
C# my guess is that there is only one Type created for each different type,
so calling .Equals on a type should always give you the correct answer, for
example this prints true:
Console.WriteLine("asd".GetType().Equals("asd1222" .GetType()));
Nov 15 '05 #4
Ah that was exactly what I was hoping was the case but could not confirm.
Glad to know it.

"100" <10*@100.com> wrote in message
news:OR**************@TK2MSFTNGP11.phx.gbl...
Hi Kenneth,
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must be

Obj1.GetType() == Obj2GetType()

There is only one Type-class instance per type. Thus, if the objects are

of the same type GetType method will return the same reference regardless of
the object which GetType method is called.

HTH
B\rgds
100

Nov 15 '05 #5
Frank Oquendo wrote:
I'm not following you. Why two tests? If either is true so is the
other.


Strike that. Duh.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #6
cs <[rem]casolorz[rem]@hot[rem]mail.com> wrote:
in java there is only one object Class created for each different class, in
C# my guess is that there is only one Type created for each different type,
so calling .Equals on a type should always give you the correct answer, for
example this prints true:
Console.WriteLine("asd".GetType().Equals("asd1222" .GetType()));


There only being one Type instance created for each type means that you
*don't* need to call Equals - you can just use ==.

If you ended up with multiple Type instances for each type, *then* you
would probably need to use Equals (which would probably do the right
thing).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
cs

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
cs <[rem]casolorz[rem]@hot[rem]mail.com> wrote:
in java there is only one object Class created for each different class, in C# my guess is that there is only one Type created for each different type, so calling .Equals on a type should always give you the correct answer, for example this prints true:
Console.WriteLine("asd".GetType().Equals("asd1222" .GetType()));


There only being one Type instance created for each type means that you
*don't* need to call Equals - you can just use ==.

If you ended up with multiple Type instances for each type, *then* you
would probably need to use Equals (which would probably do the right
thing).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


true :-)
Nov 15 '05 #8
Hi,
As we're never sure that two objects have the same reference even with the
same data and to avoid confusion, the comparison must take into account the
namespace and the assembly of the compared types.
The function i personnaly use is :

public bool areEqualTypes(Type type1,Type type2)
{
bool equal=false;
if
((type1.Assembly.FullName==type2.Assembly.FullName )&&(type1.Name==type2.Name
)&&(type1.Namespace==type2.Namespace))
equal=true;
return equal;
}

Hope this helps.
Abdessamad.

"Kenneth Baltrinic" <ke*****@baltrinic.com> a écrit dans le message de
news:ek**************@TK2MSFTNGP11.phx.gbl...
What is the best way to determine if two objects are of the same type? The 'is' operator is not precise enough. Currently I am using
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must be a better way. The following code gives a better idea of the situation:

abstract class A
{
//the arguments could be instances of any of the classes defined
static void TestFunc(A obj1, A obj2)
{
if(obj1.GetType().Name == obj2.GetType().Name)
{ ... //some code}
else
{ ... // do something else }
}
}
class B {} : A
class B2 {} : A
class C {} : B

Just a thought: would (Obj1 is Obj2 && Obj2 is Obj1) work better and be more efficient?

--Ken

Nov 15 '05 #9
Abdessamad Belangour <be*******@irin.univ-nantes.fr> wrote:
As we're never sure that two objects have the same reference even with the
same data


Are we not? Not *in general*, admittedly, but from the documentation of
Type:

<quote>
A Type object that represents a type is unique; that is, two Type
object references refer to the same object if and only if they
represent the same type. This allows for comparison of Type objects
using reference equality.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Raphael Iloh | last post by:
Hi all, I'm having problems comparing array objects. Take a look at this: int array1 = new int{1}; int array2 = new int{1}; Console.Writeln(array1.Equals(array2)); One would expect the above...
5
by: George | last post by:
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...
19
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 =...
2
by: Dennis | last post by:
Why won't either one of the below If expressiions evaluate to TRUE: Dim ob As Object ob = "This String" If ob.GetType Is Type.GetType("String") Then ..... End If If ob.GetType is...
5
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where...
6
by: Edward Diener | last post by:
Now that operator overloading allows to ref classes to be compared for equality using == syntax, how does one compare the actual ref pointers ( ^ ) for equality instead ? As an example: ...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
3
by: Mike J | last post by:
Hi..need help comparing types example method istypeof(object someobj) { if (someobj=int32) } ya kinda get my idea here.... MJ
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.