By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Testing class type

P: n/a
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();


Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


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

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

--Liam.

"Claire" <as*******@ntlworld.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
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();

Nov 16 '05 #2

P: n/a
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

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.