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.

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 4985
pa*********@abbott.com wrote:
A simple code example is easier than trying to describe the issue. I
have:

public class myClassA
{
.
.
.
}

public class myClassB {
.
.
public List<myClassAclassBfunc()
{
.
.
.
}
.
.
}

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<myClassAclassBfunc<myClassA>()
{}

The generic parameter needs to be passed to the function
<pa*********@abbott.comwrote in message
news:2d**********************************@c65g2000 hsa.googlegroups.com...
>A simple code example is easier than trying to describe the issue. I
have:

public class myClassA
{
.
.
.
}

public class myClassB {
.
.
public List<myClassAclassBfunc()
{
.
.
.
}
.
.
}

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<myClassAclassBfunc<myClassA>()
{}

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

public List<myClassAclassBfunc()

with

public List<myClassAclassBfunc<myClassA>()

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******@xs4all.nlwrote in message
news:48*********************@news.xs4all.nl...
Bill McCarthy wrote:
> public List<myClassAclassBfunc<myClassA>()
{}

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

public List<myClassAclassBfunc()

with

public List<myClassAclassBfunc<myClassA>()

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<myClassAclassBfunc<myClassA>()
{}

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<stringclassBFunc()
{
...
}
}

?

--
Jon Skeet - <sk***@pobox.com>
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******@xs4all.nlwrote in message
news:48*********************@news.xs4all.nl...
>Bill McCarthy wrote:
>> public List<myClassAclassBfunc<myClassA>()
{}

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

public List<myClassAclassBfunc()

with

public List<myClassAclassBfunc<myClassA>()

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 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.comwrote:

<snip>

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

--
Jon Skeet - <sk***@pobox.com>
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.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Bill McCarthy <Bi**@localhost.comwrote:
> public List<myClassAclassBfunc<myClassA>()
{}

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<stringclassBFunc()
{
...
}
}

?
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******@xs4all.nlwrote in message
news:48*********************@news.xs4all.nl...
>>Bill McCarthy wrote:
public List<myClassAclassBfunc<myClassA>()
{}

The generic parameter needs to be passed to the function

Good catch! This is the difference between

public List<myClassAclassBfunc()

with

public List<myClassAclassBfunc<myClassA>()

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 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
Rick Lones wrote:
Jeroen Mostert wrote:
<snip>
>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 . . .
If all you care about is the solution, yes. A *speedy* solution, on the
other hand... In this case we just got lucky that the problem was simple
enough to diagnose by exhaustion. Generally, trying to guess what someone's
code might be doing wrong if they don't show it is nigh impossible.

--
J.
Aug 14 '08 #11
Jeroen Mostert <jm******@xs4all.nlwrote:
If all you care about is the solution, yes. A *speedy* solution, on the
other hand... In this case we just got lucky that the problem was simple
enough to diagnose by exhaustion. Generally, trying to guess what someone's
code might be doing wrong if they don't show it is nigh impossible.
Although psychic debugging is always amusing:

http://blogs.msdn.com/ericlippert/ar...bugging/defaul
t.aspx

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

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

Similar topics

4
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>....
5
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;...
6
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...
0
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...
7
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...
44
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...
45
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...
2
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,...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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...
0
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,...
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.