473,549 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GetType()

ron
Hi,
I am currently using the base class GetType() in the
following way and was woundering if there was a different
way of looking at types and doing a comparison for
equality.

Thanks Ron

foreach(System. Web.UI.Control ctl in this.Controls)
{
if(ctl.GetType( ).ToString()
== "System.Web.UI. WebControls.Dro pDownList")
{
DropDownList ddl = (DropDownList)c tl.FindControl
("ddlAddNewLoca tion");
if(ddl != null)
{
locationId = int.Parse(ddl.S electedItem.Val ue);
}
}
}
Nov 15 '05 #1
8 46040
ron, you have a couple of options. To keep a similar flow you could use the
typeof() operator instead of .GetType().

I would recommend you check out the is keyword though. With is you can do
something like this:

foreach (...)
{
if (ctl is System.Web.UI.W ebControls.Drop DownList)
{
DropDownList ddl = (DropDownList)c tl;
//do your work on the dropdown list
}
}

--
Greg Ewing [MVP]
http://www.citidc.com/
"ron" <an*******@disc ussions.microso ft.com> wrote in message
news:0a******** *************** *****@phx.gbl.. .
Hi,
I am currently using the base class GetType() in the
following way and was woundering if there was a different
way of looking at types and doing a comparison for
equality.

Thanks Ron

foreach(System. Web.UI.Control ctl in this.Controls)
{
if(ctl.GetType( ).ToString()
== "System.Web.UI. WebControls.Dro pDownList")
{
DropDownList ddl = (DropDownList)c tl.FindControl
("ddlAddNewLoca tion");
if(ddl != null)
{
locationId = int.Parse(ddl.S electedItem.Val ue);
}
}
}

Nov 15 '05 #2
ron,

You should use this instead:

// Check to see if it is a drop down list.
if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))

The typeof operator will get the type defined in parenthesis for you.
This is good because it doesn't force you to have the full name of the type
(version, etc, etc).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"ron" <an*******@disc ussions.microso ft.com> wrote in message
news:0a******** *************** *****@phx.gbl.. .
Hi,
I am currently using the base class GetType() in the
following way and was woundering if there was a different
way of looking at types and doing a comparison for
equality.

Thanks Ron

foreach(System. Web.UI.Control ctl in this.Controls)
{
if(ctl.GetType( ).ToString()
== "System.Web.UI. WebControls.Dro pDownList")
{
DropDownList ddl = (DropDownList)c tl.FindControl
("ddlAddNewLoca tion");
if(ddl != null)
{
locationId = int.Parse(ddl.S electedItem.Val ue);
}
}
}

Nov 15 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Ron,

if(ctl.GetType( ) == typeof(System.W eb.UI.WebContro ls.DropDownList )) { ... }

That way you can have the compiler check that
"System.Web.UI. WebControls.Dro pDownList" indeed exist, instead of having
to make sure that you've typed it correctly :)

ron wrote:

| Hi,
| I am currently using the base class GetType() in the
| following way and was woundering if there was a different
| way of looking at types and doing a comparison for
| equality.
|
| Thanks Ron
|
| foreach(System. Web.UI.Control ctl in this.Controls)
| {
| if(ctl.GetType( ).ToString()
| == "System.Web.UI. WebControls.Dro pDownList")
| {
| DropDownList ddl = (DropDownList)c tl.FindControl
| ("ddlAddNewLoca tion");
| if(ddl != null)
| {
| locationId = int.Parse(ddl.S electedItem.Val ue);
| }
| }
| }
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/oSQWwEwccQ4rWPg RAg4gAJ47gDw/foxCVP4OY61V0km ruyDL5gCfaNjO
akZ/PKyLrb7XRtkftQA TDZ4=
=ZUEM
-----END PGP SIGNATURE-----

Nov 15 '05 #4
ron
Thanks that clears things up better.
Ron
-----Original Message-----
Hi,
I am currently using the base class GetType() in the
following way and was woundering if there was a differentway of looking at types and doing a comparison for
equality.

Thanks Ron

foreach(System .Web.UI.Control ctl in this.Controls)
{
if(ctl.GetType( ).ToString()
== "System.Web.UI. WebControls.Dro pDownList")
{
DropDownList ddl = (DropDownList)c tl.FindControl
("ddlAddNewLoc ation");
if(ddl != null)
{
locationId = int.Parse(ddl.S electedItem.Val ue);
}
}
}
.

Nov 15 '05 #5
So Nicholas and Ray, what do you guys have against the is operator? <g> is
seems to be a whole lot faster than GetType and typeof(). Here's what I
found for 1000 iterations:

Using the is operator took 00:00:00.000025 1
Using GetType() took 00:00:00.000205 3

Here's the code I used. It is using Whidbey so that might affect things but
I soft of doubt it would make GetType 10 times slower like I saw.

[STAThread]
static void Main(string[] args)
{
Control ctl = new DropDownList();
Stopwatch sw = new Stopwatch();
int j = 0;
sw.Start();
for (int i = 0; i < 1000; i++)
{
if (ctl is System.Web.UI.W ebControls.Drop DownList)
{
j++;
//Console.WriteLi ne("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLi ne("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLi ne("Using the is operator took " + sw.Elapsed);
sw.Start();
for (int i = 0; i < 1000; i++)
{
if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
{
j++;
//Console.WriteLi ne("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLi ne("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLi ne("Using GetType() took " + sw.Elapsed);
Console.Read();
}

--
Greg Ewing [MVP]
http://www.citidc.com/


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:eq******** ******@TK2MSFTN GP11.phx.gbl...
ron,

You should use this instead:

// Check to see if it is a drop down list.
if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))

The typeof operator will get the type defined in parenthesis for you.
This is good because it doesn't force you to have the full name of the type (version, etc, etc).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"ron" <an*******@disc ussions.microso ft.com> wrote in message
news:0a******** *************** *****@phx.gbl.. .
Hi,
I am currently using the base class GetType() in the
following way and was woundering if there was a different
way of looking at types and doing a comparison for
equality.

Thanks Ron

foreach(System. Web.UI.Control ctl in this.Controls)
{
if(ctl.GetType( ).ToString()
== "System.Web.UI. WebControls.Dro pDownList")
{
DropDownList ddl = (DropDownList)c tl.FindControl
("ddlAddNewLoca tion");
if(ddl != null)
{
locationId = int.Parse(ddl.S electedItem.Val ue);
}
}
}


Nov 15 '05 #6
Greg,

Since you have so much time on your hands, perhaps you should test the
following as well:

if ((ctl as System.Web.UI.W ebControls.Drop DownList) != null)
{
j++;
//Console.WriteLi ne("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLi ne("ctl is NOT a DropDownList");
}

<g>

Also, you might want to provide numbers using VS.NET 2003, as Whidbey is
not available to the general public =)

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Greg Ewing [MVP]" <gewing@_NO_SPA M_citidc.com> wrote in message
news:OO******** ******@tk2msftn gp13.phx.gbl...
So Nicholas and Ray, what do you guys have against the is operator? <g> is seems to be a whole lot faster than GetType and typeof(). Here's what I
found for 1000 iterations:

Using the is operator took 00:00:00.000025 1
Using GetType() took 00:00:00.000205 3

Here's the code I used. It is using Whidbey so that might affect things but I soft of doubt it would make GetType 10 times slower like I saw.

[STAThread]
static void Main(string[] args)
{
Control ctl = new DropDownList();
Stopwatch sw = new Stopwatch();
int j = 0;
sw.Start();
for (int i = 0; i < 1000; i++)
{
if (ctl is System.Web.UI.W ebControls.Drop DownList)
{
j++;
//Console.WriteLi ne("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLi ne("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLi ne("Using the is operator took " + sw.Elapsed);
sw.Start();
for (int i = 0; i < 1000; i++)
{
if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
{
j++;
//Console.WriteLi ne("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLi ne("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLi ne("Using GetType() took " + sw.Elapsed);
Console.Read();
}

--
Greg Ewing [MVP]
http://www.citidc.com/


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message news:eq******** ******@TK2MSFTN GP11.phx.gbl...
ron,

You should use this instead:

// Check to see if it is a drop down list.
if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))

The typeof operator will get the type defined in parenthesis for you. This is good because it doesn't force you to have the full name of the

type
(version, etc, etc).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"ron" <an*******@disc ussions.microso ft.com> wrote in message
news:0a******** *************** *****@phx.gbl.. .
Hi,
I am currently using the base class GetType() in the
following way and was woundering if there was a different
way of looking at types and doing a comparison for
equality.

Thanks Ron

foreach(System. Web.UI.Control ctl in this.Controls)
{
if(ctl.GetType( ).ToString()
== "System.Web.UI. WebControls.Dro pDownList")
{
DropDownList ddl = (DropDownList)c tl.FindControl
("ddlAddNewLoca tion");
if(ddl != null)
{
locationId = int.Parse(ddl.S electedItem.Val ue);
}
}
}



Nov 15 '05 #7
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I thought he wanted the type to be exact, since I was just translating
his first line into how I would do it. But after seeing your reply, I
read Ron's code further down, and I realized that:

1. Ron apparently just wanted to check if he could cast it to a DropDownList
2. The DropDownList is already at the bottom of the hierarchy, so "is"
will give him a true only if the instance is indeed a DropDownList anyway...

So yeah, "is" does satisfy the requirements, and much more concise to
type :)

About the performance thing (where did you get this StopWatch class?
Seems quite useful), how about putting the typeof() outside the loop?
Guess that'll make a difference! :)

Greg Ewing [MVP] wrote:
| So Nicholas and Ray, what do you guys have against the is operator?
<g> is
| seems to be a whole lot faster than GetType and typeof(). Here's what I
| found for 1000 iterations:
|
| Using the is operator took 00:00:00.000025 1
| Using GetType() took 00:00:00.000205 3
|
| Here's the code I used. It is using Whidbey so that might affect
things but
| I soft of doubt it would make GetType 10 times slower like I saw.
|
| [STAThread]
| static void Main(string[] args)
| {
| Control ctl = new DropDownList();
| Stopwatch sw = new Stopwatch();
| int j = 0;
| sw.Start();
| for (int i = 0; i < 1000; i++)
| {
| if (ctl is System.Web.UI.W ebControls.Drop DownList)
| {
| j++;
| //Console.WriteLi ne("ctl is a DropDownList");
| }
| else
| {
| j++;
| //Console.WriteLi ne("ctl is NOT a DropDownList");
| }
| }
| sw.Stop();
| Console.WriteLi ne("Using the is operator took " + sw.Elapsed);
| sw.Start();
| for (int i = 0; i < 1000; i++)
| {
| if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
| {
| j++;
| //Console.WriteLi ne("ctl is a DropDownList");
| }
| else
| {
| j++;
| //Console.WriteLi ne("ctl is NOT a DropDownList");
| }
| }
| sw.Stop();
| Console.WriteLi ne("Using GetType() took " + sw.Elapsed);
| Console.Read();
| }
|
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/oTP4wEwccQ4rWPg RAnSWAJ9jRyA0E+ qpCiLpcY8Ap9fty +DoJgCdFKKo
JeJ3RhBNh9zYoMm lUOdmXrw=
=U+E1
-----END PGP SIGNATURE-----

Nov 15 '05 #8
OK, here are the results with the as operator for you Nicholas.

Using the is operator took 00:00:00.000024 0
Using GetType() took 00:00:00.000236 3
Using the as operator took 00:00:00.000239 4

Why not VS.Net 2003? Cause I'm lazy and Whidbey is the only thing I have on
my laptop. <g> I expect that the only difference is that there is no
stopwatch class on 2003. I don't think the type checking will be any
different. I'll check out VS.Net 2003 tonight if I have time. I've
attached the code if someone else wants to make it work for VS.Net 2003
before then.

--
Greg Ewing [MVP]
http://www.citidc.com/
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:#2******** ******@TK2MSFTN GP12.phx.gbl...
Greg,

Since you have so much time on your hands, perhaps you should test the
following as well:

if ((ctl as System.Web.UI.W ebControls.Drop DownList) != null)
{
j++;
//Console.WriteLi ne("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLi ne("ctl is NOT a DropDownList");
}

<g>

Also, you might want to provide numbers using VS.NET 2003, as Whidbey is not available to the general public =)

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Greg Ewing [MVP]" <gewing@_NO_SPA M_citidc.com> wrote in message
news:OO******** ******@tk2msftn gp13.phx.gbl...
So Nicholas and Ray, what do you guys have against the is operator? <g>

is
seems to be a whole lot faster than GetType and typeof(). Here's what I
found for 1000 iterations:

Using the is operator took 00:00:00.000025 1
Using GetType() took 00:00:00.000205 3

Here's the code I used. It is using Whidbey so that might affect things

but
I soft of doubt it would make GetType 10 times slower like I saw.

[STAThread]
static void Main(string[] args)
{
Control ctl = new DropDownList();
Stopwatch sw = new Stopwatch();
int j = 0;
sw.Start();
for (int i = 0; i < 1000; i++)
{
if (ctl is System.Web.UI.W ebControls.Drop DownList)
{
j++;
//Console.WriteLi ne("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLi ne("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLi ne("Using the is operator took " + sw.Elapsed);
sw.Start();
for (int i = 0; i < 1000; i++)
{
if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
{
j++;
//Console.WriteLi ne("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLi ne("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLi ne("Using GetType() took " + sw.Elapsed);
Console.Read();
}

--
Greg Ewing [MVP]
http://www.citidc.com/


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote

in
message news:eq******** ******@TK2MSFTN GP11.phx.gbl...
ron,

You should use this instead:

// Check to see if it is a drop down list.
if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))

The typeof operator will get the type defined in parenthesis for you. This is good because it doesn't force you to have the full name of the

type
(version, etc, etc).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"ron" <an*******@disc ussions.microso ft.com> wrote in message
news:0a******** *************** *****@phx.gbl.. .
> Hi,
> I am currently using the base class GetType() in the
> following way and was woundering if there was a different
> way of looking at types and doing a comparison for
> equality.
>
> Thanks Ron
>
> foreach(System. Web.UI.Control ctl in this.Controls)
> {
> if(ctl.GetType( ).ToString()
> == "System.Web.UI. WebControls.Dro pDownList")
> {
> DropDownList ddl = (DropDownList)c tl.FindControl
> ("ddlAddNewLoca tion");
> if(ddl != null)
> {
> locationId = int.Parse(ddl.S electedItem.Val ue);
> }
> }
> }





Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
7991
by: Yan Vinogradov | last post by:
Hi, Turns out it's possible to spoof another type with Object.GetType method. If you do this: namespace N { class C { public new Type GetType() { return (String.Empty.GetType()); }
10
11765
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal Dictionary as IDictionary) If Dictionary is Nothing Then Return Dim o as Object = DirectCast(Dictionary, Object)
5
2257
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 = "otherText" xmlSave("C:\folder\file.xml", mySettings) Here is the sub: Public Shared Sub xmlSave(ByVal path As String, ByVal config As
4
15100
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)? What namespace is the GetType function(?) part of?
4
2854
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 is passed in as a known type, but gappWord is has a scope of the class, so a test of the type is valid (making believe the sub does not know the...
3
4455
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 member members. i.e. I do not want to use GetType(<GenericType>(Of String).IsAssignableFrom(<MyType>)
3
2062
by: Erland | last post by:
Hi, I've been trying to load a type and and get all of the methods within that type. I finally made it happen by using typeof, but im really stumped how could one implement the same with Type.GetType(). I have read the documentation and it says that this looks currently executing assembly and mscorlib.dll. I have couple of questions in this...
7
7800
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}" As far as I know yet -- hence this question -- there is no 'one solution fits all', but instead there are several parts that have to be put...
2
2507
by: Carlos Rodriguez | last post by:
I have the following function in C#public void Undo(IDesignerHost host) { if (!this.componentName.Equals(string.Empty) && (this.member != null)) { IContainer container1 = (IContainer) host.GetService(typeof(IContainer)); IComponent component1 = container1.Components; PropertyInfo info1 = component1.GetType().GetProperty(this.member.Name);
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7450
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.