Reputation: 15787
Is there any difference between doing this:
Public Class Class1
Protected Test1 As String
Public Sub New(ByVal test2 As String)
Test1 = test2
End Sub
End Class
Public Class Class2
Inherits Class1
Public Sub New()
MyBase.New("called from class 2")
End Sub
End Class
and this:
Public Class Class1
Protected Test1 As String
End Class
Public Class Class2
Inherits Class1
Public Sub New()
Test1 = "Called from class 2"
End Sub
End Class
In the first example, the superclass instance variable is initialised in the constructor. In the second example, the superclass instance variable is initialised in the subclass.
The reason I ask is because I am trying to do this from the subclass:
Public Sub New()
MyBase.New( System.Configuration.ConfigurationManager.AppSettings.Item("PurgeFile" & Me.GetType.Name), & _
System.Configuration.ConfigurationManager.AppSettings.Item("PurgeHeader" & Me.GetType.Name) )
End Sub
and I am getting an error: "reference to an object under construction is not valid when calling another constructor".
Upvotes: 0
Views: 913
Reputation: 125620
You can't use Me
within MyBase.New()
call, so following part of your code is invalid:
Me.GetType.Name
Update
There is huge difference your 2 samples: First one doesn't allow Class1
initialization without constructor parameter and the second one does.
I would rather think about something like:
Public MustInherit Class Class1
Public MustOverride ReadOnly Property Test1 As String
End Class
Public Class Class2
Inherits Class1
Private _Test1 As String = "Called from class 2"
Public Overrides ReadOnly Property Test1 As String
Get
Return _Test1
End Get
End Property
End Class
Upvotes: 2