473,703 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a List<> generic as a method return type

A simple code example is easier than trying to describe the issue. I
have:

public class myClassA
{
Aug 13 '08 #1
11 5007
pa*********@abb ott.com wrote:
A simple code example is easier than trying to describe the issue. I
have:

public class myClassA
{
.
.
.
}

public class myClassB {
.
.
public List<myClassAcl assBfunc()
{
.
.
.
}
.
.
}

When I attempt to compile, I get the error "} expected" on the line
containing the opening curly brace of classBfunc(). What am I doing
wrong? Is what I'm trying possible?
I get a different error: "Invalid token '.' in class, struct, or interface
member declaration".

Please post complete examples if you want compile errors diagnosed,
otherwise it's impossible for us to tell where the actual error might be.
What you want to do is possible and you're using the correct syntax, so it's
anyone's guess.

--
J.
Aug 13 '08 #2
public List<myClassAcl assBfunc<myClas sA>()
{}

The generic parameter needs to be passed to the function
<pa*********@ab bott.comwrote in message
news:2d******** *************** ***********@c65 g2000hsa.google groups.com...
>A simple code example is easier than trying to describe the issue. I
have:

public class myClassA
{
.
.
.
}

public class myClassB {
.
.
public List<myClassAcl assBfunc()
{
.
.
.
}
.
.
}

When I attempt to compile, I get the error "} expected" on the line
containing the opening curly brace of classBfunc(). What am I doing
wrong? Is what I'm trying possible?

TIA
Aug 13 '08 #3
Bill McCarthy wrote:
public List<myClassAcl assBfunc<myClas sA>()
{}

The generic parameter needs to be passed to the function
Good catch! This is the difference between

public List<myClassAcl assBfunc()

with

public List<myClassAcl assBfunc<myClas sA>()

The former is a non-generic method that returns a particular instantiation
of List with the type "myClassA". The latter is a generic method that
returns an instantiation of List using the type parameter "myClassA".

If "myClassA" is not defined, the first one is an error. So if "myClassA" is
a concrete type, the OP's probably missing an assembly reference.

--
J.
Aug 13 '08 #4

"Jeroen Mostert" <jm******@xs4al l.nlwrote in message
news:48******** *************@n ews.xs4all.nl.. .
Bill McCarthy wrote:
> public List<myClassAcl assBfunc<myClas sA>()
{}

The generic parameter needs to be passed to the function
Good catch! This is the difference between

public List<myClassAcl assBfunc()

with

public List<myClassAcl assBfunc<myClas sA>()

The former is a non-generic method that returns a particular instantiation
of List with the type "myClassA". The latter is a generic method that
returns an instantiation of List using the type parameter "myClassA".

If "myClassA" is not defined, the first one is an error. So if "myClassA"
is a concrete type, the OP's probably missing an assembly reference.

Just looked at the original code again and he did define myCLassA, so the
intent is probably a concrete List, but in which case I can't see where the
error could be . The thread title threw me off ;)

Aug 13 '08 #5
Bill McCarthy <Bi**@localhost .comwrote:
public List<myClassAcl assBfunc<myClas sA>()
{}

The generic parameter needs to be passed to the function
Only when it's meant to be a generic method. There's nothing to
indicate that here. Put it this way - would you have seen anything
wrong if the OP hadn't posted myClassA at all, but had posted:

public class myClassB
{
public List<stringclas sBFunc()
{
...
}
}

?

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 13 '08 #6
Bill McCarthy wrote:
>
"Jeroen Mostert" <jm******@xs4al l.nlwrote in message
news:48******** *************@n ews.xs4all.nl.. .
>Bill McCarthy wrote:
>> public List<myClassAcl assBfunc<myClas sA>()
{}

The generic parameter needs to be passed to the function
Good catch! This is the difference between

public List<myClassAcl assBfunc()

with

public List<myClassAcl assBfunc<myClas sA>()

The former is a non-generic method that returns a particular
instantiatio n of List with the type "myClassA". The latter is a
generic method that returns an instantiation of List using the type
parameter "myClassA".

If "myClassA" is not defined, the first one is an error. So if
"myClassA" is a concrete type, the OP's probably missing an assembly
reference.


Just looked at the original code again and he did define myCLassA, so
the intent is probably a concrete List, but in which case I can't see
where the error could be .
The error is that the compiler can't find myClassA -- it gives exactly the
message reported by the OP in this case. His code isn't representative of
what he posted; most likely myClassA isn't in the same namespace as myClassB
and he forgot to add a reference.

Again, it all just goes to show you that minimal complete examples are not
optional in these cases...

--
J.
Aug 13 '08 #7
Jon Skeet [C# MVP] <sk***@pobox.co mwrote:

<snip>

Ah, the joys of simultaneous posting... I see you've now realised that
:)

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 13 '08 #8

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
Bill McCarthy <Bi**@localhost .comwrote:
> public List<myClassAcl assBfunc<myClas sA>()
{}

The generic parameter needs to be passed to the function

Only when it's meant to be a generic method. There's nothing to
indicate that here. Put it this way - would you have seen anything
wrong if the OP hadn't posted myClassA at all, but had posted:

public class myClassB
{
public List<stringclas sBFunc()
{
...
}
}

?
Yep the ... and ? <g Actually I think the problem with his code is
probably a namespace before the classes and not the closing } on the
namespace. The title of the thread and the function code/return type
probably aren't the issue at all ;)

Aug 13 '08 #9
Jeroen Mostert wrote:
Bill McCarthy wrote:
>>
"Jeroen Mostert" <jm******@xs4al l.nlwrote in message
news:48******* **************@ news.xs4all.nl. ..
>>Bill McCarthy wrote:
public List<myClassAcl assBfunc<myClas sA>()
{}

The generic parameter needs to be passed to the function

Good catch! This is the difference between

public List<myClassAcl assBfunc()

with

public List<myClassAcl assBfunc<myClas sA>()

The former is a non-generic method that returns a particular
instantiati on of List with the type "myClassA". The latter is a
generic method that returns an instantiation of List using the type
parameter "myClassA".

If "myClassA" is not defined, the first one is an error. So if
"myClassA" is a concrete type, the OP's probably missing an assembly
reference.


Just looked at the original code again and he did define myCLassA, so
the intent is probably a concrete List, but in which case I can't see
where the error could be .

The error is that the compiler can't find myClassA -- it gives exactly
the message reported by the OP in this case. His code isn't
representative of what he posted; most likely myClassA isn't in the same
namespace as myClassB and he forgot to add a reference.

Again, it all just goes to show you that minimal complete examples are
not optional in these cases...
Hmm - the OP never did come back with a complete example, so it seems like you
may have shown the reverse, actually . . .

-rick-
Aug 14 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
52981
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
5
1921
by: PJ | last post by:
I have a class definition : public class PagingList<T> : List<T> { private int pageSize, pageNumber; public PagingList() { pageSize = (this.Count == 0) ? 1 : this.Count; pageNumber = 1; }
6
13912
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an instance for the copy constructor). For example: C++ vector<classvClass;
0
1749
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse traversal and natively supports sorting, i.e., STL's list<T>. There isn't even a set of algorithms that would allow me to easily sort a generic collection. System.Array has a robust set of static algorithms, why not extend this to ICollection?
7
57551
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
44
39169
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume List<is far better. So, why do people use ArrayList so often? Am I missing somehing? What's the difference between them? Zytan
45
18877
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
2
6155
by: Fred Mellender | last post by:
I am trying to use reflection to output the fields (names and values) of an arbitrary object -- an object dump to a TreeView. It works pretty well, but I am having trouble with generic lists, like List<char>. What I have working is : Type type = newObj.GetType(); //newObj is what I'm trying to dump
6
2982
by: Peter | last post by:
I have a WebService which returns a List of RunningReport class How do I read this XML data on the client side. How do I convert List<RunningReportfrom the WebService side to List<RunningReporton the client side I have tried the following: List<RunningReportreportList = null; localhost.ReportService localrs = new localhost.ReportService();
0
9251
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9014
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7861
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2063
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.