parametize the "is" keyword

N

newcomer2k

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)
 
T

Terry Rogers

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.
 
H

Horacio N. Hdez.

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
 
N

newcomer2k

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...
 
J

Jon Skeet [C# MVP]

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.
 
N

newcomer2k

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...:(
 
F

Frans Bouma [C# MVP]

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#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top