Base class methods?

Is there a way just for the base class?

I have work on using the module, but this separates the functionality that will be used by the base class.

I was thinking about the line 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.

does not display the Parse method.

How can I do that? (If possible at all)

EDIT

Current implementations It really should be a base class method in a 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 class (base)

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

Digits 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
+1
source share
3 answers

Such relationships really violate the concept of OO. If the parent type provides publicly available functionality, then the child should also (think that the child is the parent).

, , ( Token , ), .

+2

Parse.

, , , IntelliSense . 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
+1

CharReader IConvertible, System.Convert. , Boolean, String, Char, Int32, Double .., 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 ,

Protected Shared Shadows Function Parse(ByVal CR As CharReader) As Digit
                    ^
                    |
            (note the Shadows keyword)

Note that this only works for generic methods, since you access them through the type name, not through the instance.

0
source

All Articles