In C #, I have the following code:
- base class with virtual function InitClass ()
- a child class that overrides the InitClass () function. In this function, I set a personal variable
public property that opens my private field
namespace InheritenceTest
{
class Program
{
static void Main(string[] args)
{
ChildClass childClass = new ChildClass();
Console.WriteLine(childClass.SomeString);
Console.ReadLine();
}
}
public class BaseClass
{
public BaseClass()
{
InitClass();
}
protected virtual void InitClass()
{
}
}
public class ChildClass: BaseClass
{
private string _someString = "Default Value";
public ChildClass()
{
}
protected override void InitClass()
{
base.InitClass();
_someString = "Set in InitClass()";
}
public string SomeString
{
get { return _someString; }
set { _someString = value; }
}
}
}
Exit Console from this program "Set in InitClass ()".
Now I have converted this piece of code to VB.NET.
Imports System
Imports System.Collections.Generic
Imports System.Text
Namespace InheritenceTest
Module Program
Public Sub Main(ByVal args As String())
Dim childClass As New ChildClass()
Console.WriteLine(childClass.SomeString)
Console.ReadLine()
End Sub
End Module
Public Class BaseClass
Public Sub New()
InitClass()
End Sub
Protected Overridable Sub InitClass()
' Do Nothing here
End Sub
End Class
Public Class ChildClass
Inherits BaseClass
Private _someString As String = "Default Value"
Public Sub New()
End Sub
Protected Overrides Sub InitClass()
MyBase.InitClass()
_someString = "Set in InitClass()"
End Sub
Public Property SomeString() As String
Get
Return _someString
End Get
Set(ByVal value As String)
_someString = value
End Set
End Property
End Class
End Namespace
The output of this program is VB.NET "Default Value".
When you look at the code during debugging (VB.NET code), you see that when the constructor finished the job, the default value is set, and for C # it is the other way around (the default value is first set, which is what the constructor is called for).
- , ? ""?
Hemar