How do you declare a "MustOverride" Constant?

Posted on 2010-11-15
Last Modified: 2012-05-10
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.

Question by:Javin007
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Author Comment

ID: 34140955
Update:  I've now found that I can make a "MustInherit" ReadOnly Property, but that still also feels hackish.
LVL 75

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 34140963
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
            Return _foo
        End Get
    End Property
End Class

Open in new window


Author Comment

ID: 34145821
Bummer.  This is considered the "right" way to do it?
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 34147162
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!
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 34147172

This isn't necessarily a restriction of OOP--more likely it is a restriction of VB.NET.

Author Closing Comment

ID: 34148705

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to make search between pages 17 57
Error building VS2105 solution from repository 1 56
"lblTime is not declared" 3 63
How do i create a simple array in a public class? 6 35
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question