What is constructor?

Aggregation refers to adding members to a class.When your class has members that are themselves classes, and the class takes responsibility for creating and destroying instances of those members, we call this an aggregation relationship. When a class has members that are classes but some external entity creates and destroys those classes.We refer to this as an association in your class, you need to create a constructor for your class.

You can create a constructor for any reason, but you will need to create a constructor if you want to instantiate or create an instance of a class aggregate members.The default constructor is represented by sub new with no parameters.

public sub new()
my base new()
farrylist= new arraylist()
end sub


Comments

Author: Gaurav Aroraa22 Feb 2009 Member Level: Gold   Points : 2

Hi,
Here is my view:

In a simple words Constructor is nothing but a method, a special kind of method of a class, which gets executed when its (class) object is created.
Now, let’s take above in broader sense, A constructor is a class method automatically executed whenever class’s object is created or whenever class is initialized. Consider following bit of code:


public class MsDotNetHeaven
{
public MsDotNetHeaven()
{
//A default Constructor
}

//Class members

}

In above snippet, try to change the name of constructor from MsDotNetHeaven to MsDotNetMentor and see whats happen, you have to put same name as you gave it to class.

What happened behind the scenes : whenever you try to create an object of class or initialize a class, then the default constructor is invoked.

//Initializes the Class
MsDotNetHeaven objMsDnH = new MsDotNetHeaven();


Author: Parvathi S02 May 2009 Member Level: Silver   Points : 2

Constructor gets called when the first object is created. If we did not specify constructor a default constructor will be automatically called which initializes all the member variables to zero.

we can also define our own default constructor for initializing members of the class.

Constructors can be overloaded.

we can pass arguments to the constructor.
Constructors should be in the same name of the class name.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: