|
I want to make a List and add derived classes to this list.
I see here that this is not possible by design in .NET: http://msdn.microsoft.com/en-us/library/aa479859.aspx#fundamentals_topic12
So what is the best practice solution to this? I guess I can...
Started by Alex on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
What the article links....
In the list (without casting) there are no problems with adding a derived type to a collection of base retrieve the elements from the list, and use the basic rules of polymorphism for changing the types types.
|
|
Look at the following example (partially taken from MSDN Blog ):
class Animal { } class Giraffe : Animal { } static void Main(string[] args) { // Array assignment works, but... Animal[] animals = new Giraffe[10]; // implicit... List<Animal> animalsList...
Started by AndiDog on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
In .NET 2.0, you can take advantage of array covariance .
Situation in C# 2, do you need to maintain one list, or would you be happy creating a new list list, but it is a "workaround" so to speak.
|
|
When we can inherit from base class / interface, why can't we declare a List<> using same classes / interface
interface A { } class B : A { } class C : B { } class Test { static void Main(string[] args) { A a = new C(); // OK List<A> listOfA...
Started by Asad Butt on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Your question then is "why can I not assign a list of giraffes to a variable of type list ....
This can be done using ConvertAll:
List< where the relationships are clear .
This work is to iterate over the list and cast the elements.
|
Ask your Facebook Friends
|
Why does this work:
public IList<ICoupon> GetCouponsForSite(string siteSlug) { var coupons = _db.Coupons.Where(x => x.Site.slug == siteSlug) .Select(x => new Coupon(x.id)); var list = new List<ICoupon>(); foreach (var coupon in coupons...
Started by Martin on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Now if the receiver decides to poke SomeEvilTypeThatImplementsICoupon into it, all is well, because the List was built....
Because db.Coupons...ToList into that list.
IQueryable<ICoupon> is not derived from IList<ICoupon> .
|
|
How can I do the follwing:
public class BaseItem { public string Title { get; set; } } public class DerivedItem : BaseItem { public string Description { get; set; } } class Program { static void Main(string[] args) { List<BaseItem> baseList = new...
Started by Henk on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
For now, you could (in .NET 3.5) use:
baseList.AddRange(derivedList.Cast<BaseItem>());
(note that you'll need " using System.Linq... .
However, this would probably work OK in C# 4.0/.NET 4.0 .
In C# 3.0/.NET 3.5, IEnumerable<T> is not covariant.
|
|
What I want to do is this:
class Ba { } class Da : public Ba {} class Db : public Ba {} class Bb // abstract base class that must not be a template. { void Process() { list<Ba*>::iterator pos; // I known the derived class will also derive // from...
Started by BCS on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
They don't have virtual members, there is no benefit; } template<T> class L : public ... .
To solve exceptions) are not meant to be derived from.
The list should contain since you want to determine the type in the derived class.
|
|
I have an interface list which stores a variety of objects derived from that interface. I would like to write a function where the caller specifies the type to extract. I've tried this:
List<IParts> PartList;
...
public List<IPart> Fetch(Type...
Started by tbischel on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Specify it as a type parameter instead:
public List<IPart> Fetch<T>() where T : IPart { return this.PartList.OfType<T>().Cast<IPart>().ToList(); }
You'd call it like this:
List<IPart> foo = parts.Fetch<Exhaust>();
That... .
|
|
Hi;
I feel my question is pretty dumb, or another way to put it is : I'm too lost in my code to see a workaround for now. Stay too long on a problem, and your vision becomes narrower and narrower ><. Plus I'm not good enough with inheritance, polymorphism...
Started by Chouppy on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
One trick I presented above - note that we are able to add a T to the list; useful in particular if we add the T : new a list) it would ....
FuncOnBase could attempt to add a Baseclass to the list, and the compiler wouldn't spot it.
|
|
This may seem kind of "homework-ish" and/or trivial, but it is for a real business purpose; it's just the easiest way I could think of to explain what I'm conceptually trying to do.
Suppose I have an Animal class, and some other classes (Bird, Cat, Kangaroo...
Started by Ron Lhufasen on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
If (animal is Bird.
Use the 'is' keyword.
In brittle coupling to a specific set of derived classes.
|
|
Here is some code outlining a problem I've been wrestling with. The final problem (as far as g++ is concerned at the moment) is that: "error: 'Foo-T' was not declared in this scope" when performing the Bar::Bar(...) constructor routine. Otherwise, the...
Started by Shamster on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Stated:
As of now I can't see a way past using both the template argument and a matching derived-class list because it's not in scsope until here Foo_T = TypeA(a_arg); // if an a_arg_t is passed foo_arg_t _foo_arg; }; // the derived....
|