471,348 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 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 4816
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by PJ | last post: by
reply views Thread by Iron Moped | last post: by
7 posts views Thread by Andrew Robinson | last post: by
44 posts views Thread by Zytan | last post: by
2 posts views Thread by Fred Mellender | last post: by
6 posts views Thread by Peter | last post: by
1 post views Thread by Ronak mishra | 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.