473,410 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 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 1283
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zoury | last post by:
Good day! :O) I have the following code : //*** using System; using System.Windows.Forms; <snip>
3
by: david.kao | last post by:
I am trying to use Type.GetType(name of type in string format) to get an instance Type. For example: Type t = Type.GetType("System.Data.DataTable"). I have no trouble to get a value type For...
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
4
by: Greg Burns | last post by:
What the difference between these two? System.Type.GetType("System.Int32") and GetType(Integer) Or more specifically, why does GetType(Integer) work, but not System.Type.GetType(Integer)?...
4
by: Howard Kaikow | last post by:
For the code below, for both appWord and gappWord, I get the error "Public member 'GetType' on type 'ApplicationClass' not found" I realize the test for appWord is superflous as the parameter...
3
by: Joe Adams | last post by:
Hi All, How can I use GetType(<GenericType>).IsAssignableFrom(<MyType>) I need to now if the <MyType> is the same type of class as the <GenericType> without having to add the generic type...
5
by: Grande | last post by:
Hi all, I'm trying to use GetType to get an Interface, but it's not working? Is there a special way you have to do this, since it's an Interface (rather than a complete class)? Code: Type...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
4
by: Steph | last post by:
hello, i want to find the type from a generic... like : public static john<T>(object obj, T myControl) { ((myControl.getType())myControl).OnClientClick ="code"; } because my generic var can...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.