开发者

Can I override variables declared in a class using VB.NET?

I'm trying to create a generic Controller Class for generic events. But these these events still need to access their class variable. So my idea is to create the Base Controller Class with a ModelBaseclass variable as _ClassVar, which is inherited by all of the other class Controller classes will derive from. But I want the derived controller classes to override the _ClassVar with whichever one they need.

I want to do this, so the ControllerBaseClass can have all the generic functions that all the Derived Classes would use.

Model :

Public Class ModelBaseClass
    Public Overridable Function Foo() As String
        Return "Name"
    End Function
End Class

Public Class ModelDerivedClass1
    Inherits ModelBaseClass
    Public Overrides Function Foo() As String
        Return "ModelDerivedClass1"
    End Function
End Class

Public Class ModelDerivedClass2
    Inherits ModelBaseClass
    Public Overrides Function Foo() As String
        Return "ModelDerivedClass2"
    End Function
End Class

Controller :

Public Class ControllerBase
           Public Overridable _ClassVar As ModelBaseClass       <----
    Public Function PrintFoo() As String                        
        Return _ClassVar.Foo()
    End Function
End Class

Public Class ControllerDerivedClass1
    Inherits ControllerBase
         Public Overrides _ClassVar As ModelDerivedClas开发者_StackOverflow社区s1       <---- 
End Class

Public Class ControllerDerivedClass2
    Inherits ControllerBase
         Public Overrides _ClassVar As ModelDerivedClass2       <----  
End Class


Thanks to polymorphism, there's no reason you should have to do this.

Both ModelDerivedClass1 and ModelDerivedClass2 inherit from ModelBaseClass, so declaring the variable in your base class as type ModelBaseClass will allow you to store an object of any of those types. Any method that is defined in the base type ModelBaseClass is automatically available on the derived types. If you need to call a method that is only defined in one of the derived types, you'll need to upcast the object to a more specific type:

DirectCast(_ClassVar, ModelDerivedClass1)


As a more specific and literal answer to your question, you can't override variable declarations. You'd have to declare them as properties in order to override their declarations, and even then, you couldn't override exclusively by return type.


How about using Generics for this one:

Public Class ControllerBase(Of T As ModelBaseClass)
    Private _ClassVar As T
    Public Property ClassVar() As T
        Get
            Return _ClassVar
        End Get
        Set(ByVal value As T)
            _ClassVar = value
        End Set
    End Property

    Public Function PrintFoo() As String
        Return ClassVar.Foo()
    End Function
End Class

Public Class ControllerDerivedClass1
    Inherits ControllerBase(Of ModelDerivedClass1)
    ' No need to declare this
    ' Public Overrides _ClassVar As ModelDerivedClass1       <----  
End Class

Public Class ControllerDerivedClass2
    Inherits ControllerBase(Of ModelDerivedClass2)
    ' No need to declare this
    ' Public Overrides _ClassVar As ModelDerivedClass2       <----  
End Class
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜