Reputation: 732
Is there a way of only-base class only methods?
I have a work around of using a module, but this separates the functionality which will only be utilized by the base-class.
I was think on the line of the following
Public MustInherit Class Token
' Token stuff
NotInheritable Shared Function Parse(Of T As Token)(CR As CharReader) As T
' Would also be good to be able to do the following without resorting
' to the reflection based bodgery.
Return T.Parser(CR)
End Function
End Class
Public Class Digit
Inherit Token
' Digit Stuff
Protected Shared Function Parser(CR As CharReader) As Digit
If CR.Current.HasValue = False Then Return Nothing
Case Select CR.Value
Case "0"c To "9"c
Return New Digit(CR.Index,0)
Case Else
Return False
End Select
End Function
So now when
Dim d0 = Token.Parse(Of Digit)(cr)
but
Dim d1 = Digit.
wouldn't show the Parse Method.
So how can this be done? (If possible at all)
EDIT
Current Implementations This should really a Base Class only method in the Token Class
Public Module TokenModule
Public Function Parse(Of T As Token)(cr As CharReader) As T
'
' Here Be Nasty Reflection Based Bodge Job
'
' Why? What I want to write. ( Call a static method on the generic (constrianed) type specifier.)
'
' Return T.Parser(cr)
'
' Start Bodgery {
Dim tt As T
tt = GetType(T).InvokeMember("Parser",
Reflection.BindingFlags.InvokeMethod +
Reflection.BindingFlags.NonPublic +
Reflection.BindingFlags.Static, Nothing, tt, {cr})
Return tt
' } End Bodgery
End Function
End Module
Token (Base) Class
Public MustInherit Class Token
Private _Index As Integer
Private _Count As Integer
Protected Friend Sub New(ByVal Index As Integer, Count As Integer)
_Index = Index : _Count = Count
End Sub
Public ReadOnly Property Index As Integer
Get
Return _Index
End Get
End Property
Public ReadOnly Property Count As Integer
Get
Return _Count
End Get
End Property
Protected Shared Function Parser(cr As CharReader) As Token
Return Nothing
End Function
End Class
Digit Class
Public Class Digit
Inherits Token.Token
Private Sub New(ByVal Index As Integer, Count As Integer)
MyBase.New(Index, Count)
End Sub
Protected Overloads Shared Function Parser(cr As CharReader) As Digit
Dim crc = cr.Current
If crc.HasValue = False Then Return Nothing
Select Case crc.Value
Case "0"c To "9"c
Return New Digit(cr.Index, 1)
Case Else
Return Nothing
End Select
End Function
End Class
Upvotes: 1
Views: 456
Reputation: 245419
That kind of relationship really breaks the OO Concepts. If a parent type provides public functionality, then the child should as well (think Child is a Parent).
The kind of functionality you want can easily be achieved via composition (Digit would contain an instance of Token and proxy whatever calls are necessary to that instance) rather than inheritance.
Upvotes: 2
Reputation: 941377
wouldn't show the Parse Method.
Not sure I follow but you only seem concerned about IntelliSense showing the method. That's easy to solve with EditorBrowsableAttribute:
Imports System.ComponentModel
...
Class Token
Public Shared Sub Parse()
'' etc
End Sub
End Class
Class Digit
Inherits Token
<EditorBrowsable(EditorBrowsableState.Never)> _
Public Shared Shadows Sub Parse()
Token.Parse()
End Sub
End Class
Upvotes: 1
Reputation: 112324
If CharReader
implements IConvertible
, you can convert it to other types with System.Convert
. The standard types like Boolean
, String
, Char
, Int32
, Double
, etc. implement IConvertible
.
Shared Function Parse(Of T As Token)(ByVal CR As CharReader) As T
Return DirectCast(Convert.ChangeType(CR, GetType(T)), T)
End Function
--
Parse
can be hidden by shadowing it with a non public method
Protected Shared Shadows Function Parse(ByVal CR As CharReader) As Digit
^
|
(note the Shadows keyword)
Note that this works only for shared methods, since you access them through the type name and not through an instance.
Upvotes: 0