Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor chaining passing computed values for parameters

Tags:

c#

How can I pass thru from one instantiator to another? Suppose we had this class. How do I pass from foo(string, string) to foo(Uri)?

public foo
{
   string path { get; private set; }
   string query { get; private set; }

   public foo (Uri someUrl)
   {
      // ... do stuff here
   }

   public foo (string path, string query)
   {
      Uri someUrl = new Uri(String.Concat(path, query);

      // ... do stuff here to pass thru to foo(someUrl)
   }
}
like image 951
Bill Noble Avatar asked Jun 23 '13 22:06

Bill Noble


People also ask

What parameters are required to be passed to a class constructor?

This method has four parameters: the loan amount, the interest rate, the future value and the number of periods. The first three are double-precision floating point numbers, and the fourth is an integer.

What does this () mean in constructor chaining concept?

Answer: Constructor chaining is the process of calling one constructor from another constructor with respect to current object. Constructor chaining can be done in two ways: Within same class: It can be done using this() keyword for constructors in same class.

What is constructor chaining and how can it be achieved in Java?

Constructor chaining is the process of calling a sequence of constructors. We can do it in two ways: by using this() keyword for chaining constructors in the same class. by using super() keyword for chaining constructors from the parent class.

Why we use parameterized constructor in C#?

Parameterized Constructor in C# A constructor with at least one parameter is called a parameterized constructor. The advantage of a parameterized constructor is that you can initialize each instance of the class with a different value.


2 Answers

For simple constructor chaining, you can use the special keywords this or base to refer to the current or parent class's other constructors. You are allowed to use any parameters for the executing constructor as parameters to the chained constructor, and you can combine them using any legal single expression. This is basically the same rules that apply to any other inline expression supplied to a function call, except that you should avoid using any members of the class (since it's not been constructed yet) and restrict yourself to constants and pass-through parameters:

public foo (Uri someUrl)
{
     this.url = someUrl;
}

public foo (string path, string query)
    : this(new Uri(String.Concat(path, query)))
{
    // this.url is now set and available.
}

This will work as long as the processing you need to do can be done in a single valued expression. If you needed, for example, to do something else with the Uri before it's sent to the other constructor, or if you needed some complex if/then logic, you wouldn't be able to get away with this technique. The alternative is to refactor your code into an initialization method:

public foo (Uri someUrl)
{
    this.init(someUrl);
}

public foo (string path, string query)
{
    var url = String.Concat(path, query);
    url = url.Replace("http://", "https://");

    this.init(url);
}

private void init (Uri someUrl)
{
     this.url = someUrl;
}
like image 74
Michael Edenfield Avatar answered Nov 14 '22 22:11

Michael Edenfield


You can also do something like:

class Frob
{
    public Frob (Uri uri)
    {

    }

    public Frob(string path, string query)
        : this(TidyUpUri(path, query))
    {

    }

    private static Uri TidyUpUri(string path, string query)
    {
        var uri = new Uri(string.Concat(path, query));

        // etc.

        return uri;
    }
}
like image 31
a little sheep Avatar answered Nov 14 '22 21:11

a little sheep