Constructor

G

Guest

I forget the right syntax....

If I Have multiple signatures for a constructor, but I want each to call a
"main" version of the constructor...I can't remember how to do this in C#....

public MyClass(string a, string b)
{
Do something....
}

public MyClass(sting a, string b, string c)
{
_FieldC = c;
MyClass(a, b);
}

So trying to call the "main" constructor as MyClass(a,b) from withing the
MyClass(a, b, c) constructors is not the right syntax...how should this be
called in C#?

Thanks in advance for your assistance...
 
G

Guest

It would appear that this would invoke the inherited method first and then
the "original called constructor". What if I needed the called constructor
to be invoked first and then the "base" constructor?

Thanks for your comments!!!!
 
P

Peter Duniho

It would appear that this would invoke the inherited method first and
then
the "original called constructor". What if I needed the called
constructor
to be invoked first and then the "base" constructor?

Your original question, and the subsequent reply, did not appear to have
anything to do with any inherited class. So I'm a bit confused as to what
you mean by "it would appear that this would...", since you go on to talk
about inherited methods, which weren't a party of any of this thread so
far. What is "this" in "that this would" that you are referring to (since
obviously not any part of the thread so far)?

In any case, you cannot call an inherited constructor _after_ executing
your own constructor. One of the basic rules of all the OOP languages
I've used is that the base class has to be fully constructed/initialized
before the derived class is. This precludes calling your derived
constructor before you execute the constructor for the base class
(implicitly or explicitly).

By the way, you didn't ask, but if you do still want to call the base
constructor, you can do it in the same way that Alex showed you to call a
constructor from the same class, except that you use the keyword "base"
instead of "this".

Pete
 
N

Nicholas Paldino [.NET/C# MVP]

It is possible to do what you want, but not through a constructor.
Rather, you should have an initialization method which you call from your
constructor.
 
F

Fred Mellender

Could you not use the "this" construct:

public MyClass (string a, string b, string c): this(a,b)
{
_FieldC = c;
}

Nicholas Paldino said:
It is possible to do what you want, but not through a constructor.
Rather, you should have an initialization method which you call from your
constructor.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"OldButStillLearning" <[email protected]>
wrote in message
I forget the right syntax....

If I Have multiple signatures for a constructor, but I want each to call
a
"main" version of the constructor...I can't remember how to do this in
C#....

public MyClass(string a, string b)
{
Do something....
}

public MyClass(sting a, string b, string c)
{
_FieldC = c;
MyClass(a, b);
}

So trying to call the "main" constructor as MyClass(a,b) from withing the
MyClass(a, b, c) constructors is not the right syntax...how should this
be
called in C#?

Thanks in advance for your assistance...
 
N

Nicholas Paldino [.NET/C# MVP]

Fred,

You could, but that would result in the other constructor being called
before _FieldC is assigned. The OP wants to do something like this (this is
not valid syntax):

public MyClass
{
public MyClass()
{
// Do something here.
}

public MyClass(int x)
{
// Do something else.
...

// Call the default constructor.
this();
}
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fred Mellender said:
Could you not use the "this" construct:

public MyClass (string a, string b, string c): this(a,b)
{
_FieldC = c;
}

Nicholas Paldino said:
It is possible to do what you want, but not through a constructor.
Rather, you should have an initialization method which you call from your
constructor.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"OldButStillLearning" <[email protected]>
wrote in message
I forget the right syntax....

If I Have multiple signatures for a constructor, but I want each to call
a
"main" version of the constructor...I can't remember how to do this in
C#....

public MyClass(string a, string b)
{
Do something....
}

public MyClass(sting a, string b, string c)
{
_FieldC = c;
MyClass(a, b);
}

So trying to call the "main" constructor as MyClass(a,b) from withing
the
MyClass(a, b, c) constructors is not the right syntax...how should this
be
called in C#?

Thanks in advance for your assistance...
 

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