Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chained constructers in C# - using intermediate logic

Tags:

c#

constructor

From this thread : http://geekswithblogs.net/kaju/archive/2005/12/05/62266.aspx someone asked (in the comments) this question:

is there any way to do something like this:

public FooBar(string fooBar)
{
string[] s = fooBar.split(new char[] { ':' });
this(s[0], s[1]);
}

public Foo(string foo, string bar)
{
...
} 

Well, I've run into the a situation where I need the same thing. Is it somehow possible? Thanks in advance.

EDIT

I meant this

public Foo(string fooBar)
{
string[] s = fooBar.split(new char[] { ':' });
this(s[0], s[1]);
}

public Foo(string foo, string bar)
{
...
} 

Foo is a constructor.

My problem is that I have to do a lot of logic - including some IO stuff - before calling the other constructor.

like image 798
Moberg Avatar asked Jan 24 '26 00:01

Moberg


1 Answers

Not directly, but:

public FooBar(string fooBar)
    : this(fooBar.Split(new char[] { ':' }))
{
}

private FooBar(string[] s)
    : this(s[0], s[1])
{
}

public FooBar(string foo, string bar)
{
...
}
like image 196
Tim Robinson Avatar answered Jan 26 '26 18:01

Tim Robinson