473,382 Members | 1,359 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,382 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 1059
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.