Reputation: 15817
Say I have a class structure like this:
Public Class Student
Public Enum Days
Monday
Tuesday
Wednesday
Thursday
End Enum
Public _Day As Days
Public Shared Function Factory(ByVal StudentType As String, ByVal day As Days) As Student
If StudentType = "PostGraduate" Then
Return New PostGraduate(day)
Else
Return New Undergraduate(day)
End If
End Function
End Class
Public Class PostGraduate
Inherits Student
Public Sub New(ByVal day As Days)
End Sub
End Class
Public Class Undergraduate
Inherits Student
Public Sub New(ByVal day As Days)
_Day = day
End Sub
End Class
Only the undergraduate class uses the enum, however the Enum has to be declared in the superclass (Student) because the factory method requires a Day to be passed to it. Is there a way I can put the Enum in the Undergraduate class? Unfortunately you cannot inherit from enums.
Upvotes: 0
Views: 100
Reputation: 7590
If PostGraduate
does not need to use Days
, then take out the constructor Days
parameter but initialise Days
to a default value.
This brings another very important point: use a default 0 value for your Enums, something like Unknown, NotSet or anything. Enum are just kind-of glorified int
s, if you don't initialise them they start at 0, which in your case means Days.Monday
... that is not what you want.
You should also make your _Day
property as public
get but protected
set (sorry I don't remember the VB keyword, basically it can only be set by itself and derived classes).
Upvotes: 1