471,350 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

GetType Question - Best way to do this?

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
Dec 13 '05 #1
7 1229
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]
- mv*@spam.guard.caspershouse.com

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:uL****************@TK2MSFTNGP15.phx.gbl...
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

Dec 13 '05 #2
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" <dk*******@yahoo.com> wrote in message
news:uL****************@TK2MSFTNGP15.phx.gbl...
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

Dec 13 '05 #3
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ue**************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:uL****************@TK2MSFTNGP15.phx.gbl...
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


Dec 13 '05 #4
Got it - thanks!!!!

dh
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eK*************@TK2MSFTNGP12.phx.gbl...
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" <dk*******@yahoo.com> wrote in message
news:uL****************@TK2MSFTNGP15.phx.gbl...
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


Dec 13 '05 #5
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
Dec 13 '05 #6
D. Yates wrote:
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.
Dec 13 '05 #7
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".

Dec 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Matthew | last post: by
4 posts views Thread by Greg Burns | last post: by
4 posts views Thread by Howard Kaikow | last post: by
3 posts views Thread by Joe Adams | last post: by
5 posts views Thread by Grande | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.