473,386 Members | 1,799 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,386 software developers and data experts.

overloading a constructor and call one from the other

Hi,

I want to overload a constructor of a class. But I want to call the other
one from the second if called. I explain with code because of my english:

public class XMLConfig
{
public XMLConfig(string FileName)
{
// here I want to do something with Filename and some init
}

public XMLConfig()
{
// this one should call the other one with a default filename
XMLConfig("default_filename");
}

Can someone explain how to do this ? If I do it as this then I get a
compiler error.

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #1
5 1548
public class XMLConfig
{
public XMLConfig(string FileName)
{
// here I want to do something with Filename and some init
}

public XMLConfig()
: this("default_filename")
{
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I want to overload a constructor of a class. But I want to call the other
one from the second if called. I explain with code because of my english:

public class XMLConfig
{
public XMLConfig(string FileName)
{
// here I want to do something with Filename and some init
}

public XMLConfig()
{
// this one should call the other one with a default filename
XMLConfig("default_filename");
}

Can someone explain how to do this ? If I do it as this then I get a
compiler error.
Nov 16 '05 #2
Hi Richard,
public XMLConfig(): this("default_filename")
{
}


Thanks, this compiles, but I was not clear in my question. the 'default
filename' has to be computed in the overloaded constructor. With this coding
I can only give a fixed string to it. Or again I dont see something not :(

for exampel I want to call the create method in overloaded constructor like
somethin this:

XMLConfig(Path.ChangeExtension(Application.Executa blePath, ".xml");

rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #3
Wilfried Mestdagh <Wi**************@discussions.microsoft.com> wrote:
public XMLConfig(): this("default_filename")
{
}


Thanks, this compiles, but I was not clear in my question. the 'default
filename' has to be computed in the overloaded constructor. With this coding
I can only give a fixed string to it. Or again I dont see something not :(

for exampel I want to call the create method in overloaded constructor like
somethin this:

XMLConfig(Path.ChangeExtension(Application.Executa blePath, ".xml");


So do:

public XMLConfig() : this (Path.ChangeExtension
(Application.ExecutablePath, ".xml"))
{
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hi Jon,
public XMLConfig() : this (Path.ChangeExtension
(Application.ExecutablePath, ".xml"))
{
}


Amazing how solutions can be so simple :) Thanks !
One more question because I anger to learn :)

What if I want to have an overloaded constructor that has to execute a bunch
of extra code and then after it calls another constructor ?

eg:
public XMLConfig()
{
// here a lot of code
// after it call another overload constructor
}

thx, Wilfried
Nov 16 '05 #5
Wilfried Mestdagh <Wi**************@discussions.microsoft.com> wrote:
public XMLConfig() : this (Path.ChangeExtension
(Application.ExecutablePath, ".xml"))
{
}


Amazing how solutions can be so simple :) Thanks !
One more question because I anger to learn :)

What if I want to have an overloaded constructor that has to execute a bunch
of extra code and then after it calls another constructor ?

eg:
public XMLConfig()
{
// here a lot of code
// after it call another overload constructor
}


No, you can't do that - but you *can* call another method in the
parameter part, as shown with the example given before. (That was
Path.ChangeExtension, but it could be any other method, including
static methods in the same class.)

Of course, that doesn't help you if you need to call another
constructor which doesn't take any parameters.

If you find constructors restrictive, you may want to think about
making static methods which return an instance.

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

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

Similar topics

14
by: trouble | last post by:
Hi, I have the following code, which is basically overloading + and += for complex numbers. It works fine, but can someone tell me how to use the overloaded + in the definition of += rather than...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
6
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually...
4
by: Doug | last post by:
Hi I am 'returning' to the learning of C# after a change in jobs and I remember that I used to struggle with the defintion, purpose and function of 'overloading'. When I was beginning with C# I...
16
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.