generics compile error

  • Thread starter Thread starter parez
  • Start date Start date
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;
}
 
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);
 
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.
 
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
 
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.
 
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
 
Back
Top