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

List to contain objects that derive from a generic base class

I have a generic base class and need a list to contain different types
of objects that derive from this.

Public class genericBase<Twhere T: ConfigBase{}

I have tried :
List<genericBase<ConfigBase>_objectTypes

but when I try to add to it using:
ObjectTypes.Add((ObjectType<ObjectConfigBase>)UI);

I get a type conversion exception. Any ideas?
Dec 12 '07 #1
2 1733
On Dec 12, 1:26 pm, alastair g <purplepango...@gmail.comwrote:
I have a generic base class and need a list to contain different types
of objects that derive from this.

Public class genericBase<Twhere T: ConfigBase{}

I have tried :
List<genericBase<ConfigBase>_objectTypes

but when I try to add to it using:
ObjectTypes.Add((ObjectType<ObjectConfigBase>)UI);

I get a type conversion exception. Any ideas?
Well, I know what's wrong - C# generics don't support covariance/
contravariance, and ObjectType<ObjectConfigBaseisn't convertible to
ObjectType<ConfigBase>.

The *answer* to it really depends on what you need to do, and why.

Jon
Dec 12 '07 #2
On Dec 12, 1:43 pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Dec 12, 1:26 pm, alastair g <purplepango...@gmail.comwrote:
I have a generic base class and need a list to contain different types
of objects that derive from this.
Public class genericBase<Twhere T: ConfigBase{}
I have tried :
List<genericBase<ConfigBase>_objectTypes
but when I try to add to it using:
ObjectTypes.Add((ObjectType<ObjectConfigBase>)UI);
I get a type conversion exception. Any ideas?

Well, I know what's wrong - C# generics don't support covariance/
contravariance, and ObjectType<ObjectConfigBaseisn't convertible to
ObjectType<ConfigBase>.

The *answer* to it really depends on what you need to do, and why.

Jon
ok, thanks. Need to rethink my approach then. Simplest would just be
to use an ArrayList and allow it to contain any object. I can always
validate what I put into it.
Dec 12 '07 #3

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>....
10
by: Andrew McLellan | last post by:
I think I must be missing something about generics, perhaps just about the syntax. I'd like to derive a class MyList from System.Collections.Generic so that it can only contain instance of MyItem....
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
9
by: Naren | last post by:
Hi, why cant a list<derived*be implicitly castable to list<base*>? Any alternatives other than global operators? thanks in advance, Naren.
5
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
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...
0
by: purplepangolin | last post by:
I have a generic base class and need a list to contain different types of objects that derive from this. Public class genericBase<T> where T: ConfigBase{} I have tried :...
7
by: DippyDog | last post by:
My problem is the subject line. Basically, my thinking was that, given class TestSpec1 inherits from class TestBase, since I can do this... Dim tst As TestBase = GetTestObj() Function...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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:
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...

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.