about types

  • Thread starter Thread starter Owen
  • Start date Start date
O

Owen

Hello:

I want to know the class of object, to compare to some class, how can I do
that?

Owen.
 
Owen said:
Hello:

I want to know the class of object, to compare to some class, how can
I do that?

The is operator can do this quite simply, it gets the type and handles the
comparison for you. Ex:

MyClass myObject = new MyClass;
if (myObject is MyClass) return true;

That would return true.
--
Tom Porterfield
MS-MVP MCE
http://support.telop.org

Please post all follow-ups to the newsgroup only.
 
Owen said:
I want to know the class of object, to compare to some class, how can I do
that?

Well, there's:

if (myObject is SomeType)

or

myObject.GetType() to get the type of the object

or

typeof(SomeType) to get the type of SomeType (which you know in
advance)
 
Hi Owen,

If you want to check whether an object is instance of given class you can
use *as* or *is* operators.

If you want to check exactly the run-time class of an object
you can do

if(obj.GetType() == typeof(SomeType))
{
//The run-time type of the object is SomeType
....
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top