开发者

How to use derived class shared variables in shared methods of base class

开发者 https://www.devze.com 2023-01-02 00:30 出处:网络
I am trying to add shared members in derived classes and use that values in base classes... I have base

I am trying to add shared members in derived classes and use that values in base classes...

I have base

class DBLayer
    public shared function GetDetail(byval UIN as integer)
    dim StrSql = string.format("select * from {0} wh开发者_如何转开发ere uin = {1}", tablename, uin)
    ....
    end function
end class

my derived class

class User inherits dblayer
    public shared tabledname as string = "users"
end class

class item inherits dblayer
    public shared tabledname as string = "item"
end class

class category inherits dblayer
    public shared tabledname as string = "category"
end class

Currently there is an error using the tablename variable of a derived class in my base class.


Class User
    Inherits DBLayer
    Public Shared Shadows Function TableName() As String
        Return "users"
    End Function
    Public Overrides Function GetTableName() As String
        Return User.TableName
    End Function
End Class

MustInherit Class DBLayer
    MustOverride Function GetTableName() As String

    Public Function GetDetail(ByVal UIN As Integer)
        Return GetDetail(UIN, GetTableName)
    End Function

    Public Shared Function GetDetail(ByVal UIN As Integer, ByVal TableName As String)
        Dim StrSql As String = String.Format("select * from {0} where uin = {1}", TableName, UIN)
        Return StrSql
    End Function

End Class

Class DBLayer

    Private _tableName As String
    Public Property TableName() As String
        Get
            Return _tableName
        End Get
        Set(ByVal value As String)
            _tableName = value
        End Set
    End Property

    Public Sub New(ByVal tableName As String)
        _tableName = tableName
    End Sub
    Public Function GetDetail(ByVal UIN As Integer)
        Return GetDetail(UIN, Me.TableName)
    End Function

    Public Shared Function GetDetail(ByVal UIN As Integer, ByVal TableName As String)
        Dim StrSql As String = String.Format("select * from {0} where uin = {1}", TableName, UIN)
    End Function

End Class


I'd create

Protected MustInherit Readonly Property TableName as string

in the base class. And then each of the child classes would need to override it and return the correct name from the property.

So in one of the child classes it would be:

Protected OverRides Readonly Property TableName as string
   Get
       Return "users" ' or category or item etc
   End Get
End Property
0

精彩评论

暂无评论...
验证码 换一张
取 消