Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and initialising different sub-types without lots of casting

Tags:

c#

.net

generics

I am sure am messing around with a lot of casting and such in this code below. It seems like there should be a smoother way. I'm basically trying to use a builder method (CreateNewPattern) to handle creating new objects of the passed sub-class type (by the CreateNewCircularPattern and CreateNewLinePattern methods). I presently only have two sub-classed types CircularHolePattern and SingleLineHolePattern that inherit from HolePattern, but I expect to have more as my app grows.

Is this a place for using a delegate or a lambda? It know nothing about them, so please be as specific as possible with and code suggestions.

private CircularHolePattern CreateNewCircularPattern()
{
   var CreatedPattern = CreateNewPattern(typeof(CircularHolePattern));
   return (CircularHolePattern)CreatedPattern;
}


private SingleLineHolePattern CreateNewLinePattern()
{
   var CreatedPattern=CreateNewPattern(typeof(SingleLineHolePattern));
   return (SingleLineHolePattern)CreatedPattern;
}

private HolePattern CreateNewPattern(Type PatternTypeToCreate)
{
    var NewHolePattern = (HolePattern)Activator.CreateInstance(PatternTypeToCreate);
    NewHolePattern.PatternName = "Pattern #" + (HolePatterns.Count + 1).ToString();
    this.AddPattern(NewHolePattern);
    this.SetActivePattern(NewHolePattern);
    return NewHolePattern;
}
like image 530
MattSlay Avatar asked Dec 05 '25 06:12

MattSlay


1 Answers

I suspect you want generics:

private T CreateNewPattern<T>() where T : HolePattern, new()
{
    var newHolePattern = new T();
    newHolePattern.PatternName = "Pattern #" +
        (HolePatterns.Count + 1).ToString();
    this.AddPattern(newHolePattern);
    this.SetActivePattern(newHolePattern);
    return newHolePattern;          
}

private SingleLineHolePattern CreateNewLinePattern() {
    return CreateNewPattern<SingleLineHolePattern>();
}

private CircularHolePattern CreateNewCircularPattern() {
    return CreateNewPattern<CircularHolePattern>();
}

The T is the generic-type-argument; the type we want to create. The where says "it must be HolePattern or a sub-type, and it must have a public parameterless constructor" - this lets us use new T() to create a new instance of it, and access all members of HolePattern against such instances (such as PatternName). This also allows us to call the methods that accept a HolePattern as an argument.

like image 183
Marc Gravell Avatar answered Dec 07 '25 22:12

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!