.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Constructors

Posted By:Shashi Ray       Posted Date: February 22, 2009    Points: 10    Category: C#    URL: http://www.dotnetspark.com  
 

Constructors

 

All classes have specific constructors that are called when you create an instance of a class. These constructors always have the same name as the class. This applies to form classes as well as non- visual classes. Here is the constructor the system generates for our simple hello window in the class Form1:

 

public class Form1 {

public Form1(){ //constructor

InitializeComponent();

}

 

When you create your own classes, you must create constructor methods to initialize them, and can pass arguments into the class to initialize class parameters to specific values. If you do not specifically include a constructor in any class you write, a constructor having no arguments is

generated for you under the covers.

 

The InitializeComponent method is generated by the IDE as well, and contains code that creates and positions all the visual controls in that window. If we need to set up additional code as part of the initialization of a Form class, we will always write a private init method that we call after

the InitializeComponent method call.

 

public Form1(){

InitializeComponent();

init();

}

private void init() {

x = 12.5f; //set initial value of x

}

 

 

Shashi Ray


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend