Testing class type

  • Thread starter Thread starter Claire
  • Start date Start date
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();
 
Assuming you're setting the ClassType member with <object>.GetType(), to
compare use:

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

--Liam.
 
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
 
Back
Top