GetType Question - Best way to do this?

D

Doug Handler

Hi,

I'm using a modified Tree control that contains a Tag property of Object. I
iterate through two different tables to build the Tree w/ root nodes being
Groups and child nodes being GroupMembers. As i iterate, i'm adding to the
Tag property either the instance of that specific Group or GroupMember.
Works fine, all is good.

My question is this....I need to know if the user selected on the Tree a
Group "node" or a GroupMember "node." When the user clicks on the node I'm
returned the currentSelectedNode and that contains the Tag property (again
fine). What's the best way to determine if Tag is a Group or GroupMember?
I'm currently doing the following and wasn't sure if this is the best way:

Type type = this.currentSelectedNode.Tag.GetType();

if (type.Name = "Group")
.....
else
if (type.Name = "GroupMember")
.....

Thanks in advance
dh
 
N

Nicholas Paldino [.NET/C# MVP]

Doug,

I would actually do this:

if (type == typeof(Group))

if (type == typeof(GroupMember))

This would be the better way, as opposed to checking the name of the
type.

Also, what you could do is have Group and GroupMember derive from the
same type (or implement the same interface), and have the base type or
interface have a method/property return a boolean value indicating whether
or not it is a group or group member.

This way, you could cast the tag to the interface/base class, and then
make your determination.

Hope this helps.
 
D

Doug Handler

Nicholas,

Thanks for getting back to me quickly...cause i don't want to go back and
change my Group and GroupMembers' code right now, i'll take the "easy" way
and use what you recommended at the top.

dh
Nicholas Paldino said:
Doug,

I would actually do this:

if (type == typeof(Group))

if (type == typeof(GroupMember))

This would be the better way, as opposed to checking the name of the
type.

Also, what you could do is have Group and GroupMember derive from the
same type (or implement the same interface), and have the base type or
interface have a method/property return a boolean value indicating whether
or not it is a group or group member.

This way, you could cast the tag to the interface/base class, and then
make your determination.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Doug Handler said:
Hi,

I'm using a modified Tree control that contains a Tag property of Object.
I iterate through two different tables to build the Tree w/ root nodes
being Groups and child nodes being GroupMembers. As i iterate, i'm
adding to the Tag property either the instance of that specific Group or
GroupMember. Works fine, all is good.

My question is this....I need to know if the user selected on the Tree a
Group "node" or a GroupMember "node." When the user clicks on the node
I'm returned the currentSelectedNode and that contains the Tag property
(again fine). What's the best way to determine if Tag is a Group or
GroupMember? I'm currently doing the following and wasn't sure if this is
the best way:

Type type = this.currentSelectedNode.Tag.GetType();

if (type.Name = "Group")
....
else
if (type.Name = "GroupMember")
....

Thanks in advance
dh
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Dough,

I believe it would be better of you do

if(type == typeof(Group)) and the same for the other types.
 
D

Doug Handler

Got it - thanks!!!!

dh
Stoitcho Goutsev (100) said:
Dough,

I believe it would be better of you do

if(type == typeof(Group)) and the same for the other types.


--

Stoitcho Goutsev (100) [C# MVP]

Doug Handler said:
Hi,

I'm using a modified Tree control that contains a Tag property of Object.
I iterate through two different tables to build the Tree w/ root nodes
being Groups and child nodes being GroupMembers. As i iterate, i'm
adding to the Tag property either the instance of that specific Group or
GroupMember. Works fine, all is good.

My question is this....I need to know if the user selected on the Tree a
Group "node" or a GroupMember "node." When the user clicks on the node
I'm returned the currentSelectedNode and that contains the Tag property
(again fine). What's the best way to determine if Tag is a Group or
GroupMember? I'm currently doing the following and wasn't sure if this is
the best way:

Type type = this.currentSelectedNode.Tag.GetType();

if (type.Name = "Group")
....
else
if (type.Name = "GroupMember")
....

Thanks in advance
dh
 
D

D. Yates

Doug,

Perhaps I'm missing something here, but if Group and GroupMember are two
seperate classes or even dervied from the same base class and attached to
the Tag property, wouldn't it be better to use the "is" operator instead of
typeof?

As in:

// Group and GroupMember are the class names
if (this.currentSelectedNode.Tag is Group)
{

}
else if (this.currentSelectedNode.Tag is GroupMember)
{
}


Assuming they are classes, maybe one of the MVPs can address, which way is
faster (typeof call or using the is operator)?

Dave
 
M

MuZZy

D. Yates said:
Doug,

Perhaps I'm missing something here, but if Group and GroupMember are two
seperate classes or even dervied from the same base class and attached to
the Tag property, wouldn't it be better to use the "is" operator instead of
typeof?

As in:

// Group and GroupMember are the class names
if (this.currentSelectedNode.Tag is Group)
{

}
else if (this.currentSelectedNode.Tag is GroupMember)
{
}


Assuming they are classes, maybe one of the MVPs can address, which way is
faster (typeof call or using the is operator)?

Dave

I would say "is" is faster than typeof() - anyway it's my preference
over using "typeof" in comparing types.
 
B

Bruce Wood

Either way, there's a real advantage in using "== typeof(X)" or "is X"
instead of comparing with the name. In the OP's particular case it
doesn't matter, but in general there is the problem of two types from
different assemblies / namespaces with the same name. Simply comparing

if (x.GetType().Name == "X")

will not distinguish them. It will be true for _any_ type named "X"
from any namespace or any assembly, whereas

if (x.GetType() == typeof(X))

or

if (x is X)

will be true only if "x" is of the type to which the compiler resolves
the name "X".
 

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