declaring mustoverride shared constants in an interface


I

Iain Mcleod

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
 
Ad

Advertisements

K

Ken Tucker [MVP]

Hi,

Use a property instead

Public MustOverride Property Test() As String



Ken

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

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
 
I

Iain Mcleod

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
 
D

David Browne

Iain Mcleod said:
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
 
I

Iain Mcleod

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
Ad

Advertisements

D

David Browne

Iain Mcleod said:
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
 
Ad

Advertisements

I

Iain Mcleod

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top