By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,977 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,977 IT Pros & Developers. It's quick & easy.

parametize the "is" keyword

P: n/a
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.Controls)
{
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
Share this Question
Share on Google+
6 Replies


P: n/a
Perhaps you require a generic method, e.g.

private void foo<T>(T arg)
{
foreach (Control uc in _displayPanel.Controls)
{
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...@yahoo.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.Controls)
{
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

P: n/a
Hi,

You can use the typeof,
something like this

private void foo(Type arg)
{
foreach (Control uc in _displayPanel.Controls)
{
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...@yahoo.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.Controls)
{
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

P: n/a
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.Controls)
{
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...@yahoo.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.Controls)
{
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

P: n/a
<ne********@yahoo.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.IsAssignableFrom.

--
Jon Skeet - <sk***@pobox.com>
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

P: n/a
On Jan 30, 2:36 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
<newcome...@yahoo.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.IsAssignableFrom.
Yeah, I think that would work too...

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

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

Jan 30 '07 #6

P: n/a
ne********@yahoo.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.Controls)
{
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.Controls)
{
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 discussion thread is closed

Replies have been disabled for this discussion.