473,473 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Constructor

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
Nov 15 '05 #1
5 3941

"Zoltan Hernyak" <ar***@ektf.hu> wrote in message
news:es********************************@4ax.com...
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);
}
} aroan

Nov 15 '05 #2
On Sun, 12 Oct 2003 10:15:58 GMT, in
microsoft.public.dotnet.languages.csharp you wrote:
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?
aroan On Sun, 12 Oct 2003 10:15:58 GMT, in
microsoft.public.dotnet.languages.csharp you wrote:
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?
aroan


Nov 15 '05 #3
Zoltan Hernyak <ar***@ektf.hu> wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zoltan Hernyak <ar***@ektf.hu> wrote:
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.


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. --
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5
On Sun, 12 Oct 2003 14:20:12 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
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
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
13
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.