473,748 Members | 9,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7783
"Bill Nguyen" <bi************ *****@jaco.com> wrote in message news:u$******** *****@TK2MSFTNG P12.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.Constant 1'). 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*******@user s.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%******** ********@tk2msf tngp13.phx.gbl. ..
<al*******@user s.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*******@user s.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

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

Similar topics

20
4501
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 structurally sound to use Application_OnStart. Am I attempting the impossible, and if so, why?
12
3302
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. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would influence the opinions of others, and I really want to know how other people view these things,...
2
2778
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 I can get it in the Login form. In another form , say Employee form , I want to save the Employee details to the Employee table. But when I tried to use connection which is already opened I couldn't get it.
7
2557
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 and read the whole XML document everytime a page is requested. In classic ASP, I used Include files and define constants using Const keyword so that every page that has been included the file can refer the constants.
2
1610
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 day. Obviously there is the application object route. However, because of the new feature of sql server cache invalidation, would it not be best just to create a normal data access layer which can retrieve a datatable containing the constants...
3
2554
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 holds constants (such as XlPattern.XL.....). This must be possible (e.g. as with a typelib) but I can't track it down. Any help appreciated.
34
3383
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 long double const_pi=0.0; lines to // rodata
10
1549
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 thread safety issue. Are consts thread safe? Would the following example create any thread safety issues? Would you recommend using static readonly members instead of constants?
36
2116
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 == 0) \ ({ *(77) = (val); }) \ #preprocessor_else_if (const_index == 1) \ ({ *(99) = (val); }) \
0
8987
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
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
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.