PC Review


Reply
Thread Tools Rate Thread

Calling a constructor in its own class

 
 
Nicolas
Guest
Posts: n/a
 
      30th Dec 2004
Hi,

How can one call a constructor in the same class as the constructor itself ?

Thanks.
 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      30th Dec 2004

>How can one call a constructor in the same class as the constructor itself ?


class Foo
{
public Foo(string s) { ... }
public Foo(int i) : this(i.ToString()) {}
// like this -> ^^^^^^^^^^^^^^^^^^^^
}



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Nicolas
Guest
Posts: n/a
 
      30th Dec 2004
Mattias Sjögren wrote:
>>How can one call a constructor in the same class as the constructor itself ?

>
>
> class Foo
> {
> public Foo(string s) { ... }
> public Foo(int i) : this(i.ToString()) {}
> // like this -> ^^^^^^^^^^^^^^^^^^^^
> }
>


I'm sorry that won't solve my problem, because what I need to do is to
call a constructor from another method of the same class (not from
another constructor of the same class)
 
Reply With Quote
 
Peter N Roth
Guest
Posts: n/a
 
      30th Dec 2004
if you mean like

C c1 = new C();
C c2 = c1.Clone();

then Clone will look like

public C Clone() {
C result = new C();
return result;
}
ottomh
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET


"Nicolas" <(E-Mail Removed)> wrote in message
news:41d4361e$0$4697$(E-Mail Removed)...
> Mattias Sjögren wrote:
>>>How can one call a constructor in the same class as the constructor
>>>itself ?

>>
>>
>> class Foo
>> {
>> public Foo(string s) { ... }
>> public Foo(int i) : this(i.ToString()) {}
>> // like this -> ^^^^^^^^^^^^^^^^^^^^
>> }
>>

>
> I'm sorry that won't solve my problem, because what I need to do is to
> call a constructor from another method of the same class (not from another
> constructor of the same class)



 
Reply With Quote
 
James Curran
Guest
Posts: n/a
 
      30th Dec 2004
Just call it like you would from anywhere else....

class Foo
{
public void SomeFunc()
{
Foo foo = new Foo();
// etc....
}
}


Unless you are retrying to restructure the object itself, inwhich case, you
don't want to call a constructor at all (the object already exists), but a
initializer:

class Foo
{
public Foo(int n, string s)
{
Set(n,s);
}

public SomeFunc()
{
Set(5, "Hello World");
}

private void Set(int n, string s)
{
// So what ever you want here.
}
}

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Nicolas" <(E-Mail Removed)> wrote in message
news:41d42319$0$31457$(E-Mail Removed)...
> Hi,
>
> How can one call a constructor in the same class as the constructor itself

?
>
> Thanks.



 
Reply With Quote
 
Richard Blewett [DevelopMentor]
Guest
Posts: n/a
 
      30th Dec 2004
Are you trying to "re-initialize" your instance using a specific constructor?

Regards

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

Mattias Sj&ouml;gren wrote:
>>How can one call a constructor in the same class as the constructor itself ?

>
>
> class Foo
> {
> public Foo(string s) { ... }
> public Foo(int i) : this(i.ToString()) {}
> // like this -> ^^^^^^^^^^^^^^^^^^^^
> }
>


I'm sorry that won't solve my problem, because what I need to do is to
call a constructor from another method of the same class (not from
another constructor of the same class)

 
Reply With Quote
 
Nicolas
Guest
Posts: n/a
 
      30th Dec 2004
Richard Blewett [DevelopMentor] wrote:
> Are you trying to "re-initialize" your instance using a specific constructor?
>


That's it exactly. I know that simply calling an init() method from both
the constructor and the place where I want to re-init is an option, but
I am just curious to see whether it's possible to do it with a ctor.
 
Reply With Quote
 
James Curran
Guest
Posts: n/a
 
      30th Dec 2004
"Nicolas" <(E-Mail Removed)> wrote in message
news:41d46483$0$14150$(E-Mail Removed)...

> That's it exactly. I know that simply calling an init() method from both
> the constructor and the place where I want to re-init is an option, but
> I am just curious to see whether it's possible to do it with a ctor.


No. Ctors create something out of nothing. To allow ctors to do this
would be an abasement of the concept.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com




 
Reply With Quote
 
Peter N Roth
Guest
Posts: n/a
 
      30th Dec 2004
"James Curran" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
[ ]
> --
> Truth,
> James Curran

=======================================
> [erstwhile VC++ MVP] <<<<<<<<<<<<<<<< ???

=======================================
> Home: www.noveltheory.com Work: www.njtheater.com
> Blog: www.honestillusion.com Day Job: www.partsearch.com


Hi James - why'd you "switch"?
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET


 
Reply With Quote
 
James Curran
Guest
Posts: n/a
 
      3rd Jan 2005
"Peter N Roth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> =======================================
> > [erstwhile VC++ MVP] <<<<<<<<<<<<<<<< ???

> =======================================


> Hi James - why'd you "switch"?



In my new day job, I do C# instead of C++. And the questions one finds
in the C++ newsgroup were getting less interesting -- mostly dreary DLL or
memory issue.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com



 
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
InitializeComponent is generating the wrong code for my custom class. Calling it's base class constructor jrhoads23@hotmail.com Microsoft Dot NET Framework Forms 0 1st Feb 2005 07:10 PM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM
calling other constructor of same class Nikola Skoric Microsoft C# .NET 5 2nd Nov 2004 04:06 AM
Calling Constructor from within the class. Jeff Woodie Microsoft C# .NET 3 21st Aug 2003 11:55 PM
Calling Constructor from within the class. Zoury Microsoft C# .NET 2 21st Aug 2003 11:54 PM


Features
 

Advertising
 

Newsgroups
 


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