Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do you declare a "MustOverride" Constant?

Posted on 2010-11-15
6
Medium Priority
?
955 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
[X]
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
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 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 2000 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 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 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