In C# 4, how can I have constructors with optional parameters in a subclass of a parent with an overloaded constructor?
Asked Answered
F

2

6

I have a parent class that has an overloaded constructor, and I have a subclass that has a constructor with optional parameters. Is there a way to have the subclass's constructors still expose the overloaded-ness of the parent class while preserving it's own optional parameters?

Here's some example code of the two classes and their required constructors:

class Foo {
    Foo(String arg0) 
    {
      // do some stuff with arg0
    }

    Foo(String arg0, List<x> arg1)
        : this(arg0)
    {
      // do some other stuff with arg1 that is special because we have an arg1
    }
}

class Bar : Foo {
    Bar(String arg0, List<y> arg2 = null, String arg3 = "") 
        : base(arg0)
    {
      // some third thing with arg2 and arg3
    }
}

This is the method signature for the other subclass constructor I would like to also have to expose the overload of the parent constructor, but the question is how to do it:

Bar(String arg0, List<x> arg1, List<y> arg2 = null, String arg3 = "")

I have, I think, found a solution, but I am not sure it is as clean as it could be. I have posted it as an answer just in case it is the only option.

Fakir answered 6/5, 2011 at 18:55 Comment(3)
Can you change the signature of Foo to just have one constructor Foo(String arg0, List<x> arg1 = null)? Or do you need to distinguish between passing null and not passing a value at all?Arlinda
@Arlinda - I need to distinguish between passing null vs. not passing anything as that parameter. I will update the example to reflect this. Unfortunately, it's a lot more complicated than this example, but what I have here is the crux of it.Fakir
I'd personally try to revisit the design to see if it'd be possible to change it to not have that requirement, but it's hard to say if that's possible without knowing more about what it's supposed to do.Arlinda
F
2

Here is the solution I have come up with:

class Foo {
    Foo(String arg0) 
    {
      // do some stuff with arg0
    }

    Foo(String arg0, List<x> arg1)
        : this(arg0)
    {
      // do some other stuff with arg1
    }
}

class Bar : Foo {
    Bar(String arg0, List<y> arg2 = null, String arg3 = "") 
        : base(arg0)
    {
        this.Initialize( arg2, arg3);
    }

    Bar(String arg0, List<x> arg1, List<y> arg2 = null, String arg3 = "")
        : base(arg0, arg1)
    {
      this.Initialize( arg2, arg3);
    }

    private void Initialize(List<y> arg2, String arg3)
    {
      // some third thing with arg2 and arg3
    }
}

It seems a bit unclean because I am not chaining the subclass's constructors together and am calling a function instead, but I can't figure out any other way to do this.

Fakir answered 6/5, 2011 at 18:56 Comment(0)
A
4

If you can change Foo to only have one constructor with an optional parameter you can do the following:

public class Foo
{
    public Foo(String arg0, List<x> arg1 = null)
    {
        // do some stuff with arg0
        if (arg1 != null)
        {
            // do some other stuff with arg1
        }
    }
}

public class Bar : Foo
{
    public Bar(String arg0, List<x> arg1 = null, List<y> arg2 = null, String arg3 = "")
        : base(arg0, arg1)
    {
        // some third thing with arg2 and arg3
    }
}
Arlinda answered 6/5, 2011 at 19:11 Comment(0)
F
2

Here is the solution I have come up with:

class Foo {
    Foo(String arg0) 
    {
      // do some stuff with arg0
    }

    Foo(String arg0, List<x> arg1)
        : this(arg0)
    {
      // do some other stuff with arg1
    }
}

class Bar : Foo {
    Bar(String arg0, List<y> arg2 = null, String arg3 = "") 
        : base(arg0)
    {
        this.Initialize( arg2, arg3);
    }

    Bar(String arg0, List<x> arg1, List<y> arg2 = null, String arg3 = "")
        : base(arg0, arg1)
    {
      this.Initialize( arg2, arg3);
    }

    private void Initialize(List<y> arg2, String arg3)
    {
      // some third thing with arg2 and arg3
    }
}

It seems a bit unclean because I am not chaining the subclass's constructors together and am calling a function instead, but I can't figure out any other way to do this.

Fakir answered 6/5, 2011 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.