Solved

How do you declare a "MustOverride" Constant?

Posted on 2010-11-15
6
892 Views
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.

Suggestions?
0
Comment
Question by:Javin007
  • 3
  • 3
6 Comments
 
LVL 4

Author Comment

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

Accepted Solution

by:
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
        Get
            Return _foo
        End Get
    End Property
End Class

Open in new window

0
 
LVL 4

Author Comment

by:Javin007
ID: 34145821
Bummer.  This is considered the "right" way to do it?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 74

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!
0
 
LVL 74

Assisted Solution

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

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

Author Closing Comment

by:Javin007
ID: 34148705
Thanks!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now