|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Const Member in Base Class to be overridden in Derived Class?I am trying to figure out how I can define a constant in a base class and override it in a derived class. The following shows a little example ... Module Test Sub Main() Dim A As BaseClass Dim lSelect As Integer = 1 Select Case lSelect Case 0 A = New BaseClass Console.WriteLine(A.CONSTANT) A.print() Case 1 A = New DerivedClassA Console.WriteLine(A.CONSTANT) A.print() Case 2 A = New DerivedClassB Console.WriteLine(A.CONSTANT) A.print() End Select End Sub End Module Public Class BaseClass Public Const CONSTANT As String = "BaseClass" Public Overridable Sub print() Console.WriteLine("BaseClass.print()") End Sub End Class Public Class DerivedClassA Inherits BaseClass Public Shadows Const CONSTANT As String = "DerivedClassA" Public Overrides Sub print() Console.WriteLine("DerivedClassA.print()") End Sub End Class Public Class DerivedClassB Inherits BaseClass Public Shadows Const CONSTANT As String = "DerivedClassB" Public Overrides Sub print() Console.WriteLine("DerivedClassB.print()") End Sub End Class The problem is that no matter what I set lSelect to, it will use the appropriate print() function but not the appropriate CONSTANT. This is part of an effort where we switch the design of one of our utilities to use OOP. The other problem is that we used to have some constants defined in a module. So all the user had to do is use the constant once the namespace was imported. Now it will always require to specify the base class (i.e. A.CONSTANT). Is there a way to do that without the A? Can I make constants from a class visible without the class namespace? Thanks for any feedback! Joe "Joe HM" <unixve***@yahoo.com> schrieb: Use a 'ReadOnly' property instead of the 'Const' declaration.> I am trying to figure out how I can define a constant in a base class > and override it in a derived class. The following shows a little > example ... -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Hello -
Thanks for you reply. I did try the ReadOnly as follows but it is still not working ... Public Class BaseClass Public ReadOnly CONSTANT As String = "BaseClass" Public Overridable Sub print() Console.WriteLine("BaseClass.print()") End Sub End Class Public Class DerivedClassA Inherits BaseClass Public Shadows ReadOnly CONSTANT As String = "DerivedClassA" Public Overrides Sub print() Console.WriteLine("DerivedClassA.print()") End Sub End Class Public Class DerivedClassB Inherits BaseClass Public Shadows ReadOnly CONSTANT As String = "DerivedClassB" Public Overrides Sub print() Console.WriteLine("DerivedClassB.print()") End Sub End Class The following still outputs the CONSTANT from the BaseClass rather than DerivedClassA ... Dim A As BaseClass A = New DerivedClassA Console.WriteLine(A.CONSTANT) A.print() Thanks! Joe "Joe HM" <unixve***@yahoo.com> schrieb: Use a /property/ instead of a constant field:> Thanks for you reply. I did try the ReadOnly as follows but it is > still not working ... > > Public Class BaseClass > Public ReadOnly CONSTANT As String = "BaseClass" \\\ Public Overridable ReadOnly Property Foo() As String Get Return "BaseClass" End Get End Property /// -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Joe HM wrote:
> Hello - Herfried said "use a ReadOnly *property*" :> > Thanks for you reply. I did try the ReadOnly as follows but it is > still not working ... Module Module1 Sub Main() Dim A As BaseClass A = New DerivedClassA Console.WriteLine(A.CONSTANT) A.print() Console.ReadLine() End Sub End Module Public Class BaseClass Public Overridable ReadOnly Property CONSTANT() As String Get Return "BaseClass" End Get End Property Public Overridable Sub print() Console.WriteLine("BaseClass.print()") End Sub End Class Public Class DerivedClassA Inherits BaseClass Public Overrides Sub print() Console.WriteLine("DerivedClassA.print()") End Sub Public Overrides ReadOnly Property CONSTANT() As String Get Return "DerivedClassA" End Get End Property End Class ' ReadOnly makes a Property have a Get but no Set (WriteOnly exists as wel!) -- Larry Lard Replies to group please Hello -
I actually just figured out the perfect solution for my problem ... using a Property ... Public Class BaseClass Private BOX_PROMPTBase As String = "BaseClass" Overridable ReadOnly Property CONST() As String Get Return CONSTBase End Get End Property .... Public Class DerivedClassA Private CONSTANTDerivedClassA As String = "DerivedClassA" Overridable ReadOnly Property CONST() As String Get Return CONSTANTDerivedClassA End Get End Property The only thing I have not figured out is how to avoid having to use the class instance "A" in A.CONST but that is probably something that just has to be that way. Joe "Joe HM" <unixve***@yahoo.com> schrieb: Mark the property as 'Shared'. However, when doing so you cannot override > The only thing I have not figured out is how to avoid having to use the > class instance "A" in A.CONST but that is probably something that just > has to be that way. it any more because shared methods cannot be overridden. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Thanks for the tip! I think I need to be able to override it so I
guess I have to live with the "A.". I actually have another questions regarding properties. I have a function Public Sub print(Optional ByVal aParameter = XXX) where I would like to use a Property for XXX. I would like that XXX will be the overridden Property in the DerivedClassA when print() is called on an instance of DerivedClassA (which does not have print() implemented but inherits it from BaseClass). The compiler tells me that an Optional argument needs to be Const. So is there a way that this can be set at runtime? Thanks a lot for the feedback! Joe "Joe HM" <unixve***@yahoo.com> schrieb: No, that's not possible. However, you could use overloaded methods instead:>The compiler tells me that an > Optional argument needs to be Const. So is there a way that this can > be set at runtime? \\\ Public Overloads Sub Foo() Foo(Me.Bla) ' 'Me.Bla' doesn't need to be a constant. End Sub Public Overloads Sub Foo(ByVal b As Bla) ... End Sub /// -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/>
Sync databound combobox to current bound record in form
Comparing 2 datatables... how do you execute javascript after script is regsistered? Dragging a file from Windows into My program Dynamic Menus with Events Whats this vbScript eval() function equivalent in VB.Net - Dynamically evaluate code ComboBox how to: remove unequal white spaces with trim method Rounded form corners and a custom title bar |
|||||||||||||||||||||||