Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling overloaded constructor with values inside another constructor

0 views
Skip to first unread message

Tristan

unread,
Mar 30, 2004, 6:47:48 AM3/30/04
to
Hi,

I am trying to call an overloaded constructor from within an another
one with parameters that have to be processed inside the constructor.
Therfore I can't use the Constructor() : Constructor(params) type of
syntax. I think the code below, explains better what I'm trying to do.

Class AClass
{
AClass(string fileName)
{ }

AClass(string key)
{
// Do some special processing on key to get a fileName

// Here I want to call AClass(fileName) but how!!!!
}
}

I suspect this isn't possible and I just have to define some private
method that can get called by both constructors?

Any help appreciated.

Tristan.

Jon Skeet [C# MVP]

unread,
Mar 30, 2004, 6:56:08 AM3/30/04
to

Well, the first problem is that both of those constructors have the
same signature. However, once you've got rid of that, the way to
proceed is:

AClass (string key) : this (KeyToFilename(key))
{
}

// FileInfo used here as example
static FileInfo KeyToFilename(string key)
{
....
}

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

Marcin Grzębski

unread,
Mar 30, 2004, 7:05:49 AM3/30/04
to
Hi Tristan,

It's impossible to define the same constructor second time.
Constructor/method parameters are identified by types (not by names).

If You want to create second constructor try this:

AClass(object key)
: this(key.ToString())
{
// and other stuff
}

Regards

Marcin

0 new messages