473,573 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_VARIABL E1"

End Class

Public Class MyClass2

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABL E2"

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

Use a property instead

Public MustOverride Property Test() As String

Ken

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

"Iain Mcleod" <mc******@dcs.g la.ac.uk> wrote in message
news:u8******** *****@TK2MSFTNG P09.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_VARIABL E1"

End Class

Public Class MyClass2

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABL E2"

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***@bellsout h.net> wrote in message
news:%2******** **********@TK2M SFTNGP11.phx.gb l...
Hi,

Use a property instead

Public MustOverride Property Test() As String

Ken

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

"Iain Mcleod" <mc******@dcs.g la.ac.uk> wrote in message
news:u8******** *****@TK2MSFTNG P09.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_VARIABL E1"

End Class

Public Class MyClass2

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABL E2"

End Class

Nov 21 '05 #3

"Iain Mcleod" <mc******@dcs.g la.ac.uk> wrote in message
news:uN******** *****@tk2msftng p13.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.WriteLi ne(ThisType.Foo .A)
Console.WriteLi ne(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" <davidbaxterbro wne no potted me**@hotmail.co m> wrote in
message news:uX******** ******@TK2MSFTN GP10.phx.gbl...

"Iain Mcleod" <mc******@dcs.g la.ac.uk> wrote in message
news:uN******** *****@tk2msftng p13.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.WriteLi ne(ThisType.Foo .A)
Console.WriteLi ne(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
"polymorphi c 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_VARIABL E1"
End Get
End Property

End Class

Public Class MyClass2
Inherits MyInterface

Public Overrides ReadOnly Property myVariable() As String
Get
Return "MY_VARIABL E2"
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_VARIABL E1"

End Class

Public Class MyClass2

Public Const myVariable As String = "MY_VARIABL E2"

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

Dim y As String = MyClass1.myVari able
Dim y As String = MyClass2.myVari able

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.g la.ac.uk> wrote in message
news:u8******** *****@TK2MSFTNG P09.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_VARIABL E1"

End Class

Public Class MyClass2

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABL E2"

End Class

Nov 21 '05 #6

"Iain Mcleod" <mc******@dcs.g la.ac.uk> wrote in message
news:uV******** ******@TK2MSFTN GP10.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******** *****@TK2MSFTNG P14.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
"polymorphi c 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_VARIABL E1"
End Get
End Property

End Class

Public Class MyClass2
Inherits MyInterface

Public Overrides ReadOnly Property myVariable() As String
Get
Return "MY_VARIABL E2"
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_VARIABL E1"

End Class

Public Class MyClass2

Public Const myVariable As String = "MY_VARIABL E2"

End Class


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

Dim y As String = MyClass1.myVari able
Dim y As String = MyClass2.myVari able

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.g la.ac.uk> wrote in message
news:u8******** *****@TK2MSFTNG P09.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_VARIABL E1"

End Class

Public Class MyClass2

Implements MyInterface

Public Overrides NotInheritable Shared Const myVariable As String =
"MY_VARIABL E2"

End Class


Nov 21 '05 #8

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

Similar topics

5
2821
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 delegates had to be placed outside the definition of the interface. To me, the delegate definition is an integral part of the interface definition. Also,...
6
1288
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
2840
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. '**************************************************************************** ' Issues '****************************************************************************
33
3455
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 to re-generate the classes on level 2 I have a 3rd level that inherits from them and implements specific hand coded methods. 4. My colleague asked...
7
1875
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 common methods. Public MustInherit Class UserManager Public MustOverride Sub AddUser(usr as InternetUser) End Class Public Class InternetUser...
2
1768
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 plug-in DLLs so that I could early-bind to the plug-ins by declaring objects of the interface class type. This worked fine, until one day I found...
5
1339
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" Public function GetClientInfos() as Dataset ... Public sub InsertClientInfos() ...
6
1495
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 ... End Sub
3
1804
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 the web server. I need to declare shared values (as constants) to assist communication between client and server. I declare these constants using...
0
7760
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7996
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7760
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8049
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5573
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5284
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3724
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2185
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.