• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1024
  • Last Modified:

How do you declare a "MustOverride" Constant?

Okay, I'm painfully new to TRUE OOP programming, and am working with inheritance.  I have a constant that I want each child class to be forced to set, so that in the base class a common method can be run that uses the value as set by its children.

I've tried the following:

Protected MustOverride Const Foo As Long

But needless to say, VB.Net no likey.  

I've come to the conclusion that I can create a regular variable in the base class, then make a required sub that "sets" the value of the variable, but this definitely feels like a hack.

Suggestions?
0
Javin007
Asked:
Javin007
  • 3
  • 3
3 Solutions
 
Javin007Author Commented:
Update:  I've now found that I can make a "MustInherit" ReadOnly Property, but that still also feels hackish.
0
 
käµfm³d 👽Commented:
You can declare a MustOverride ReadOnly property on your base class (and define the class as MustInherit). Then, in your child class, implement the property. Here is an example:
Public MustInherit Class BaseClass
    Public MustOverride ReadOnly Property Foo() As Long
End Class



Public Class Child1
    Inherits BaseClass

    Private Const _foo As Long = 10

    Public Overrides ReadOnly Property Foo() As Long
        Get
            Return _foo
        End Get
    End Property
End Class

Open in new window

0
 
Javin007Author Commented:
Bummer.  This is considered the "right" way to do it?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
käµfm³d 👽Commented:
AFIK, yes, because MustOverride only applies to properties and procedures. You are still accomplishing the same goal provided you keep the ReadOnly keyword in there. Of course, it wouldn't make sense to have a "setter" point to a const private member anyways!
0
 
käµfm³d 👽Commented:
P.S.

This isn't necessarily a restriction of OOP--more likely it is a restriction of VB.NET.
0
 
Javin007Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now