473,406 Members | 2,356 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,406 software developers and data experts.

How to make constants available for the whole application?

I need to make a set of constants to be available for the whole application.
Public const is confined to the form from which constants are declared.
Is there a way to declare once and all forms can access the constant values?
Thanks
Bill
Nov 21 '05 #1
11 7745
"Bill Nguyen" <bi*****************@jaco.com> wrote in message news:u$*************@TK2MSFTNGP12.phx.gbl...
I need to make a set of constants to be available for the whole application.
Public const is confined to the form from which constants are declared.
Is there a way to declare once and all forms can access the constant values?
Thanks
Bill


Place them in a standard module.
Nov 21 '05 #2
"Bill Nguyen" <bi*****************@jaco.com> schrieb:
I need to make a set of constants to be available for the whole
application.
Public const is confined to the form from which constants are declared.
Is there a way to declare once and all forms can access the constant
values?


If you place them in a class, you will have to qualify the constant's names
with the name of the class containing the constant definitions (e.g.
'Form1.Constant1'). Alternatively you can place the constants in a module
in order to be able to access them without explicit qualification.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
By declaring a public constant in a class you can make it visible
anywhere that class is visible, it just needs to be accessed using the
class name

e.g.

public class Widget
public const MAX_SIZE as integer = 17
end class
... < some other code >

if (size < Widget.MAX_SIZE) then
DoSomething
end if
Also, declaring them in a module will make them visible anywhere that
module is visible.

e.g.

public module StdDefs
public const MAX_SIZE as integer = 17
end module

if (size < MAX_SIZE) then
DoSomething
end if

I really dislike the module approach. Some of that is just my Java
background whispering, "Modules, we doan need no stinkin' modules. Just
use classes", but I also dislike the idea of stuffing all the various
constants that one needs for an application into a single std defs
module.

How many constants REALLY need to be accessed everywhere and even those
which do, why are they not associated with classes.

So far I have it possible to always associate constants with
appropriate classes.
Prepending the class name may mean extra typing but so does using
variable names of more that 2 chars in length; the amount of typing
(within reason) should not be the only selection criterion and the
class name usually helps explain the context/use of the constant.

Nov 21 '05 #4
<al*******@users.com> schrieb:
public module StdDefs
public const MAX_SIZE as integer = 17
end module

if (size < MAX_SIZE) then
DoSomething
end if
I really dislike the module approach. Some of that is just my Java
background whispering, "Modules, we doan need no stinkin' modules. Just
use classes", but I also dislike the idea of stuffing all the various
constants that one needs for an application into a single std defs
module.
Well, I dislike the use of interfaces for grouping constants which is often
used in Java because of the /lack of/ semantically more correct modules.
How many constants REALLY need to be accessed everywhere and even those
which do, why are they not associated with classes.


Imagine a bunch of p/invoke declares, type definitions, and constants. IMO
modules are a good solution for grouping these declares, ...

BTW: You can even fully qualify a constant defined in a module.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
Thank you all.
It worked fine for me either way.

Bill
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e%****************@tk2msftngp13.phx.gbl...
<al*******@users.com> schrieb:
public module StdDefs
public const MAX_SIZE as integer = 17
end module

if (size < MAX_SIZE) then
DoSomething
end if
I really dislike the module approach. Some of that is just my Java
background whispering, "Modules, we doan need no stinkin' modules. Just
use classes", but I also dislike the idea of stuffing all the various
constants that one needs for an application into a single std defs
module.


Well, I dislike the use of interfaces for grouping constants which is
often used in Java because of the /lack of/ semantically more correct
modules.
How many constants REALLY need to be accessed everywhere and even those
which do, why are they not associated with classes.


Imagine a bunch of p/invoke declares, type definitions, and constants.
IMO modules are a good solution for grouping these declares, ...

BTW: You can even fully qualify a constant defined in a module.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6
>Imagine a bunch of p/invoke declares, type definitions, and constants. IMO
modules are a good solution for grouping these declares, ...


The point was that such things as p/invoke declares constants, et al.
are generally not used randomly throughout the system, they provide
support for some specified functionality that functionality can be
grouped together within a class.

The usual problem with a general 'definitions and constants' module is
that it becomes a dumping ground for all items that "we don't know
where it should really go", or "we don't have time to sort out at the
moment".

If we don't have just one module and spend time splitting them up in
some useful fashion then why would one not put them into a class.

Nov 21 '05 #7
<al*******@users.com> schrieb:
Imagine a bunch of p/invoke declares, type definitions, and constants.
IMO
modules are a good solution for grouping these declares, ...


The point was that such things as p/invoke declares constants, et al.
are generally not used randomly throughout the system, they provide
support for some specified functionality that functionality can be
grouped together within a class.

The usual problem with a general 'definitions and constants' module is
that it becomes a dumping ground for all items that "we don't know
where it should really go", or "we don't have time to sort out at the
moment".


I don't see any difference in creating a class or module to contain all the
stuff "dumping around". This problem is /not/ related to the existance and
use of modules.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8

The OP was a question on how to make constants available to the whole
program; modules or classes can be used for either. I stated a
personal preference for the classes option to which you replied that
modules were 'sematically more correct'

I am still confused by that comment.

VB.Net is an OO language, it behooves one to use OO designs and not
just cut-and-paste VB6 code, tweaking it until it compiles.

Constants in Modules:
If an application is designed/implemented as a set of inter-relating
classes/objects then there should be no need for 'naked' constants in a
module.

P/Invoke Declares In Modules:
Why should the application code see/know about p/invoke declarations?
The functionality should be wrapped in a class and that class used,
reducing the 'surface area' that the application developer needs to
know/remember.

Classes as Dumping Grounds:
Although one can construct a class to act like a module and then use it
as a dumping ground for misc defs, why would one?

My point was that associating the constants with the appropriate class
is a better option than putting them into a general module, not that
one is incapable of using a class like a dumping ground. Better tools
do not prevent poor design, they just provide and opportunity to do
things better than 'the way we've always done it'

Alan.

Nov 21 '05 #9
On 23 Jun 2005 08:55:45 -0700, al*******@users.com wrote:
My point was that associating the constants with the appropriate class
is a better option than putting them into a general module, not that
one is incapable of using a class like a dumping ground. Better tools
do not prevent poor design, they just provide and opportunity to do
things better than 'the way we've always done it'


I don't understand why you consider it a "better design" to create a class
that does nothing but group a bunch of constants together, even if they are
semantically related, if there is no more substance to the class.

One step further, I fail to see the joy in a class that has no member or
private variables, only shared methods and constants. If I'm calling the
exact same function every time, and it has no internal state of any kind,
and I always have to feed it a full set of arguments, why should it pretend
it's part of a class or an object?
Nov 21 '05 #10
Ross,

"Ross Presser" <rp******@NOSPAMgmail.com.invalid> schrieb:
My point was that associating the constants with the appropriate class
is a better option than putting them into a general module, not that
one is incapable of using a class like a dumping ground. Better tools
do not prevent poor design, they just provide and opportunity to do
things better than 'the way we've always done it'


I don't understand why you consider it a "better design" to create a class
that does nothing but group a bunch of constants together, even if they
are
semantically related, if there is no more substance to the class.

One step further, I fail to see the joy in a class that has no member or
private variables, only shared methods and constants. If I'm calling the
exact same function every time, and it has no internal state of any kind,
and I always have to feed it a full set of arguments, why should it
pretend
it's part of a class or an object?


That's what my concern is about. Classes are IMO a language feature used to
model entities, and thus not intended for /grouping/ objects (functions,
constants, ...) with a loose relationship.

However, I don't think that modules are the best possible solution because
they are automatically imported. I would prefer modules which need to be
imported explicitly or whose members do not always appear in IntelliSense.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #11
I was neccessarily talking about a class that contains only constants
(though that is not intrinically a problem), I was talking about
associating constants with an appropriate class rather than sticking
them in a general holding tank.

Simple example, Client Colors

Say we have an application and want to have all the forms use a given
color scheme (we want a specific one rather than use the Windows
scheme)

Instead of setting them on all the forms at design time (which will be
tedious to change when someone in marketing says, 'Oh! Puce is the new
black!"), we have a set of constants for the colors.
Where do we put them?

We can stick them in a module, or
We can place them in, say, a ClientConfig class along with the default
screen height and width and whatever other values that we reckon are
appropriate for Client Configuration.

Does it have any functionality included? Perhaps not, at first; we have
a fixed set of colors that the user is not allowed to change.

But if at some point we decide to allow the users to customize the
client colors we can add that functionality to this class and with very
little (if any) change to the application code we now have a client
with customizable colors.

I do not see that a module is 'sematically more correct' or even
semantically anything. A module started off life as ' the source code
for a program', evolved into 'a compilation unit' and now to me seems
like a vermiform source file. I personally think they are not useful
but that's a free opinion (and worth every penny)

My comments boil down to

1) A single large source file (be it class or module) for holding all
the constants that may need global scope, is a bad thing.

2) On anecdotal evidence, one a StdDef.bas gets created, everything
gets dumped in there. Almost by reflex. We're short on time, don't
have a chance to work through where a new 'something' should be put.
Stick it here and we'll get back to it later... promise.

3) Constants are given a global scope when they need to used in more
than one place but they can be usually said to 'belong' to a specific
piece of functionality and should be associated with that.

e.g.
ClientConfig.SCREEN_WIDTH
MyApplication.APP_NAME

4) Sort of OT; Low level constants and declares should not be visble
to the application code at all.
P/invoke delcarations, OS constants (say, WS_EX_TOOLWINDOW or
SIZE_MINIMIZED) should be wrapped by a class and that functionality
invoked through the class.
This removes the need for them to be in the global namespace at all.

Alan.

Nov 21 '05 #12

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

Similar topics

20
by: 2obvious | last post by:
I've been trying to create read-only global variables by creating constants (Const) in my global.asa, but I can't seem to reference them. Sticking them in an include works fine, but it seems more...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
2
by: RD | last post by:
My application contains several Classes & Forms . From the Login form I will get the User ID and Password for the SqlConnection. I established the connection through another class and...
7
by: Don Wash | last post by:
Hi There! I'm trying to define constants so that I can refer those constants from any page of my ASP.NET website. I know I can use <appSettings> in web.config XML file but I don't want to parse...
2
by: Steve | last post by:
I am developing an asp.net 2.0 application and have come across the dilemma of how to store constant values, which change infrequently. The website will have tens of thousands of visitors every...
3
by: Lenonardo | last post by:
Can anyone tell me how I refer to Excel constants when using late binding. I've tried searching the Excel.application interface but can't find any reference in the hierarchy to an object that...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
10
by: Paul | last post by:
Hi all, All of the classes in my DAL are static, with constants defining the stored procedures and parameters. I've been having some problems with my site which makes me wonder if there's a...
36
by: anon.asdf | last post by:
Hello! Can the proprocessor make conditional decisions. Here's an example of the functionality (not standard C!) #define PUT_BYTE(const_index, val) \ #preprocessor_if (const_index ==...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
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,...

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.