I have been trying to find an elegant way to avoid repeating code in all of my derived classes. At this point, I am unsure as to the best way to proceed.
I'd like to write a single method in the base class that will instantiate and use any of its derived classes without having to edit the method when I write new derived classes.
I have tried learning/using a generic method but started to think I might be heading down a dead end for this application. I understand that using reflection can be expensive, and since this method is meant to handle hundreds or even thousands of Elements, it seemed like a bad idea.
Now I'm thinking of trying to pass in the class itself as an argument somehow... maybe. That doesn't seem quite right to me either.
I'm willing to do the research, but would love any help pointing me in the right direction.
Here is an abridged version of what I have...
Base Class:
public abstract class Element
{
public string ElementName { get; }
public List<string> BadParameters { get; set; } = new List<string>();
//Constructor
public Element(string name)
{
ElementName = name;
}
//The method in question---
public static List<string> GetBadParameters(//derived class to instantiate)
{
var elem = new //derived class();
return elem.BadParameters;
}
}
Derived Class 1:
public class Wall : Element
{
public double Length { get; set; }
public bool LoadBearing { get; set; }
//Constructor
public Wall(string name): base(name)
{
SetBadParameters();
}
public void SetBadParameters()
{
BadParameters = //A wall specific way of setting bad parameters
}
}
Derived Class 2:
public class Floor : Element
{
public double Area { get; set; }
public double Slope { get; set; }
//Constructor
public Floor(string name): base(name)
{
SetBadParameters();
}
public void SetBadParameters()
{
BadParameters = //A floor specific way of setting bad parameters
}
}
Implementation:
public class Implementation
{
public List<string> GetAllBadElementParameters()
{
List<string> output = new List<string>;
List<string> badWalls = GetBadParameters(//Wall class)
List<string> badFloors = GetBadParameters(//Floor class)
output = output.AddRange(badWalls).AddRange(badFloors);
return output;
}
}
EDIT - To clarify:
The actual content of
public List<string> BadParameters
does not matter. Bad parameters, how and why they are bad, are inconsequential.
What I'm trying to accomplish is avoid having the method "GetBadParameters()" defined in the derived class, since this method will be the exact same for all derived classes.
It is only the populating of the "BadParameter" base class property that changes from one derived class to another.
EDIT 2 - My attempt at a generic method in the base class:
I know this won't work, but it may convey what I'd like to have happen.
public static List<string> GetAllBadParameters<T>(List<string> names) where T : ANY DERIVED CLASS, new()
{
List<string> output = new List<string>();
foreach (string name in names)
{
var elem = new T(name);
foreach (string badParameter in elem.BadParameters)
{
output.Add(badParameter);
}
}
return output;
}