Avatar of brettr
brettr

asked on 

How to create this List<>?

I'd like to create a List<baseType> that can be looped through.  I'll call a method in the loop that can have different signatures.  Is there a way to do this?

List<baseType> myList = new List<baseType>();
myList.Add(someChild1);
myList.Add(someChild2);
 
foreach(myClass my in myClassList)
{
foreach(baseType b in myList)
{
  b.DoSomething(common1, common2, my.different1);
  b.DoSomething(common1, common2, my.different2);
  b.DoSomething(common1, common2, my.different3);
}
}
 
//////
abstract class baseType<T>
{
 public abstract void DoSomething(common1, common2, List<T>);
}
 
//////
class someChild1 : baseType<different1>
{
 public override void DoSomething(common1, common2, different1){...}
}
 
class someChild2 : baseType<different2>
{
 public override void DoSomething(common1, common2, different2){...}
}

Open in new window

.NET ProgrammingEditors IDEs

Avatar of undefined
Last Comment
brettr
Avatar of Hendo1973
Hendo1973
Flag of United States of America image

Use an interface to define the dosomething method and in your classes, implement that interface.

Then you can use a List.
Avatar of brettr
brettr

ASKER

I'm doing the same thing with the abstract class.  Doesn't make a difference here.  The problem is that you can't add different types to the same list.  Because each class's method (notice last parameter is different) will be different.  It's probably a design issue on my part and I need to rethink it.
SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Hendo1973
Hendo1973
Flag of United States of America image

jaime makes a good point.  If you use an interface and define your function like this:

DoSomething(common1Type, common2Type, object);

Then in the implementation, you can cast it to the appropriate type for each object.
ASKER CERTIFIED SOLUTION
Avatar of Hendo1973
Hendo1973
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of brettr
brettr

ASKER

Thanks.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo