473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 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 1061
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
17
by: atgraham | last post by:
Here is the "lead C# architect" attempting to impugn C++ templates (bottom of the page). http://www.artima.com/intv/generics2.html (bottom of the page) (Full article begins at...
4
by: Gazarsgo | last post by:
This seems to be a bit of a contradiction, but how can I construct a Generic class using a System.Type variable that is assigned at run time? Is there some parallel to the Generics concept that...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
3
by: psyCK0 | last post by:
Hi all! I have a problem of casting generics to their base type. In the code below I first define a BaseList class that can hold items of any type that inherits from BaseItem. I then define a...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have some problems with Crystal Reports (Version 10.2, Runtime 2.0). In Section3 I have added a OLE Object (Bitmap). Now when I open the report in my code I would like to set this...
3
by: Anders Borum | last post by:
Hello, I've worked on an API for quite some time and have (on several occasions) tried to introduce generics at the core abstract level of business objects (especially a hierarchical node). The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.