473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

declaring mustoverride shared constants in an interface

Hi
This must be an often encountered problem. I want to declare an abstract
class or an interface with nothing but several static constants so that I
can use polymorphism when I call each of them from my code. My stab at the
problem is shown below. Can anyone suggest what my most efficient workable
solution would be (i.e. I don't want to have to create instances of the
classes as they will only store constant information and I would preferably
like the member variables to be constant, static and not inheritable). They
have to be polymorphic.

The compiler, of course, complains vociferously with the code I have
detailed below due to the fact that you can't declare variables in an
interface, you can't override a constant, you can't declare constants to be
notinheritable... etc etc etc. The list is endless and I am a bad java
programmer trying to do vb.net :-)

I would be very grateful for any suggestions that don't involve giving up
programming.

Many thanks
Iain
Public Interface MyInterface

MustOverride Public Const Shared myVariable as String

End Interface

Public Class MyClass1

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABLE1"

End Class

Public Class MyClass2

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABLE2"

End Class
Nov 21 '05 #1
7 9261
Hi,

Use a property instead

Public MustOverride Property Test() As String

Ken

--------------------

"Iain Mcleod" <mc******@dcs.gla.ac.uk> wrote in message
news:u8*************@TK2MSFTNGP09.phx.gbl...
Hi
This must be an often encountered problem. I want to declare an abstract
class or an interface with nothing but several static constants so that I
can use polymorphism when I call each of them from my code. My stab at the
problem is shown below. Can anyone suggest what my most efficient workable
solution would be (i.e. I don't want to have to create instances of the
classes as they will only store constant information and I would preferably
like the member variables to be constant, static and not inheritable). They
have to be polymorphic.

The compiler, of course, complains vociferously with the code I have
detailed below due to the fact that you can't declare variables in an
interface, you can't override a constant, you can't declare constants to be
notinheritable... etc etc etc. The list is endless and I am a bad java
programmer trying to do vb.net :-)

I would be very grateful for any suggestions that don't involve giving up
programming.

Many thanks
Iain
Public Interface MyInterface

MustOverride Public Const Shared myVariable as String

End Interface

Public Class MyClass1

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABLE1"

End Class

Public Class MyClass2

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABLE2"

End Class

Nov 21 '05 #2
Thanks Ken...

Tried it though.

The problem with properties is that you can't declare them static... When
you use them, you have to create a new instance of MyClass1 and MyClass2
etc...

Cheers
IM

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
Hi,

Use a property instead

Public MustOverride Property Test() As String

Ken

--------------------

"Iain Mcleod" <mc******@dcs.gla.ac.uk> wrote in message
news:u8*************@TK2MSFTNGP09.phx.gbl...
Hi
This must be an often encountered problem. I want to declare an abstract
class or an interface with nothing but several static constants so that I
can use polymorphism when I call each of them from my code. My stab at
the
problem is shown below. Can anyone suggest what my most efficient
workable
solution would be (i.e. I don't want to have to create instances of the
classes as they will only store constant information and I would
preferably
like the member variables to be constant, static and not inheritable).
They
have to be polymorphic.

The compiler, of course, complains vociferously with the code I have
detailed below due to the fact that you can't declare variables in an
interface, you can't override a constant, you can't declare constants to
be
notinheritable... etc etc etc. The list is endless and I am a bad java
programmer trying to do vb.net :-)

I would be very grateful for any suggestions that don't involve giving up
programming.

Many thanks
Iain
Public Interface MyInterface

MustOverride Public Const Shared myVariable as String

End Interface

Public Class MyClass1

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABLE1"

End Class

Public Class MyClass2

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABLE2"

End Class

Nov 21 '05 #3

"Iain Mcleod" <mc******@dcs.gla.ac.uk> wrote in message
news:uN*************@tk2msftngp13.phx.gbl...
Thanks Ken...

Tried it though.

The problem with properties is that you can't declare them static... When
you use them, you have to create a new instance of MyClass1 and MyClass2
etc...


You can't use static methods on an interface. Only an object instance can
implement an interface. Static methods belong to a class, not an instance,
and so cannot be be in an interface.

If you want two classes to have similar static data, you can define a type
to hold the data and give both classes a single static readonly instance of
that data class.

Like this:

Here Foo in an immutable final data class that defines the variables. And
ThisType and ThatType both have a single static instance of Foo. So you can
get to either type's Foo without an object instance.

Option Explicit On
Option Strict On

NotInheritable Class Foo
Public ReadOnly A As String
Public ReadOnly B As String
Public ReadOnly C As String
Public Sub New(ByVal A As String, ByVal B As String, ByVal C As String)
Me.A = A
Me.B = B
Me.C = C
End Sub
End Class

Class ThisType
Public Shared ReadOnly Foo As New Foo("A", "B", "C")

End Class

Class ThatType
Public Shared ReadOnly Foo As New Foo("1", "2", "3")

End Class
Module Module1

Sub Main()
Console.WriteLine(ThisType.Foo.A)
Console.WriteLine(ThatType.Foo.A)
End Sub

End Module

David
Nov 21 '05 #4
Thanks for the response.

It's quite a neat solution, however a new instance of the Foo has to be
created at least once for each ThisType and ThatType that you use. This is
an overhead which could be bad for an ASP.NET application (which is what I
am producing). What are the lifecycle implications for this method (i.e.
when exactly is each Foo initialised and is it re-initialised on every page
hit, or only when the web app is restarted?)

Thanks again
Iain

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:uX**************@TK2MSFTNGP10.phx.gbl...

"Iain Mcleod" <mc******@dcs.gla.ac.uk> wrote in message
news:uN*************@tk2msftngp13.phx.gbl...
Thanks Ken...

Tried it though.

The problem with properties is that you can't declare them static... When
you use them, you have to create a new instance of MyClass1 and MyClass2
etc...


You can't use static methods on an interface. Only an object instance can
implement an interface. Static methods belong to a class, not an
instance, and so cannot be be in an interface.

If you want two classes to have similar static data, you can define a type
to hold the data and give both classes a single static readonly instance
of that data class.

Like this:

Here Foo in an immutable final data class that defines the variables. And
ThisType and ThatType both have a single static instance of Foo. So you
can get to either type's Foo without an object instance.

Option Explicit On
Option Strict On

NotInheritable Class Foo
Public ReadOnly A As String
Public ReadOnly B As String
Public ReadOnly C As String
Public Sub New(ByVal A As String, ByVal B As String, ByVal C As String)
Me.A = A
Me.B = B
Me.C = C
End Sub
End Class

Class ThisType
Public Shared ReadOnly Foo As New Foo("A", "B", "C")

End Class

Class ThatType
Public Shared ReadOnly Foo As New Foo("1", "2", "3")

End Class
Module Module1

Sub Main()
Console.WriteLine(ThisType.Foo.A)
Console.WriteLine(ThatType.Foo.A)
End Sub

End Module

David

Nov 21 '05 #5
Iain,
In addition to the other comments:

Polymorphism (inheritance & implementation) requires an instance of an
object, Constants are Shared (static) which precludes them from being
Polymorphic.

I normally use an Overridable (or MustOverride) Readonly Property to have
"polymorphic constants" Something like:

Public MustInherit Class MyInterface

Public MustOverride ReadOnly Property myVariable() As String

End Class

Public Class MyClass1
Inherits MyInterface

Public Overrides ReadOnly Property myVariable() As String
Get
Return "MY_VARIABLE1"
End Get
End Property

End Class

Public Class MyClass2
Inherits MyInterface

Public Overrides ReadOnly Property myVariable() As String
Get
Return "MY_VARIABLE2"
End Get
End Property

End Class

However! In another post in this thread you have a concern about creating
objects in an ASP.NET environment. Do the above classes behavior other then
the "constansts"?

If they don't I would probably forgo the baseclass & simply create classes
that have constants in them.
Public Class MyClass1

Public Const myVariable As String = "MY_VARIABLE1"

End Class

Public Class MyClass2

Public Const myVariable As String = "MY_VARIABLE2"

End Class
Then when I needed one of the constansts I would simply use it.

Dim y As String = MyClass1.myVariable
Dim y As String = MyClass2.myVariable

It really depends on how I knew to use MyClass1 or MyClass2 specifically or
if it was determined elsewhere (a field in a database & a class factory for
example).

If I had a field in a database & a class factory, I would consider having
the class factory work with Singletons (a Flyweight?) instead of creating a
new instance each time. I would normally make the factory a shared function
in the base class.

Hope this helps
Jay

"Iain Mcleod" <mc******@dcs.gla.ac.uk> wrote in message
news:u8*************@TK2MSFTNGP09.phx.gbl... Hi
This must be an often encountered problem. I want to declare an abstract
class or an interface with nothing but several static constants so that I
can use polymorphism when I call each of them from my code. My stab at
the problem is shown below. Can anyone suggest what my most efficient
workable solution would be (i.e. I don't want to have to create instances
of the classes as they will only store constant information and I would
preferably like the member variables to be constant, static and not
inheritable). They have to be polymorphic.

The compiler, of course, complains vociferously with the code I have
detailed below due to the fact that you can't declare variables in an
interface, you can't override a constant, you can't declare constants to
be notinheritable... etc etc etc. The list is endless and I am a bad java
programmer trying to do vb.net :-)

I would be very grateful for any suggestions that don't involve giving up
programming.

Many thanks
Iain
Public Interface MyInterface

MustOverride Public Const Shared myVariable as String

End Interface

Public Class MyClass1

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABLE1"

End Class

Public Class MyClass2

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABLE2"

End Class

Nov 21 '05 #6

"Iain Mcleod" <mc******@dcs.gla.ac.uk> wrote in message
news:uV**************@TK2MSFTNGP10.phx.gbl...
Thanks for the response.

It's quite a neat solution, however a new instance of the Foo has to be
created at least once for each ThisType and ThatType that you use. This
is an overhead which could be bad for an ASP.NET application (which is
what I am producing). What are the lifecycle implications for this method
(i.e. when exactly is each Foo initialised and is it re-initialised on
every page hit, or only when the web app is restarted?)


The static instance of Foo is created only once: at class load time. It is
part of the class initialization and happens when you first reference the
type. The class stays loaded for the lifespan of the application.

Class load time is almost as good as design time, especially for
applications like ASP.NET which startup infrequently and stay running for a
long time.

David
Nov 21 '05 #7
Thanks guys

I implemented the method that David sent me and it's doing everything that I
could wish for.
If I had a field in a database & a class factory
Jay, how did you know that it was for fields in a database??? (I was looking
for a way of automating the creation of parameters for stored procedures as
it happens) Are you a mind reader as well as a good programmer. LOL!

Much appreciated from all.
Cheers
Iain

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eR*************@TK2MSFTNGP14.phx.gbl... Iain,
In addition to the other comments:

Polymorphism (inheritance & implementation) requires an instance of an
object, Constants are Shared (static) which precludes them from being
Polymorphic.

I normally use an Overridable (or MustOverride) Readonly Property to have
"polymorphic constants" Something like:

Public MustInherit Class MyInterface

Public MustOverride ReadOnly Property myVariable() As String

End Class

Public Class MyClass1
Inherits MyInterface

Public Overrides ReadOnly Property myVariable() As String
Get
Return "MY_VARIABLE1"
End Get
End Property

End Class

Public Class MyClass2
Inherits MyInterface

Public Overrides ReadOnly Property myVariable() As String
Get
Return "MY_VARIABLE2"
End Get
End Property

End Class

However! In another post in this thread you have a concern about creating
objects in an ASP.NET environment. Do the above classes behavior other
then the "constansts"?

If they don't I would probably forgo the baseclass & simply create classes
that have constants in them.
Public Class MyClass1

Public Const myVariable As String = "MY_VARIABLE1"

End Class

Public Class MyClass2

Public Const myVariable As String = "MY_VARIABLE2"

End Class


Then when I needed one of the constansts I would simply use it.

Dim y As String = MyClass1.myVariable
Dim y As String = MyClass2.myVariable

It really depends on how I knew to use MyClass1 or MyClass2 specifically
or if it was determined elsewhere (a field in a database & a class factory
for example).

If I had a field in a database & a class factory, I would consider having
the class factory work with Singletons (a Flyweight?) instead of creating
a new instance each time. I would normally make the factory a shared
function in the base class.

Hope this helps
Jay

"Iain Mcleod" <mc******@dcs.gla.ac.uk> wrote in message
news:u8*************@TK2MSFTNGP09.phx.gbl...
Hi
This must be an often encountered problem. I want to declare an abstract
class or an interface with nothing but several static constants so that I
can use polymorphism when I call each of them from my code. My stab at
the problem is shown below. Can anyone suggest what my most efficient
workable solution would be (i.e. I don't want to have to create instances
of the classes as they will only store constant information and I would
preferably like the member variables to be constant, static and not
inheritable). They have to be polymorphic.

The compiler, of course, complains vociferously with the code I have
detailed below due to the fact that you can't declare variables in an
interface, you can't override a constant, you can't declare constants to
be notinheritable... etc etc etc. The list is endless and I am a bad
java programmer trying to do vb.net :-)

I would be very grateful for any suggestions that don't involve giving up
programming.

Many thanks
Iain
Public Interface MyInterface

MustOverride Public Const Shared myVariable as String

End Interface

Public Class MyClass1

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABLE1"

End Class

Public Class MyClass2

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABLE2"

End Class


Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peter Berry | last post by:
I was surprised and somewhat disappointed by the fact that you cannot declare types in an interface. I discovered this when defining an interface that included events, where the definition of the...
6
by: Simon Harvey | last post by:
Hi, Is there any benefit to declaring variables as constants other than not being able to overwrite them by mistake? Thanks Simon
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
33
by: Joe Fallon | last post by:
1. I have a Base class that has a certain amount of functionality. 2. Then I have a CodeSmith generated class that inherits from the Base class and adds functionality. 3. Since I want to be able...
7
by: Raterus | last post by:
I've got a good question about object-oriented programming, I hope I can explain it well enough. I'm creating a UserManager, which I'm going to inherit from so different applications can share...
2
by: Oenone | last post by:
I could use a little advice to help prevent me making a possible mess of a project. :) In VB6, I once created a project that exposed a public interface class. I then Implemented this in various...
5
by: Eric | last post by:
Hi, I am kind of new to OO and I have the following problem : I have a class containing a huge list of functions for accessing an SQL database. Public Class Transaction #region "Client"...
6
by: wingphil | last post by:
Hi there, I have a shared method in a base class, and I need to know which subclass it has been called from. So for example Public Mustinherit Class BaseClass Public Shared Sub SharedMethod...
3
by: www.gerardvignes.com | last post by:
I do most JavaScript programming with objects and methods. I use tiny boot and shutdown scripts as hooks for the browser load and unload events. My JavaScripts interact with scripts running on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.