
74
|
C# & VB.NET Conversion Pocket Reference
End Sub
End Class
Notice from this example that you must add a call to invoke
the base class’s constructor. To do this you must use the
MyBase keyword. The keyword MyBase can be used to invoke
any method in the base class. Unlike C#, in which you use
the
base keyword without specifying the name of a function,
in VB you must use
MyBase in combination with the New key-
word. The VB compiler forces you to write the line
MyBase.
New( )
before any other code in the constructor.
Initializers
Field initializers are class fields that are initialized in place, as
in the following C# example:
class Account
{
int Balance = 100;
public void MakeDeposit( )
{
}
}
The line:
int Balance = 100
is a field initializer. This code does not have an explicit con-
structor function. However, if you do not provide one, the
C# compiler adds a default constructor. It also adds code to
this constructor to invoke the field initializers. In other
words, when the code is compiled, the result should resem-
ble the following:
class Account
{
int Balance;
public Account( )
{
Balance = 100; //field initializers
base( ); //call the base constructor
C#
C#