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

lists of generics

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
10 1365
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
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
Because it's exaclty what you're running into here and it's not
supported by C# generics.

May 18 '06 #5
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
5
by: Andrew Ducker | last post by:
I have something I'm trying to make work with generics, and it seems like it should, but I can't quite get there. I have a subclass of List<Tthat I want to populate automatically. Each different...
5
by: Lonifasiko | last post by:
Hi, I'm having quite a strange behaviour when using Generics classes and ComboBox controls in Winforms applications. Hope somebody has seen the same behaviour. Two combobox controls in my...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
1
by: Jeff | last post by:
..NET 2.0 Is generic lists faster then tradional lists when sending over a collection of objects (value by reference) in .NET remoting. Lets say if a list of object should be sent from a...
1
by: colin | last post by:
Hi, I have 3 object of interest wich are objects in 3d space, surface,wire,point. they are all interconnected, and they all contain lists of objects they are connected to, eg each surface will...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.