473,327 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to call an empty constructor withing it's class

Hi,

How do I call the 1st constructor in the second one (as this(); doesn't
work)? Or is it automatic?

public User(){
this._dtMngmt=new DataMngmt();
}

public User(String login, String password){
this();
this._login=login;
this._pwd=password;
}

Thank you for your answers.

Nov 17 '05 #1
5 3320
In message <11**********************@z14g2000cwz.googlegroups .com>,
Pierre <bu****@no-log.org> writes
Hi,

How do I call the 1st constructor in the second one (as this(); doesn't
work)? Or is it automatic?

public User(){
this._dtMngmt=new DataMngmt();
}

public User(String login, String password){
this();
this._login=login;
this._pwd=password;
}

Thank you for your answers.


public User(String login, String password):this()
{
this._login=login;
this._pwd=password;
}

--
Steve Walker
Nov 17 '05 #2
Pierre wrote:
Hi,

How do I call the 1st constructor in the second one (as this();
doesn't work)? Or is it automatic?

public User(){
this._dtMngmt=new DataMngmt();
}

public User(String login, String password){
this();
this._login=login;
this._pwd=password;
}

Thank you for your answers.


it's not automatic. You can use:

public User(String login, String password) : this()
{
this._login=login;
this._pwd=password;
}

It's called *before* the rest of the code.

An other solution is to have all constructors call some private method
for the common initialisation.
Hans Kesting
Nov 17 '05 #3
The other's have it right, it's the
: this();
that is the key to calling the empty constructor.

I make that mistake all the time too. I'm coming from java where you have
to put the call to any constructor first. The C# way at least enforces you
not to do any object creation when the other constructor is called.
Nov 17 '05 #4
You should, in general, call your constructors in the opposite fashion:
the no-args constructor should call the other one with default values,
even if they're null:

public User() : this(null, null)
{ }

public User(string login, string password)
{
this._dtMngmt = new DataMngmt();
this._login = login;
this._password = password;
}

This is so that you never forget to initialize fields, and the default
values for the fields are explicitly written into your code, rather
than silently defaulting the way they did in your original code.

Nov 17 '05 #5
> You should, in general, call your constructors in the opposite fashion:
I like this way, the only problem I find with VS2k3 is there is no
intellisense for arguments, especially when you're calling base methods. Is
this fixed in vs2k5? Or are you able to get the intellisense to work for
you in this case?

Thanks.
Nov 17 '05 #6

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

Similar topics

5
by: S. Levent Yilmaz | last post by:
Is it possible to have the constructor in an interface call member functions polymorphically? For instance: ============================== // INTERFACE CLASS class SomeInterface { public:...
3
by: Matt Graham | last post by:
Here's a subset of a class I'm trying to initialize: class uiForm { public: uiForm(); uiForm( unsigned short ); unsigned short m_method; unsigned short m_form_id; FormType *m_frmP; char...
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) .
4
by: William Payne | last post by:
Hello, I was under the impression that if I made a class Foo and if I didn't specify a copy constructor I would get one anyway that simply assigns the member variables (and that won't work for...
3
by: Ng Hun Yang | last post by:
I have a class that has a default constructor that initializes the class's variables. Now I want to add a new constructor to skip the initialization. My attempt: class Integer { public: class...
1
by: Art Vandelay | last post by:
Hello, I am trying to execute a VBScript, that exists inside of my project (via Add New Item>VBScript), through a button1_click event. I have not been able to find any code samples to...
8
by: meendar | last post by:
what will a object of an Empty class( contain nothing), do on default.What are all the default methods it calls. what is the use of creating the object for an empty class?
9
by: sks | last post by:
dear all, i have a problem . consider the following code class ABC { int & r; public: ABC( int &a) : r(a) {} }; int main() {
5
by: desktop | last post by:
I am reading a text that says: "The derived-class constructor initializer names its base-class followed by a (possibly empty) list of arguments. These arguments are the initial values to use in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.