Visual Studio Development Bookmark and Share   
 index > Visual C# Express Edition > Class constructor using self in call
 

Class constructor using self in call

In a class constructot, I wan to call another function using the newly created class as a parameter
Thomas Morgan  Tuesday, October 06, 2009 12:09 AM
You can use "this" inside of a constructor.  For example, both of the following work:


public class MyClass()
{
     public MyClass()
     {
          this.Initialize();
          WorkOnMyClass(this);
      }

     private void Initialize()
     {
     }

     static void WorkOnMyClass(MyClass instance)
     {
         Console.WriteLine(instance.ToString());
     }
}


However, I recommend not doing anything that may throw an exception (if avoidable), or take a long period to run, in a constructor.  It's a good idea to make constructors as small as possible, given that they will setup the class in a consistent, valid state.

Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Tuesday, October 06, 2009 12:27 AM
You can use "this" inside of a constructor.  For example, both of the following work:


public class MyClass()
{
     public MyClass()
     {
          this.Initialize();
          WorkOnMyClass(this);
      }

     private void Initialize()
     {
     }

     static void WorkOnMyClass(MyClass instance)
     {
         Console.WriteLine(instance.ToString());
     }
}


However, I recommend not doing anything that may throw an exception (if avoidable), or take a long period to run, in a constructor.  It's a good idea to make constructors as small as possible, given that they will setup the class in a consistent, valid state.

Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Tuesday, October 06, 2009 12:27 AM

You can use google to search for other answers

Custom Search

More Threads

• How do I reset my Snippets?
• how to identify the date format in asp.net
• buid a application!
• How do I get the text(ie the text of an item) of selected Item of a listview?
• create setup package
• How do I populate a textBox in a form with text from a different form?
• how do you autostretch a GDI drawing?
• Can I pass a value from InitializeComponent to the rest of my form code?
• using namespace.
• Help! plz