Constructor

Z

Zoltan Hernyak

Hi,

see my classes:

public class One
{
public One(string s) { .... }
}

public class Two:One
{
public class Two(int x)
{
...
}
}

what if I have to make some preparation before call the "base"
constructor from Two? for example:

public class Two(int x)
{
string s = String.Format("Your int was {0}",x);
base(s); // this is not working !!!!
}

So, the question is: how can I call the base constructor if
I can't call using the ":base( )" syntax. Is there any other
possibility to call the base constructor INSIDE the descendant
class' constructor?

aroan
 
D

Daniel O'Connell

Zoltan Hernyak said:
Hi,

see my classes:

public class One
{
public One(string s) { .... }
}

public class Two:One
{
public class Two(int x)
{
...
}
}

what if I have to make some preparation before call the "base"
constructor from Two? for example:

public class Two(int x)
{
string s = String.Format("Your int was {0}",x);
base(s); // this is not working !!!!
}

So, the question is: how can I call the base constructor if
I can't call using the ":base( )" syntax. Is there any other
possibility to call the base constructor INSIDE the descendant
class' constructor?
No, the language does not allow this.
Assuming you have a simple case like the one you posed, you can do
public Two(int x) : base(String.Format("Your int was {0}",x) {

}

If the case is more complex, then you may be able to use a static method (be
careful not to cause a stack overflow like this):

public class Two : One
{
public Two(int x) : base(Two.FormatInt(x))
{

}
static string FormatInt(int x)
{
//make sure you NEVER call new Two() from thsi method, however!
return String.Format("Your int was {0}",x);
}
}
 
Z

Zoltan Hernyak

public Two(int x) : base(Two.FormatInt(x))

Ok. I understand. But let's suppose One(int a, int b) constuctor needs
two ints, and works only when a<b.

One x = new One(1,2); // will work
One x = new One(2,1); // throws an exception
One x = new One(1,1); // throws an exception

I want to extend this class to "TWO". Two class' constructor
can work when a<b or a=b. When a<b, we needs to change a with b,
when a=b, we can't allow the instance to be created.

public Two(int a, int b):base( ???? )
{
if (a<b) call base(a,b)
else if (a<b) call base(b,a);
else throw new Exception("Wrong attribues!")
}

Two x = new Two(1,2); // will work
Two x = new Two(2,1); // will work
Two x = new Two(1,1); // throws an exception

How can I do that?
public Two(int x) : base(Two.FormatInt(x))

Ok. I understand. But let's suppose One(int a, int b) constuctor needs
two ints, and works only when a<b.

One x = new One(1,2); // will work
One x = new One(2,1); // throws an exception
One x = new One(1,1); // throws an exception

I want to extend this class to "TWO". Two class' constructor
can work when a<b or a=b. When a<b, we needs to change a with b,
when a=b, we can't allow the instance to be created.

public Two(int a, int b):base( ???? )
{
if (a<b) call base(a,b)
else if (a<b) call base(b,a);
else throw new Exception("Wrong attribues!")
}

Two x = new Two(1,2); // will work
Two x = new Two(2,1); // will work
Two x = new Two(1,1); // throws an exception

How can I do that?
 
J

Jon Skeet [C# MVP]

Zoltan Hernyak said:
Ok. I understand. But let's suppose One(int a, int b) constuctor needs
two ints, and works only when a<b.

One x = new One(1,2); // will work
One x = new One(2,1); // throws an exception
One x = new One(1,1); // throws an exception

I want to extend this class to "TWO". Two class' constructor
can work when a<b or a=b. When a<b, we needs to change a with b,
when a=b, we can't allow the instance to be created.

Well, there *are* ways round it - the best is probably to use a factory
method instead, ie:

public static Two CreateTwo (int a, int b)
{
if (a < b)
return new Two (a, b);
if (b < a)
return new Two (b, a);
if (a==b)
throw new ArgumentException ("a and b must be different");
}

private Two (int a, int b) : base (a, b)
{
}

I can't say I've run into this kind of problem many times though.
 
D

Daniel O'Connell

Jon Skeet said:
Well, there *are* ways round it - the best is probably to use a factory
method instead, ie:

public static Two CreateTwo (int a, int b)
{
if (a < b)
return new Two (a, b);
if (b < a)
return new Two (b, a);
if (a==b)
throw new ArgumentException ("a and b must be different");
}

private Two (int a, int b) : base (a, b)
{
}

I can't say I've run into this kind of problem many times though.
Ya, me either..
Goes to show, however, always provide an accurate sample case or people may
not understand what you need.
 
Z

Zoltan Hernyak

public static Two CreateTwo (int a, int b)
{
if (a < b)
return new Two (a, b);
if (b < a)
return new Two (b, a);
if (a==b)
throw new ArgumentException ("a and b must be different");
}

Ahh, I see. I worked a lot in Delphi, where constructors behave like
methods, and it is simple to call a base constructor (and many
constructors) inside another constructor... I was just thinking
the old way... thanks for the advices.

Zoltan Hernyak
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top