473,403 Members | 2,222 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,403 software developers and data experts.

Calling a constructor from another constructor

I am writing a class with several constructors and don't want to
repeat the code. How can I call a constructor from another
constructor?
Example:
class Foo
{
public Foo(string FName, string LName)
{
...some code ...
}

public Foo(string FName)
{
Foo(FName, String.Empty);
}

}

I keep getting "Foo is a type but is used like a variable" errors.

How do I implement this?

Tom P.
Jul 25 '08 #1
2 2205
On Fri, 25 Jul 2008 13:29:00 -0700, Tom P. <pa***********@gmail.comwrote:
[...]
public Foo(string FName, string LName)
{
...some code ...
}

public Foo(string FName)
{
Foo(FName, String.Empty);
}

}

I keep getting "Foo is a type but is used like a variable" errors.

How do I implement this?
Use the "this" keyword to call the constructor. Note that you'll have to
put the call as part of the method declaration, rather than inside the
method itself.

Pete
Jul 25 '08 #2
Tom P. <pa***********@gmail.comwrote:
I am writing a class with several constructors and don't want to
repeat the code. How can I call a constructor from another
constructor?
Example:
class Foo
{
public Foo(string FName, string LName)
{
...some code ...
}

public Foo(string FName)
{
Foo(FName, String.Empty);
}

}

I keep getting "Foo is a type but is used like a variable" errors.

How do I implement this?
public Foo (string FName) : this(FName, String.Empty)
{
}

You can call a base type constructor with "base" instead of "this".

You can only call one other constructor, you have to do it with the
above syntax (before anything else is executed, apart from variable
initializers), and you can't use "this" anywhere in the call, either
implicitly or explicitly.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 25 '08 #3

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

Similar topics

6
by: Asfand Yar Qazi | last post by:
Hi, Consider this: class C { int j; public: C(int i) : j(i)
2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
4
by: Mike Labosh | last post by:
Greetings, all: I want to invoke one constructor from another to gain code reuse. In the snip below, I want my second constructor to pass its first argument to the first constructor so that...
2
by: Sathyaish | last post by:
How does a constructor of one class call another of the same class? When would this mechanism make sense or be required? PS: Two instances I saw are: (a) While using the Singleton pattern...
31
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I...
4
by: Colin McGuire | last post by:
Hi, I have a class with lots of constructors - one is shown below Public Sub New(ByVal rows As Integer, ByVal columns As Integer) InitializeComponent() 'And code in here that sets instance...
4
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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
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,...

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.