GetType()

  • Thread starter Thread starter ron
  • Start date Start date
R

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.DropDownList")
{
DropDownList ddl = (DropDownList)ctl.FindControl
("ddlAddNewLocation");
if(ddl != null)
{
locationId = int.Parse(ddl.SelectedItem.Value);
}
}
}
 
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.WebControls.DropDownList)
{
DropDownList ddl = (DropDownList)ctl;
//do your work on the dropdown list
}
}
 
ron,

You should use this instead:

// Check to see if it is a drop down list.
if (ctl.GetType() == typeof(System.Web.UI.WebControls.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.
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Ron,

if(ctl.GetType() == typeof(System.Web.UI.WebControls.DropDownList)) { ... }

That way you can have the compiler check that
"System.Web.UI.WebControls.DropDownList" 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.DropDownList")
| {
| DropDownList ddl = (DropDownList)ctl.FindControl
| ("ddlAddNewLocation");
| if(ddl != null)
| {
| locationId = int.Parse(ddl.SelectedItem.Value);
| }
| }
| }


- --
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/oSQWwEwccQ4rWPgRAg4gAJ47gDw/foxCVP4OY61V0kmruyDL5gCfaNjO
akZ/PKyLrb7XRtkftQATDZ4=
=ZUEM
-----END PGP SIGNATURE-----
 
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.0000251
Using GetType() took 00:00:00.0002053

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.WebControls.DropDownList)
{
j++;
//Console.WriteLine("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLine("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLine("Using the is operator took " + sw.Elapsed);
sw.Start();
for (int i = 0; i < 1000; i++)
{
if (ctl.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
{
j++;
//Console.WriteLine("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLine("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLine("Using GetType() took " + sw.Elapsed);
Console.Read();
}

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




Nicholas Paldino said:
ron,

You should use this instead:

// Check to see if it is a drop down list.
if (ctl.GetType() == typeof(System.Web.UI.WebControls.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]
- (e-mail address removed)

ron said:
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.DropDownList")
{
DropDownList ddl = (DropDownList)ctl.FindControl
("ddlAddNewLocation");
if(ddl != null)
{
locationId = int.Parse(ddl.SelectedItem.Value);
}
}
}
 
Greg,

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

if ((ctl as System.Web.UI.WebControls.DropDownList) != null)
{
j++;
//Console.WriteLine("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLine("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]
- (e-mail address removed)


Greg Ewing said:
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.0000251
Using GetType() took 00:00:00.0002053

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.WebControls.DropDownList)
{
j++;
//Console.WriteLine("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLine("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLine("Using the is operator took " + sw.Elapsed);
sw.Start();
for (int i = 0; i < 1000; i++)
{
if (ctl.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
{
j++;
//Console.WriteLine("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLine("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLine("Using GetType() took " + sw.Elapsed);
Console.Read();
}

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




message news:[email protected]...
ron,

You should use this instead:

// Check to see if it is a drop down list.
if (ctl.GetType() == typeof(System.Web.UI.WebControls.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]
- (e-mail address removed)

ron said:
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.DropDownList")
{
DropDownList ddl = (DropDownList)ctl.FindControl
("ddlAddNewLocation");
if(ddl != null)
{
locationId = int.Parse(ddl.SelectedItem.Value);
}
}
}
 
-----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.0000251
| Using GetType() took 00:00:00.0002053
|
| 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.WebControls.DropDownList)
| {
| j++;
| //Console.WriteLine("ctl is a DropDownList");
| }
| else
| {
| j++;
| //Console.WriteLine("ctl is NOT a DropDownList");
| }
| }
| sw.Stop();
| Console.WriteLine("Using the is operator took " + sw.Elapsed);
| sw.Start();
| for (int i = 0; i < 1000; i++)
| {
| if (ctl.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
| {
| j++;
| //Console.WriteLine("ctl is a DropDownList");
| }
| else
| {
| j++;
| //Console.WriteLine("ctl is NOT a DropDownList");
| }
| }
| sw.Stop();
| Console.WriteLine("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/oTP4wEwccQ4rWPgRAnSWAJ9jRyA0E+qpCiLpcY8Ap9fty+DoJgCdFKKo
JeJ3RhBNh9zYoMmlUOdmXrw=
=U+E1
-----END PGP SIGNATURE-----
 
OK, here are the results with the as operator for you Nicholas.

Using the is operator took 00:00:00.0000240
Using GetType() took 00:00:00.0002363
Using the as operator took 00:00:00.0002394

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 said:
Greg,

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

if ((ctl as System.Web.UI.WebControls.DropDownList) != null)
{
j++;
//Console.WriteLine("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLine("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]
- (e-mail address removed)


Greg Ewing said:
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.0000251
Using GetType() took 00:00:00.0002053

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.WebControls.DropDownList)
{
j++;
//Console.WriteLine("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLine("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLine("Using the is operator took " + sw.Elapsed);
sw.Start();
for (int i = 0; i < 1000; i++)
{
if (ctl.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
{
j++;
//Console.WriteLine("ctl is a DropDownList");
}
else
{
j++;
//Console.WriteLine("ctl is NOT a DropDownList");
}
}
sw.Stop();
Console.WriteLine("Using GetType() took " + sw.Elapsed);
Console.Read();
}

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




message news:[email protected]...
ron,

You should use this instead:

// Check to see if it is a drop down list.
if (ctl.GetType() == typeof(System.Web.UI.WebControls.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]
- (e-mail address removed)

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.DropDownList")
{
DropDownList ddl = (DropDownList)ctl.FindControl
("ddlAddNewLocation");
if(ddl != null)
{
locationId = int.Parse(ddl.SelectedItem.Value);
}
}
}
 
Back
Top