IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesDelphi Prism  Print This     

Constructor

General Info: Class Constructor

Constructors are called when you instantiate an object from a class. This is where you can initialize variables and put code you wish executed each time the class is created. When you initially set the member fields and properties of an object, you are initializing the state of the object. The state of an object is the values of all it's member fields and properties at a given time.

Languages Focus

What is the syntax? Can you overload constructors? Is a special method name reserved for constructors?

Delphi Prism:  "Constructors" constructor + class name

In Prism, a constructor is called whenever a class or struct is created. You use the constructor keyword with an unnamed method. You can overload the constructor simply by adding two or more unnamed methods with various parameters.

Prism also supports a Create constructor method for backward compatibility with Delphi for Win32.

If you do not create a constructor, Prism will create an implicit constructor that initializes all member fields to their default values.

Constructors can execute at two different times. Static constructors are executed by the CLR before any objects are instantiated. Regular constructors are executed when you create an object.

Syntax Example:
Cyborg = public class
public
  constructor();
  constructor(pName: String);
end;




Cross Reference Examples:

Access VBA:  "Constructors" Class_Initialize

When an object instance is created from a class, Access VBA calls a special parameter-less sub named Class_Initialize. Since you cannot specify parameters for this sub, you also cannot overload it.

When a class is destroyed, Access VBA calls a special sub called Class_Terminate.

More Info / Comment  
ASP Classic:  "Constructors" Class_Initialize

When an object instance is created from a class, ASP calls a special parameter-less sub named Class_Initialize. Since you cannot specify parameters for this sub, you also cannot overload it.

When a class is destroyed, ASP calls a special sub called Class_Terminate.

Syntax Example:
Class Cyborg
  Public CyborgName
 
 Public Sub Class_Initialize
   Response.Write "<br>Class created"
   CyborgName = "Cameron"
  End Sub 
End Class
C#:  "Constructors" Use class name

In C#, a constructor is called whenever a class or struct is created. A constructor is a method with the same name as the class with no return value and you can overload the constructor.

If you do not create a constructor, C# will create an implicit constructor that initializes all member fields to their default values.

Constructors can execute at two different times. Static constructors are executed by the CLR before any objects are instantiated. Regular constructors are executed when you create an object.

Syntax Example:
public class Cyborg
{
public string CyborgName;
  
  //Constructor.
  public Cyborg(string pName)
{
CyborgName = pName;
}
}
C++:  "Constructors" Use Class name

A member function with the same name as the class.

Syntax Example:  
class X {
public:
X(); // constructor for class X
};
Corel Paradox:  "Constructors" Not Supported
Delphi:  "Constructors" constructor

In Delphi, use the constructor keyword to signify which method or methods are constructors for a class. It is traditional but not required to use a procedure called Create.

In addition to having multiple named constructors, you can overload constructors.

Syntax Example:
//Interface section.
TCyborg = class(TObject)
public
  constructor Create;
end; 

Then implement the class constructor in the Implementation section.

constructor TCyborg.Create;
begin
  inherited;  //Call the parent Create method
end;
Java:  "Constructors" Use class name

A method with the same name as the class.

Syntax Example:  
public class Cyborg{
  //Constructors have the same name as the class.
  public Cyborg() {
  }
}
VB Classic:  "Constructors" Class_Initialize

When an object instance is created from a class, VB6 calls a special parameter-less sub named Class_Initialize. Since you cannot specify parameters for this sub, you also cannot overload it.

When a class is destroyed, VB6 calls a special sub called Class_Terminate.

More Info / Comment  
VB.Net:  "Constructors" New

In VB.Net, a constructor is called whenever a class or struct is created. A constructor is a sub named New. You can overload the constructor simply by adding two or more New subs with various parameters (overloaded constructors).

If you do not create a constructor, VB.Net will create an implicit constructor that initializes all member fields to their default values.

Constructors can execute at two different times. Static constructors are executed by the CLR before any objects are instantiated. Regular constructors are executed when you create an object.

You can invoke the parent constructor with MyBase.New.

Syntax Example:
Public Class Cyborg
Public CyborgName As String
  
  Public Sub New(ByVal pName As String)
CyborgName = pName
End Sub
End Class




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]