471,325 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

generics compile error

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<TGetAllControls<T>(Control control)
{

List<Tcontrols = 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;
}
Jun 27 '08 #1
6 1015
parez wrote:
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<TGetAllControls<T>(Control control)
{

List<Tcontrols = 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);

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #2
On May 19, 2:10 pm, Lasse Vågsæther Karlsen <la...@vkarlsen.nowrote:
parez wrote:
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<TGetAllControls<T>(Control control)
{
List<Tcontrols = 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);

--
Lasse Vågsæther Karlsen
mailto:la...@vkarlsen.nohttp://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Thanks,

I think i had tried that too..

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

worked for me.

Jun 27 '08 #3
parez wrote:
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

--
http://barrkel.blogspot.com/
Jun 27 '08 #4
On Mon, 19 May 2008 11:23:03 -0700, parez <ps*****@gmail.comwrote:
[...]
private List<TGetAllControls<T>(Control control) where T:Control

worked for me.
And, as the compiler is suggesting, that is indeed a reasonable solution.

The most basic constraint you could get away with is "class". But if you
know that your method will only be used with inheritors of Control, the
constraint you've used would suffice as well (Control being a class).

Pete
Jun 27 '08 #5
On May 19, 2:29 pm, Barry Kelly <barry.j.ke...@gmail.comwrote:
parez wrote:
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

--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.
Jun 27 '08 #6
parez wrote:
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

--
http://barrkel.blogspot.com/
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by atgraham | last post: by
4 posts views Thread by Gazarsgo | last post: by
11 posts views Thread by hammad.awan_nospam | last post: by
8 posts views Thread by mark.norgate | last post: by
3 posts views Thread by psyCK0 | last post: by
13 posts views Thread by rkausch | last post: by
3 posts views Thread by =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.