473,804 Members | 3,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GetType() question

h
Hi!

I have a user control called ListItemControl . This works ok:

ListItemControl li = new ListItemControl ();

When I iterate with Enumerator through all controls using:

IEnumerator e = pnlList.Control s.GetEnumerator ();
while (e.MoveNext())
{
object o = e.Current;
if(o.GetType(). Equals(GetType( ListItemControl )))
{
i++;
}
}

I get 'ListItemContro lTest.ListItemC ontrol' denotes a 'class' where
a 'variable' was expected
What do I do wrong? I looked at the MSDN sample and it looked pretty
straight forward, but I can't get it to work.

thanks,
h
Nov 16 '05 #1
6 6968
You should be using typeof() instead of GetType() or just use the is keyword.

if ( o is ListItemControl ) {
}
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"h" <my@address.com > wrote in message
news:6r******** *************** *********@4ax.c om...
Hi!

I have a user control called ListItemControl . This works ok:

ListItemControl li = new ListItemControl ();

When I iterate with Enumerator through all controls using:

IEnumerator e = pnlList.Control s.GetEnumerator ();
while (e.MoveNext())
{
object o = e.Current;
if(o.GetType(). Equals(GetType( ListItemControl )))
{
i++;
}
}

I get 'ListItemContro lTest.ListItemC ontrol' denotes a 'class' where
a 'variable' was expected
What do I do wrong? I looked at the MSDN sample and it looked pretty
straight forward, but I can't get it to work.

thanks,
h

Nov 16 '05 #2
You use GetType() to get the type from an object instance. To get a type
from a classname, use typeof():
so, instead of:
if(o.GetType(). Equals(GetType( ListItemControl ))) use
if(o.GetType(). Equals(typeof(L istItemControl) ))
// word of caution: I haven't tried to compile your code, but I think this
should solve that problem.

Oh, I'm curious: Why did you explicitly retrieve the enumerator instead of
just using a foreach() loop?
"h" <my@address.com > wrote in message
news:6r******** *************** *********@4ax.c om... Hi!

I have a user control called ListItemControl . This works ok:

ListItemControl li = new ListItemControl ();

When I iterate with Enumerator through all controls using:

IEnumerator e = pnlList.Control s.GetEnumerator ();
while (e.MoveNext())
{
object o = e.Current;
if(o.GetType(). Equals(GetType( ListItemControl )))
{
i++;
}
}

I get 'ListItemContro lTest.ListItemC ontrol' denotes a 'class' where
a 'variable' was expected
What do I do wrong? I looked at the MSDN sample and it looked pretty
straight forward, but I can't get it to work.

thanks,
h

Nov 16 '05 #3
Hi,

Use the if statement this way:
if( o is ListItemControl )
....

--
Rakesh Rajan
"h" wrote:
Hi!

I have a user control called ListItemControl . This works ok:

ListItemControl li = new ListItemControl ();

When I iterate with Enumerator through all controls using:

IEnumerator e = pnlList.Control s.GetEnumerator ();
while (e.MoveNext())
{
object o = e.Current;
if(o.GetType(). Equals(GetType( ListItemControl )))
{
i++;
}
}

I get 'ListItemContro lTest.ListItemC ontrol' denotes a 'class' where
a 'variable' was expected
What do I do wrong? I looked at the MSDN sample and it looked pretty
straight forward, but I can't get it to work.

thanks,
h

Nov 16 '05 #4
GetType() vs typeof(): Microsoft should have been more careful with
cross-language confusion between VB and C#.

Check
http://www.tangiblesoftwaresolutions...rsion_Tips.htm
for an interesting summary of this issue and a couple of others.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #5
h
On Fri, 11 Jun 2004 10:54:18 -0500, "J.Marsch"
<je****@ctcdeve loper.com> wrote:

Oh, I'm curious: Why did you explicitly retrieve the enumerator instead of
just using a foreach() loop?


I'm still getting used to C# (late transition from VB which I have
used for quite a while). I was looking for "better" or other way to do
this.

Can you give advice if this is preferable way or not?

thanks everyone for your replies!

h
Nov 16 '05 #6
The only reason that I asked is that the enumerator is usually something
that you don't touch directly. Instead, if the object that you want to loop
through supports IEnumerable, you generally use a foreach loop (which uses
the enumerator under the covers for your). The foreach lets you declare a
variable for the member type of the collection you are enumerating. The form
looks like this:

foreach(MemberT ype memberVar in CollectionToBeE numerated)
{
}

Basically, this uses the enumerator to loop through every member in the
collection. Each member is cast as MemberType and assigned to memberVar.

So, you could rewrite your code something like this (haven't tried
compiling, might need some tweaking):

foreach(Control aControl in pnlList.Control s)
{
if(aControl is ListItemControl )
i++;
}

NOTE: I am using Control because I know that all of the members of Controls
inherit from control. If I use a type that is too restrictive I will get
invalid cast errors. So, if I used TextBox, and there was a button in the
Controls collection, I would get an invalid cast exception when I hit the
button.

"h" <my@address.com > wrote in message
news:h5******** *************** *********@4ax.c om...
On Fri, 11 Jun 2004 10:54:18 -0500, "J.Marsch"
<je****@ctcdeve loper.com> wrote:

Oh, I'm curious: Why did you explicitly retrieve the enumerator instead ofjust using a foreach() loop?


I'm still getting used to C# (late transition from VB which I have
used for quite a while). I was looking for "better" or other way to do
this.

Can you give advice if this is preferable way or not?

thanks everyone for your replies!

h

Nov 16 '05 #7

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

Similar topics

2
3508
by: JohnnySparkles | last post by:
Hi everyone, I'm currently writing an application which uses the XmlSerializer class to serialize/deserialize objects to/from xml. Now when deserializing an XmlDocument back into the object, I'm using the System::Type::GetType(String* typeName) to create a Type* needed to construct the XmlSerializer. The typeName argument is taken from the XmlDocument and consists of the
6
19213
by: Jim Bancroft | last post by:
Hi everyone, I'm having some trouble with the code below. I receive a compile-time error on the second line saying "; expected": private static void myTestFunction(long myLong) { System.Data.SqlTypes.SqlInt32 myTestInt;
10
11802
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)
1
1372
by: Jennyfer Barco | last post by:
Hello I have a question. I have a project called "Primary" and in that project I have a reference to a Com application called "engine". In "engine" I have a function that does many things and calls other local functions for several purposes. In one of those functions I have the code: Dim myType As Type Application_AssemblyName = "Primary"
6
1464
by: Paul | last post by:
I have two projects in one solution. One is called Frontier and holds all my base user controls, classes, etc. that are used over multiple applications. The second is my application project (OCFU) which holds the forms and code that the users run. I need to instantiate a new form only given the form's name in a string. In the calling form in the OCFU project I used the following code: Dim objNewForm As Object =...
7
7821
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 together to check. What I have so far is, and would like as much feedback as possible to ensure I've...
3
2522
by: Ron M. Newman | last post by:
Hi, I have a certain assembly that needs to create instances of objects where the object class is passed as a string (e.g. "System.Drawing.Bitmap"). I have found that calling "GetType" on this string, returns null. Naturally, I suspect that because the this assembly doesn't reference the assembly containing "System.Drawing.Bitmap", the type cannot be found and thus I'm getting null in the GetType.
4
9486
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 be a Button, LinkButton, ... with "OnClientClick " event. and i dont want use :
5
1461
by: TonyJ | last post by:
Hello!! This first expression return System.Int32. The type that is returned is a string. This is perfect understandable. Console.WriteLine(7.GetType().FullName); In this second expression is the same returned System.Int32. The type that is returned is System.RuntimeType Console.WriteLine(7.GetType());
0
9588
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.