473,387 Members | 1,834 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,387 software developers and data experts.

Generics question

Hi there

I have a question about generics in C# 2.0.

I have a generic class, Foo of T, which defines a field of type T. This
class is overridden in several base classes which define explicit types (say,
one does this with int, and another with string).

I now want to create a collection class to hold these Foo objects,
presumably inheriting from List<T>, which will be strongly typed. Inside the
collection, I want to define a new method which returns the type that the
field in the Foo is defined as.

I've written this as actual code below for clarity. The question I've got
is, how do I specify the return value of the method GetFirstValue so that it
is strongly typed - i.e. when the collection holds instances of IntFoo, this
method will return int; when the collection holds instances of StringFoo, it
will return string, and when it's holding Foo<T> it will return T?

Or does this particular scenario not make sense?

Thanks very much

- John

------------------
public abstract class Foo<T>
{
public T MyValue;
}

public class IntFoo : Foo<int>
{
}

public class StringFoo : Foo<string>
{
}

public class FooCollection<T> : List<T> where T : Foo<T>
{
// how can I specify the return value will be typeof(T.MyValue)?
public xxxxxxxx GetFirstValue()
{
return this[0].MyValue;
}
}
------------------
Nov 25 '05 #1
3 963
The question I've got
is, how do I specify the return value of the method GetFirstValue so that it
is strongly typed - i.e. when the collection holds instances of IntFoo, this
method will return int; when the collection holds instances of StringFoo, it
will return string, and when it's holding Foo<T> it will return T?

public T GetFirstValue()
{
return this[0].MyValue;
}

But note that you will not be able to store both StringFoos and
IntFoos in the same collection.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 25 '05 #2
Hi Mattias

Thanks for your help. I'm still a little vague on why that works though -
isn't T going to be set to either IntFoo or StringFoo? I'm wanting to get out
the value from the object, not the object itself.

One thing I discovered yesterday was to use two type parameters on the
class, such as

public class FooCollection<T, T2> : List<T> where T : Foo<T2>
{
public T2 GetFirstValue { ... }
}

This does actually work, but it just seems a bit messy to me. (It's like I
have to redeclare the nature of the Foos I'm storing in the collection in T2,
when T already implictly contains that information.)

If you have any further thoughts, I'd love to hear them.

Thanks very much

- John

"Mattias Sjögren" wrote:
The question I've got
is, how do I specify the return value of the method GetFirstValue so that it
is strongly typed - i.e. when the collection holds instances of IntFoo, this
method will return int; when the collection holds instances of StringFoo, it
will return string, and when it's holding Foo<T> it will return T?

public T GetFirstValue()
{
return this[0].MyValue;
}

But note that you will not be able to store both StringFoos and
IntFoos in the same collection.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 25 '05 #3
>Thanks for your help. I'm still a little vague on why that works though -
isn't T going to be set to either IntFoo or StringFoo?
Yep sorry I think I misread your original code.

One thing I discovered yesterday was to use two type parameters on the
class, such as

public class FooCollection<T, T2> : List<T> where T : Foo<T2>


Yeah if you want to keep storing IntFoos and StringFoos internally I
believe that's what you have to do.

But do they really provide any value beyond their base classes
Foo<int> and Foo<string>? If not, you could get rid of one of the type
parameters and store Foo<T>'s in the list instead, no?

public class FooCollection<T> : List<Foo<T>>
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 29 '05 #4

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

Similar topics

1
by: Steve Drake | last post by:
Hello, I may be mis understanding some of the uses of generics so this example may not be valid, but here goes. I have a method that I wont to be able to return a string or byte array, it...
5
by: Jeremy Gailor | last post by:
I'm currently just playing around with some AI stuff (working with breadth-first, depth-first searches (irrelevant)), but I'm writing a binary tree using the generics just to get a feel for them. ...
3
by: Ashish | last post by:
Iam wondering if it is possible to narrow down the type definition while specifying Generics, for example i want to create my collection class for a particular Type T, but i want this Type T to...
9
by: James Crosswell | last post by:
I'm not sure if I'm going about this the right way - it may be that Generics might be able to help me out here... but here goes: I have three classes as follows class BaseEdit class WidgetEdit:...
11
by: Olaf Krumnow | last post by:
Hi, I have a requirement that, I thought, was very simple, but in fact turned out as impossible for me. I read some posts here and the language specs from MS, but couldn't get a solution. ...
1
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... I'm a little perplexed by some of the generics collection stuff. Dictionary<Key, Valuebeing the rough equivalent of Hashtable, I was surprised to find there was no direct way to get to the...
2
by: =?Utf-8?B?QnJhdmVzQ2hhcm0=?= | last post by:
I am trying to convert a class I have to generics and I can't seem to find any possible why to implement it. I'm beginning to think I'm doing something I shouldn't or I hit generics limitation. ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.