parametize the "is" keyword

  • Thread starter Thread starter newcomer2k
  • Start date Start date
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)
 
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.
 
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...
 
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.
 
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...:(
 
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#)
------------------------------------------------------------------------
 
Back
Top