473,811 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parametize the "is" keyword

Hi,
I understand how to use the "is" keyword straight forward.
like: if (obj is MyClass1)

However, I would like to parametize the "is"'s argument. Basically, I
would to replace MyClass1 with an argument. Here is an example:

private void foo(WhatType arg)
{
foreach (Control uc in _displayPanel.C ontrols)
{
if (uc is arg)
uc.Visible = true;
else
uc.Visible = false;
}
}

And call foo like...

foo(MyClass1);

My trouble is, I don't know what is the "arg"'s type? i.e. what
should "WhatType" be?
Is this is even possible?
Thanks for any pointer.
Minh (MCP)

Jan 30 '07 #1
6 1406
Perhaps you require a generic method, e.g.

private void foo<T>(T arg)
{
foreach (Control uc in _displayPanel.C ontrols)
{
if(uc is T)
uc.Visible = true;
else
uc.Visible = false;
}
}

Do some research on generics for more info.
On Jan 30, 4:42 pm, newcome...@yaho o.com wrote:
Hi,
I understand how to use the "is" keyword straight forward.
like: if (obj is MyClass1)

However, I would like to parametize the "is"'s argument. Basically, I
would to replace MyClass1 with an argument. Here is an example:

private void foo(WhatType arg)
{
foreach (Control uc in _displayPanel.C ontrols)
{
if (uc is arg)
uc.Visible = true;
else
uc.Visible = false;
}
}

And call foo like...

foo(MyClass1);

My trouble is, I don't know what is the "arg"'s type? i.e. what
should "WhatType" be?
Is this is even possible?
Thanks for any pointer.
Minh (MCP)
Jan 30 '07 #2
Hi,

You can use the typeof,
something like this

private void foo(Type arg)
{
foreach (Control uc in _displayPanel.C ontrols)
{
if (typeof(uc) is arg)
uc.Visible = true;
else
uc.Visible = false;
}
}
And you can use it:

Type theType = typeof(bar);
foo(theType);

_______________ ______________
http://elblogdehoracio.blogspot.com

On 30 ene, 17:42, newcome...@yaho o.com wrote:
Hi,
I understand how to use the "is" keyword straight forward.
like: if (obj is MyClass1)

However, I would like to parametize the "is"'s argument. Basically, I
would to replace MyClass1 with an argument. Here is an example:

private void foo(WhatType arg)
{
foreach (Control uc in _displayPanel.C ontrols)
{
if (uc is arg)
uc.Visible = true;
else
uc.Visible = false;
}
}

And call foo like...

foo(MyClass1);

My trouble is, I don't know what is the "arg"'s type? i.e. what
should "WhatType" be?
Is this is even possible?
Thanks for any pointer.
Minh (MCP)
Jan 30 '07 #3
I think typeof takes class name not an instance.
I did tried in foo
if (uc.GetType() == type)

However, in my case (ASP.NET UserControl), the GetType() returns
ASP.MyClass1 rather than just MyClass1. Hence, the comparison fail.

Terry, I will try the generic function version later...

On Jan 30, 11:55 am, "Horacio N. Hdez." <hnh12...@gmail .comwrote:
Hi,

You can use the typeof,
something like this

private void foo(Type arg)
{
foreach (Control uc in _displayPanel.C ontrols)
{
if (typeof(uc) is arg)
uc.Visible = true;
else
uc.Visible = false;
}
}And you can use it:

Type theType = typeof(bar);
foo(theType);

_______________ ______________h ttp://elblogdehoracio .blogspot.com

On 30 ene, 17:42, newcome...@yaho o.com wrote:
Hi,
I understand how to use the "is" keyword straight forward.
like: if (obj is MyClass1)
However, I would like to parametize the "is"'s argument. Basically, I
would to replace MyClass1 with an argument. Here is an example:
private void foo(WhatType arg)
{
foreach (Control uc in _displayPanel.C ontrols)
{
if (uc is arg)
uc.Visible = true;
else
uc.Visible = false;
}
}
And call foo like...
foo(MyClass1);
My trouble is, I don't know what is the "arg"'s type? i.e. what
should "WhatType" be?
Is this is even possible?
Thanks for any pointer.
Minh (MCP)- Hide quoted text -- Show quoted text -
Jan 30 '07 #4
<ne********@yah oo.comwrote:
I think typeof takes class name not an instance.
I did tried in foo
if (uc.GetType() == type)

However, in my case (ASP.NET UserControl), the GetType() returns
ASP.MyClass1 rather than just MyClass1. Hence, the comparison fail.
Have a look at Type.IsAssignab leFrom.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 30 '07 #5
On Jan 30, 2:36 pm, Jon Skeet [C# MVP] <s...@pobox.com wrote:
<newcome...@yah oo.comwrote:
I think typeof takes class name not an instance.
I did tried in foo
if (uc.GetType() == type)
However, in my case (ASP.NET UserControl), the GetType() returns
ASP.MyClass1 rather than just MyClass1. Hence, the comparison fail.

Have a look at Type.IsAssignab leFrom.
Yeah, I think that would work too...

Anyway, this seems to work too. Pass typeof(ASP.MyCl ass1) insteads of
typeof(MyClass1 )

Still would like to use the "is" keyword...:(

Jan 30 '07 #6
ne********@yaho o.com wrote:
Hi,
I understand how to use the "is" keyword straight forward.
like: if (obj is MyClass1)

However, I would like to parametize the "is"'s argument. Basically,
I would to replace MyClass1 with an argument. Here is an example:

private void foo(WhatType arg)
{
foreach (Control uc in _displayPanel.C ontrols)
{
if (uc is arg)
uc.Visible = true;
else
uc.Visible = false;
}
}

And call foo like...

foo(MyClass1);

My trouble is, I don't know what is the "arg"'s type? i.e. what
should "WhatType" be?

private void foo(Type arg)
{
foreach (Control uc in _displayPanel.C ontrols)
{
uc.Visible=(uc. GetType()==arg) ;
}
}

FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jan 31 '07 #7

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

Similar topics

1
1924
by: Chumley the Walrus | last post by:
I am now all of a sudden getting an error in this sql connection string, saying that the Provider keyword is invalid: <add key="MM_CONNECTION_STRING_isox" value="Provider=SQLOLEDB;SERVER=xx.xx.xx.xx;UID=xx;PWD=xxx;DATABASE=xxxx;" />
8
3393
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
11
1170
by: anon | last post by:
Dear Microsoft: In your new ASP.NET 2.0 page model of being able to crosspost to another page, it would seem that if you were to type that word in Google, "crosspost" or "cross post", you will get endless amount of results from the newsgroup police complaining about posters cross posting to many newsgroups as this very message is doing. So to those at Microsoft, and since posting your form results to another page is very very...
134
9113
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
17
2319
by: nicolas.hilaire | last post by:
Hi all, i've read this article http://msdn2.microsoft.com/en-us/library/85af44e9.aspx who first interest me much. I've translated it to use generic instead of template : generic < typename T, typename U > Boolean isinst(U u) {
37
4012
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as a function
10
6135
by: Bishoy | last post by:
Hi, In VB.NET there is a keyword called "My" which has a lot of properties collected at it. Is there any equivalent to this "My" in C#? Thank you.
94
30386
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
81
3677
by: BlueJ774 | last post by:
Can someone please explain to me the difference between the "is" keyword and the == boolean operator. I can't figure it out on my own and I can't find any documentation on it. I can't understand why this works: if text is None: and why this always returns false:
0
10647
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
10386
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
10398
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
10133
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
9204
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...
1
7669
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
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
3865
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.