views:

74

answers:

2

Hi guys,

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} where 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 error using the tablename variable of derived class in base class but i want to use it... coz i dun know other techniques... if other solutions are better then u can post it or u can say how can i make it work? confused...

A: 

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
ho1
is that property shared... I want the shared property
KoolKabin
your script says "Error:: Properties cannot be mustinherit"
KoolKabin
Sorry, it's classes that are MustInherit, it should be MustOverride. I missed the bit about shared, I don't think you can do that then. Though I'd reconsider your design if you're doing inheritance from shared classes, it might be suitable sometimes but I'd think about it carefully.
ho1
The property must be MustOverride and the base class must be MustInherit. Shared properties can't be inherited though, so you're gonna have to let go of that. It doesn't seem to matter much, though. You will use it in an instance anyway, right? Otherwise, consider using Attributes.
Jouke van der Maas
A: 
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
AMissico
sure the second method works but first methods ends up with error calling base class tablename function instead of child class function. But I would prefer first method. Is there any remedy for first method?
KoolKabin
@KookKabin: Second method is so much cleaner, maintainable, and easier to read. Less chance of errors.
AMissico
@KoolKabin: I updated first method, but this is a bad design. Using Shared for this purpose is not advisable because you have "changed" what share/static means for the implementation of this class. In other words, the shared value is no longer shared amoung all classes including the derived classes. Therefore, the programmer must be disciplined to use these classes. Moreover, they many have trouble debugging and reading the code. Based on my experience and knowledge of designing libraries, I strongly suggest second method, but experiment. It is a relatively easy change.
AMissico
thnx amissico I will take care of that...
KoolKabin