Protected base constructor : Base Class « Class Module « VB.Net Tutorial






Public MustInherit Class Product
  Public Price As Single
  Public Location As String = "Unshelved"
  Public IsSpecial As Boolean = False
  Public Type As String = "Not Defined"

  Protected Sub New(T As String, Optional s As Boolean = False)
    If s Then IsSpecial = True 
    Type = T
  End Sub 
End Class

Class MyProduct
  Inherits Product

  Public Title = "Test Title"

  Public Sub New()
    MyBase.New("CD")
  End Sub

  Public Sub New(IsSpecial As Boolean)
    MyBase.New("CD", IsSpecial)
  End Sub

  Public Sub New(T As String, Optional IsSpecial As Boolean = False)
    Me.New(IsSpecial)
    Title = T
  End Sub

End Class

Module Test
  Sub Main()
    Dim C1 = New MyProduct("S")
    Dim C2 = New MyProduct("H", True)

    C1.Price = 9.99
    C2.Price = 12.99

    Console.WriteLine(C1.Title)
    Console.WriteLine(C1.Location)
    Console.WriteLine(C1.Price)
    
    Console.WriteLine(C2.Title)
    Console.WriteLine(C2.Location)
    Console.WriteLine(C2.Price)
  End Sub
End Module
S
Unshelved
9.99
H
Unshelved
12.99








6.32.Base Class
6.32.1.Call constroctor from base class
6.32.2.Shadows method in base class
6.32.3.Protected base constructor
6.32.4.Call method with MyBase