By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,195 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,195 IT Pros & Developers. It's quick & easy.

lists of generics

P: n/a
I created a class that accepts a type parameter. I then want to put all
of those into one list<>.

List<Parameter<object>> lp = new List<Parameter<object>>();

Parameter<string> licenceKey = new Parameter<string>();
licenceKey.Name = "LICENSEKEY";
licenceKey.Section = "General";
licenceKey.Value = "IVS"; //default 3 user key
lp.Add(licenceKey);

Parameter<bool> autoInsertDevice = new Parameter<bool>();
autoInsertDevice.Name = "AUTOINSERTDEVICE";
autoInsertDevice.Section = "General";
autoInsertDevice.Value = true;
lp.Add(licenceKey);

I thought by making the type in the list object the list would allow
objects that are typed derived from object to be inserted. I was wrong.
How can i fix this?
May 18 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Generics are strongly-typed at compile-time. They do not work like inherited
classes. If you create a Parameter<string>, it is a class of that type, and
not of a Generic type, which is not strictly a type at all, but more like a
Template.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:eO**************@TK2MSFTNGP04.phx.gbl...
I created a class that accepts a type parameter. I then want to put all of
those into one list<>.

List<Parameter<object>> lp = new List<Parameter<object>>();

Parameter<string> licenceKey = new Parameter<string>();
licenceKey.Name = "LICENSEKEY";
licenceKey.Section = "General";
licenceKey.Value = "IVS"; //default 3 user key
lp.Add(licenceKey);

Parameter<bool> autoInsertDevice = new Parameter<bool>();
autoInsertDevice.Name = "AUTOINSERTDEVICE";
autoInsertDevice.Section = "General";
autoInsertDevice.Value = true;
lp.Add(licenceKey);

I thought by making the type in the list object the list would allow
objects that are typed derived from object to be inserted. I was wrong.
How can i fix this?

May 18 '06 #3

P: n/a
sd********@gmail.com wrote:
http://www.boyet.com/Articles/CSharpCovarianceOne.html

that article timed out. I did go read stuff on covariance but i don't
understand how that helps me.
May 18 '06 #4

P: n/a
Because it's exaclty what you're running into here and it's not
supported by C# generics.

May 18 '06 #5

P: n/a
"Dan Holmes" <da*******@bigfoot.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP03.phx.gbl...

| that article timed out. I did go read stuff on covariance but i don't
| understand how that helps me.

If you have a class Customer derived from Person, then that is simple
inheritance.

class List<Person>
{
...
}

class DerivedList<Person> : List<Person>
{
...
}

this is inheritance of a generic type by another generic type.
class List<Person>
{
...
}

class List<Customer>
{
...
}

These are two separate classes, no inheritance, they are siblings, not
parent/child classes.

inheritance of generic types is inheritance of the *generic* type, not the
parameter to the generic type.

If you want to have a List<Person> that can hold instances of subtypes of
Person then you need to change your declaration to something like this :

class List<T> where T : Person
{
...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 18 '06 #6

P: n/a
Dan Holmes wrote:
I created a class that accepts a type parameter. I then want to put all
of those into one list<>.

List<Parameter<object>> lp = new List<Parameter<object>>();

Parameter<string> licenceKey = new Parameter<string>();
licenceKey.Name = "LICENSEKEY";
licenceKey.Section = "General";
licenceKey.Value = "IVS"; //default 3 user key
lp.Add(licenceKey);

Parameter<bool> autoInsertDevice = new Parameter<bool>();
autoInsertDevice.Name = "AUTOINSERTDEVICE";
autoInsertDevice.Section = "General";
autoInsertDevice.Value = true;
lp.Add(licenceKey);

I thought by making the type in the list object the list would allow
objects that are typed derived from object to be inserted. I was wrong.
How can i fix this?


The question is this:
Is a List<Dog> substitutable for List<Animal> where dog inherits animal?
The answer is no. (for .net 2.0)
Java 5.0 says yes(with restrictions).
Check out this excellent article for more info:
http://butunclebob.com/ArticleS.Uncl...ircleListShape
and
http://butunclebob.com/ArticleS.Uncl...undedWildcards

You could do it with common interfaces but that is a "hack" more often
than not and whats the common interface here? IComparable?

JB
May 19 '06 #7

P: n/a
How do i do that if the initial type of T is int, bool, string or float?
I really need this capability (the code in the original post) but i
can't figure a work-around for this limitation.

[Serializable]
public class Parameter<T>
{
private bool _readOnly;
private string _section, _name;
private T _value;
private List<T> _values;
//imagine public properties that have get and set for these
//private fields
}

I need to be able to have an array, list or something. I have thought
of an array of objects but that seems backwards. What are my options?

I will also have methods like this.

public List<Parameter<T>> ListParameters()
{
return null;
}

What can i replace List<Parameter<T>> with?

Joanna Carter [TeamB] wrote:
"Dan Holmes" <da*******@bigfoot.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP03.phx.gbl...

| that article timed out. I did go read stuff on covariance but i don't
| understand how that helps me.

If you have a class Customer derived from Person, then that is simple
inheritance.

class List<Person>
{
...
}

class DerivedList<Person> : List<Person>
{
...
}

this is inheritance of a generic type by another generic type.
class List<Person>
{
...
}

class List<Customer>
{
...
}

These are two separate classes, no inheritance, they are siblings, not
parent/child classes.

inheritance of generic types is inheritance of the *generic* type, not the
parameter to the generic type.

If you want to have a List<Person> that can hold instances of subtypes of
Person then you need to change your declaration to something like this :

class List<T> where T : Person
{
...
}

Joanna

May 19 '06 #8

P: n/a
I simplified the problem. I need this to work or a design alternative.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Parameter<object>[] p = new Parameter<object>[5];

p[0] = new Parameter<string>("asdf");
p[1] = new Parameter<int>(34);
p[2] = new Parameter<bool>(true);
p[3] = new Parameter<double>(2.3);

for (int i = 0; i < 5; i++)
System.Diagnostics.Debug.Print(i.ToString() + " " +
p[i].GetType);
}
internal class Parameter<T>
{
public Parameter(T value)
{
this.value = value;
}
public T value;
}

}
Dan Holmes wrote:
How do i do that if the initial type of T is int, bool, string or float?
I really need this capability (the code in the original post) but i
can't figure a work-around for this limitation.

[Serializable]
public class Parameter<T>
{
private bool _readOnly;
private string _section, _name;
private T _value;
private List<T> _values;
//imagine public properties that have get and set for these
//private fields
}

I need to be able to have an array, list or something. I have thought
of an array of objects but that seems backwards. What are my options?

I will also have methods like this.

public List<Parameter<T>> ListParameters()
{
return null;
}

What can i replace List<Parameter<T>> with?

May 19 '06 #9

P: n/a
One level of generics use to blinding to to the other level of generic
use.

So let's eliminate one:

ClassX licenceKey = new ClassX();
licenceKey.Name = "LICENSEKEY";
licenceKey.Section = "General";
licenceKey.Value = "IVS"; //default 3 user key
lp.Add(licenceKey);

ClassY autoInsertDevice = new ClassY();
autoInsertDevice.Name = "AUTOINSERTDEVICE";
autoInsertDevice.Section = "General";
autoInsertDevice.Value = true;
lp.Add(licenceKey);

Now, since you have two objects, one of type ClassX and other of the
unreleated ClassY, the simple rule of C# (unchanged since v1.0) is that
you need to use an ArrayList.

The fact the ClassX & ClassY were created from a generic is irrelevant
to the rest.

May 19 '06 #10

P: n/a
Dan Holmes <da*******@bigfoot.com> wrote in
news:#v**************@TK2MSFTNGP02.phx.gbl:
I simplified the problem. I need this to work or a design
alternative.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Parameter<object>[] p = new Parameter<object>[5];

p[0] = new Parameter<string>("asdf");
p[1] = new Parameter<int>(34);
p[2] = new Parameter<bool>(true);
p[3] = new Parameter<double>(2.3);

for (int i = 0; i < 5; i++)
System.Diagnostics.Debug.Print(i.ToString() + " " +
p[i].GetType);
}
internal class Parameter<T>
{
public Parameter(T value)
{
this.value = value;
}
public T value;
}

}
Dan Holmes wrote:
How do i do that if the initial type of T is int, bool, string or
float?
I really need this capability (the code in the original post) but i
can't figure a work-around for this limitation.

[Serializable]
public class Parameter<T>
{
private bool _readOnly;
private string _section, _name;
private T _value;
private List<T> _values;
//imagine public properties that have get and set for these

//private fields
}

I need to be able to have an array, list or something. I have
thought of an array of objects but that seems backwards. What are my
options?

I will also have methods like this.

public List<Parameter<T>> ListParameters()
{
return null;
}

What can i replace List<Parameter<T>> with?


Try something like:

public interface IParameter
{
Object Value { get; }
Type Type { get; }
}

public class Parameter<T> : IParameter
{
...
}

IParameter[] p = new IParameter[5];
p[0] = new Parameter<String>("asdf");
....
--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
May 22 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.