PC Review


Reply
Thread Tools Rate Thread

Calling overloaded constructor with values inside another constructor

 
 
Tristan
Guest
Posts: n/a
 
      30th Mar 2004
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.
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      30th Mar 2004
Tristan <(E-Mail Removed)> wrote:
> 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?


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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?UTF-8?B?TWFyY2luIEdyesSZYnNraQ==?=
Guest
Posts: n/a
 
      30th Mar 2004
Hi Tristan,

> 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?


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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call an overloaded class constructor Chris Dunaway Microsoft C# .NET 4 2nd Nov 2005 06:14 PM
overloaded constructor, access zero-argument constructor mblatch Microsoft C# .NET 3 8th Apr 2005 09:53 PM
constructor chaining vs calling this. vs calling named constructor? Flip Microsoft C# .NET 9 29th Mar 2005 05:05 PM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM
How to know inside a constructor who called the constructor? Efkas Microsoft C# .NET 2 25th Nov 2004 05:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:22 PM.