Testing class type

C

Claire

I want to pass the type of an object as a field in an object.

public class FileEventArgs : EventArgs

{

public object Data = null;

public Type ClassType = null;

}

In another function, I want to test ClassType. Depending on the Class Type
Data is handled differently.

I can't work out the correct way to do it - the following doesn't work, can
someone help please.

I could do it another way easily but I now wonder what Im doing wrong.

if (e.ClassType == DI225.SerialDriver)

Converter = new DI225.Converter();

else if (e.ClassType == PPC.Driver)

Converter = new PPC.Converter();
 
L

Liam McNamara

Assuming you're setting the ClassType member with <object>.GetType(), to
compare use:

if(e.ClassType == typeof(DI225.SerialDriver))
....

--Liam.
 
M

Mohamoss

Hi Claire
You can use either the "Is" keyword or the "As" keyword to check for the
type.
Is operator checks if an object of some type and the expression returns a
Boolean ( object_A Is String ) . while the as operator try to cast an
object to another and return null if the cast is not doable , you can use
that as a test too .
hope this helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 

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

Top