generics compile error

P

parez

The type parameter 'T' cannot be used with the 'as' operator because
it does not have a class type constraint nor a 'class' constraint


I have the following code. and i get a compile error.

private List<T> GetAllControls<T>(Control control)
{

List<T> controls = new List<T>();


foreach (Control child in control.Controls)
{

if (child is T)
controls.Add(child as T); // ERROR HERE

if (child.HasChildren)
controls.AddRange(GetAllControls<T>(child));
}
return controls;
}
 
L

Lasse Vågsæther Karlsen

parez said:
The type parameter 'T' cannot be used with the 'as' operator because
it does not have a class type constraint nor a 'class' constraint


I have the following code. and i get a compile error.

private List<T> GetAllControls<T>(Control control)
{

List<T> controls = new List<T>();


foreach (Control child in control.Controls)
{

if (child is T)
controls.Add(child as T); // ERROR HERE

if (child.HasChildren)
controls.AddRange(GetAllControls<T>(child));
}
return controls;
}

The result of the "as" operator can be null, which obviously is not
legal for a value type.

As such, "as" can only be used on classes and since you don't have a
constraint on the generic method, the compiler complains.

Try using casting instead:

controls.Add((T)child);
 
P

parez

The result of the "as" operator can be null, which obviously is not
legal for a value type.

As such, "as" can only be used on classes and since you don't have a
constraint on the generic method, the compiler complains.

Try using casting instead:

controls.Add((T)child);

Thanks,

I think i had tried that too..

private List<T> GetAllControls<T>(Control control) where T:Control

worked for me.
 
B

Barry Kelly

parez said:
if (child is T)
controls.Add(child as T); // ERROR HERE

controls.Add((T) (object) child);

BTW, combining is and as is pointless and wasteful. 'as' returns null if
the value isn't of the expected type, but you've already verified with
'is' that it is in fact of the expected type. So you don't get any
different semantics than through using a direct cast, and the CLR ends
up doing an extra test (IIRC; I haven't examined the final JIT code for
it recently).

-- Barry
 
P

parez

controls.Add((T) (object) child);

BTW, combining is and as is pointless and wasteful. 'as' returns null if
the value isn't of the expected type, but you've already verified with
'is' that it is in fact of the expected type. So you don't get any
different semantics than through using a direct cast, and the CLR ends
up doing an extra test (IIRC; I haven't examined the final JIT code for
it recently).

-- Barry

--http://barrkel.blogspot.com/

Thanks.

I am just trying stuff to get it compiled. and i probably gonna keep
it after it worked. but now that you have pointed it out

i chanaged it to

T specificControl = child as T;
if (specificControl != null)
controls.Add(specificControl);


Thanks all.
 
B

Barry Kelly

parez said:
i chanaged it to

T specificControl = child as T;
if (specificControl != null)
controls.Add(specificControl);

Of course, with this change you need to constrain T to reference types,
with

'where T: class'

at the top of your declaration (or any other reference type; Control
will also do, as you noticed).

-- Barry
 

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

Similar Threads


Top