473,236 Members | 1,771 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,236 software developers and data experts.

Where is best place to define hard coded values?

If I have many hard coded values such as file paths, file names, timeouts,
etc, where is the best place to define them? Meaning, in the case something
needs changing for example, rather than running down all the subs or
functions that may contain these values, I'd like one place to change them
and have that changed reflected in the subs or functions that use those
values. I'd like to avoid globals; keeping the values private to only those
subs or functions using them.

Thanks,
Brett
Nov 21 '05 #1
10 2788
Hi,

I would make a variable that is private to the class or form.
That way all the sub routines and functions can use it. It wont be
accessable out side the form or class.

Ken
-----------
"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If I have many hard coded values such as file paths, file names, timeouts,
etc, where is the best place to define them? Meaning, in the case something
needs changing for example, rather than running down all the subs or
functions that may contain these values, I'd like one place to change them
and have that changed reflected in the subs or functions that use those
values. I'd like to avoid globals; keeping the values private to only those
subs or functions using them.

Thanks,
Brett

Nov 21 '05 #2
Nice. Thanks.

Brett
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:Op***************@TK2MSFTNGP15.phx.gbl...
Hi,

I would make a variable that is private to the class or form.
That way all the sub routines and functions can use it. It wont be
accessable out side the form or class.

Ken
-----------
"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If I have many hard coded values such as file paths, file names, timeouts,
etc, where is the best place to define them? Meaning, in the case
something
needs changing for example, rather than running down all the subs or
functions that may contain these values, I'd like one place to change them
and have that changed reflected in the subs or functions that use those
values. I'd like to avoid globals; keeping the values private to only
those
subs or functions using them.

Thanks,
Brett

Nov 21 '05 #3
There will be some shared variables mixed in. Meaning, I'll define
variables in one class that will also be used in other classes or forms.
For those shared variables, should I just create a class that only houses
them? This provides one place to change them and have those changes cascade
through out forms/classes that are using them.

Brett

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

I would make a variable that is private to the class or form.
That way all the sub routines and functions can use it. It wont be
accessable out side the form or class.

Ken
-----------
"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If I have many hard coded values such as file paths, file names, timeouts,
etc, where is the best place to define them? Meaning, in the case
something
needs changing for example, rather than running down all the subs or
functions that may contain these values, I'd like one place to change them
and have that changed reflected in the subs or functions that use those
values. I'd like to avoid globals; keeping the values private to only
those
subs or functions using them.

Thanks,
Brett

Nov 21 '05 #4

"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If I have many hard coded values such as file paths, file names, timeouts,
etc, where is the best place to define them? Meaning, in the case something needs changing for example, rather than running down all the subs or
functions that may contain these values, I'd like one place to change them
and have that changed reflected in the subs or functions that use those
values. I'd like to avoid globals; keeping the values private to only those subs or functions using them.

Thanks,
Brett


In situations like this - what do you think of including the file paths,
file names, timeouts, and those hard-code values in a separate text file you
can read at run-time.
The program would not need to be modified when things change - just change
the values in the file.
Nov 21 '05 #5

"Hal Rosser" <hm******@bellsouth.net> wrote in message
news:1D**************@bignews2.bellsouth.net...

"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If I have many hard coded values such as file paths, file names,
timeouts,
etc, where is the best place to define them? Meaning, in the case

something
needs changing for example, rather than running down all the subs or
functions that may contain these values, I'd like one place to change
them
and have that changed reflected in the subs or functions that use those
values. I'd like to avoid globals; keeping the values private to only

those
subs or functions using them.

Thanks,
Brett


In situations like this - what do you think of including the file paths,
file names, timeouts, and those hard-code values in a separate text file
you
can read at run-time.
The program would not need to be modified when things change - just change
the values in the file.


Is this how it is normally done?

This file would only need to be read the first time the program loads
correct? It will also only be for globals right?

Thanks,
Brett
Nov 21 '05 #6

"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"Hal Rosser" <hm******@bellsouth.net> wrote in message
news:1D**************@bignews2.bellsouth.net...

"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If I have many hard coded values such as file paths, file names,
timeouts,
etc, where is the best place to define them? Meaning, in the case

something
needs changing for example, rather than running down all the subs or
functions that may contain these values, I'd like one place to change
them
and have that changed reflected in the subs or functions that use those
values. I'd like to avoid globals; keeping the values private to only

those
subs or functions using them.

Thanks,
Brett


In situations like this - what do you think of including the file paths,
file names, timeouts, and those hard-code values in a separate text file
you
can read at run-time.
The program would not need to be modified when things change - just change the values in the file.


Is this how it is normally done?

This file would only need to be read the first time the program loads
correct? It will also only be for globals right?

Thanks,
Brett

Can be used for anything that has to be hard-coded.
Some do something like this - for Sales Tax Rate and the like
- If the rate changes - someone can change the rate in the file - and no
re-programming is required for a change in rates.
A separate file for each piece of data is one way to do it -
frinstance a file named "stateTaxRate.dat" can have one entry like ".075".
When the state changes the rate - tell the user to change the file entry to
".085" - or whatever the new rate is.

Nov 21 '05 #7

"Hal Rosser" <hm******@bellsouth.net> wrote in message
news:Uw*****************@bignews6.bellsouth.net...

"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"Hal Rosser" <hm******@bellsouth.net> wrote in message
news:1D**************@bignews2.bellsouth.net...
>
> "Brett" <no@spam.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
>> If I have many hard coded values such as file paths, file names,
>> timeouts,
>> etc, where is the best place to define them? Meaning, in the case
> something
>> needs changing for example, rather than running down all the subs or
>> functions that may contain these values, I'd like one place to change
>> them
>> and have that changed reflected in the subs or functions that use
>> those
>> values. I'd like to avoid globals; keeping the values private to only
> those
>> subs or functions using them.
>>
>> Thanks,
>> Brett
>>
>>
>
> In situations like this - what do you think of including the file
> paths,
> file names, timeouts, and those hard-code values in a separate text
> file
> you
> can read at run-time.
> The program would not need to be modified when things change - just change > the values in the file.


Is this how it is normally done?

This file would only need to be read the first time the program loads
correct? It will also only be for globals right?

Thanks,
Brett

Can be used for anything that has to be hard-coded.
Some do something like this - for Sales Tax Rate and the like
- If the rate changes - someone can change the rate in the file - and no
re-programming is required for a change in rates.
A separate file for each piece of data is one way to do it -
frinstance a file named "stateTaxRate.dat" can have one entry like ".075".
When the state changes the rate - tell the user to change the file entry
to
".085" - or whatever the new rate is.

I see. I suppose you could build that into the interface for the user to
make changes as well right?

Using the text files for many variables, will that be a bunch of parsing -
instr(), mid(), perhaps regular expressions and the like in other words?
They just don't seem like the most efficient method. I guess they could be
seperated by chr(13) & chr(10).

Would an XML file work better?

Thanks,
Brett
Nov 21 '05 #8
Options may be: XML Resource File, Small DataBase, Serialized Class, etc.

"Brett" wrote:

"Hal Rosser" <hm******@bellsouth.net> wrote in message
news:Uw*****************@bignews6.bellsouth.net...

"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"Hal Rosser" <hm******@bellsouth.net> wrote in message
news:1D**************@bignews2.bellsouth.net...
>
> "Brett" <no@spam.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
>> If I have many hard coded values such as file paths, file names,
>> timeouts,
>> etc, where is the best place to define them? Meaning, in the case
> something
>> needs changing for example, rather than running down all the subs or
>> functions that may contain these values, I'd like one place to change
>> them
>> and have that changed reflected in the subs or functions that use
>> those
>> values. I'd like to avoid globals; keeping the values private to only
> those
>> subs or functions using them.
>>
>> Thanks,
>> Brett
>>
>>
>
> In situations like this - what do you think of including the file
> paths,
> file names, timeouts, and those hard-code values in a separate text
> file
> you
> can read at run-time.
> The program would not need to be modified when things change - just

change
> the values in the file.

Is this how it is normally done?

This file would only need to be read the first time the program loads
correct? It will also only be for globals right?

Thanks,
Brett

Can be used for anything that has to be hard-coded.
Some do something like this - for Sales Tax Rate and the like
- If the rate changes - someone can change the rate in the file - and no
re-programming is required for a change in rates.
A separate file for each piece of data is one way to do it -
frinstance a file named "stateTaxRate.dat" can have one entry like ".075".
When the state changes the rate - tell the user to change the file entry
to
".085" - or whatever the new rate is.

I see. I suppose you could build that into the interface for the user to
make changes as well right?

Using the text files for many variables, will that be a bunch of parsing -
instr(), mid(), perhaps regular expressions and the like in other words?
They just don't seem like the most efficient method. I guess they could be
seperated by chr(13) & chr(10).

Would an XML file work better?

Thanks,
Brett

Nov 21 '05 #9
I'm surprised no-one here has mentioned App.config yet (or did I miss it?)

Right click on your project in the solution explorer and choose to add a new
item. From the list of items that you can add to the project, scroll down
and find the one called ApplicationConfiguration File. Select it, call it
app.config and click Open to add it to the file.

In the file you just added, drop in an <appSettings> section and in that add
your values, like this
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="DirectoryName" value="c:\test"/>

</appSettings>

</configuration>

Here I've addded in a single entry called DirectoryName, with a value of
"c:\test"

Now, to get at that, and obviously the whole mass of other values you want
to store, just go ahead and create a class with some shared properties on
it, like this.

Imports System.Configuration

Public Class AppSettings

Public Shared ReadOnly Property DirectoryName() As String

Get

Return ConfigurationSettings.AppSettings("DirectoryName")

End Get

End Property

End Class

If I ever want to pull a value out of that app.settings file now I just call
AppSettings.whateverpropertyyoucoded up

Hope that helps,
--
Pete Wright
Author of ADO.NET Novice to Pro for Apress
www.petewright.org

"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If I have many hard coded values such as file paths, file names, timeouts,
etc, where is the best place to define them? Meaning, in the case
something needs changing for example, rather than running down all the
subs or functions that may contain these values, I'd like one place to
change them and have that changed reflected in the subs or functions that
use those values. I'd like to avoid globals; keeping the values private
to only those subs or functions using them.

Thanks,
Brett

Nov 21 '05 #10
Pete,
Depending on the nature of the (original) coded values, I would consider
creating custom a configuration section in the app.config rather then
directly in appSettings, as this allows better encapsulation of the coded
values...

Create a custom configuration section via configSections and implementing
the System.Configuration.IConfigurationSectionHandler. I would pattern the
custom section handler after DictionarySectionHandler (support: add, remove,
clear) or use a class derived from DictionarySectionHandler...

<navigation>
<add form="Form 1" type="namespace.form1, assembly" />
<add form="Form 2" type="namespace.form2, assembly" />
</navigation>

Then within your code you can use ConfigurationSettings.GetConfig to return
the above section of the app.config. Depending on what you do in your
IConfigurationSectionHandler.Create method (I would create a HashTable) will
decide what GetConfig returns.

See the following on how to create new sections via the configSections
section.

http://msdn.microsoft.com/library/de...onhandlers.asp

and:
http://msdn.microsoft.com/library/de...ionsschema.asp

Also read about the System.Configuration.ConfigurationSettings class and
other classes in the System.Configuration namespace.
Hope this helps
Jay

"Pete Wright" <pe**********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I'm surprised no-one here has mentioned App.config yet (or did I miss it?)

Right click on your project in the solution explorer and choose to add a
new item. From the list of items that you can add to the project, scroll
down and find the one called ApplicationConfiguration File. Select it,
call it app.config and click Open to add it to the file.

In the file you just added, drop in an <appSettings> section and in that
add your values, like this
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="DirectoryName" value="c:\test"/>

</appSettings>

</configuration>

Here I've addded in a single entry called DirectoryName, with a value of
"c:\test"

Now, to get at that, and obviously the whole mass of other values you want
to store, just go ahead and create a class with some shared properties on
it, like this.

Imports System.Configuration

Public Class AppSettings

Public Shared ReadOnly Property DirectoryName() As String

Get

Return ConfigurationSettings.AppSettings("DirectoryName")

End Get

End Property

End Class

If I ever want to pull a value out of that app.settings file now I just
call AppSettings.whateverpropertyyoucoded up

Hope that helps,
--
Pete Wright
Author of ADO.NET Novice to Pro for Apress
www.petewright.org

"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If I have many hard coded values such as file paths, file names,
timeouts, etc, where is the best place to define them? Meaning, in the
case something needs changing for example, rather than running down all
the subs or functions that may contain these values, I'd like one place
to change them and have that changed reflected in the subs or functions
that use those values. I'd like to avoid globals; keeping the values
private to only those subs or functions using them.

Thanks,
Brett


Nov 21 '05 #11

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

Similar topics

11
by: GhislainTanguay | last post by:
Hi everyone, i work on a ASP (Not .NET but version 3) project. This project is on our server for development. I use SQL-SERVER, IIS and VB for DLL. On the server i have create a folder with my...
97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
1
by: Rasika Wijayaratne | last post by:
Hello, Can I get feedback on these .NET coding best practices please. Thanks to Konrad Rudolph and Jon Skeet for the replies to my previous post on 'Writing Properties.' Thanks in advance to all...
6
by: Andrea Williams | last post by:
Where is the best place to put global variables. In traditional ASP I used to put all of them into an include file and include it in every page. Will the Global.aspx.cs do that same thing? ...
8
by: Charlie | last post by:
Hi: My ASP.NET app opens and closes database connection in several pages. Rather than hard coding string into every page, is there a place I can put it where all pages will be able to access it....
5
by: Janaka | last post by:
I'm making a web application on my local server which will then be rolled out onto our live site. My problem is that i tend to have to comment out and make a few new hard-coded lines in my files...
10
by: Mike Logan | last post by:
I am using the "contract first" design methodology. Contract First is design the WSDL first then design the server and client. However I must design my XSD/XML Schema before anything. I am...
9
by: ward | last post by:
Good morning. I created an edit_task page that allows the user to edit tasks for the database. I entered some text and used some hard returns. Put when I went to view the tasks (using a PHP...
21
by: John Salerno | last post by:
If I want to make a list of four items, e.g. L = , and then figure out if a certain element precedes another element, what would be the best way to do that? Looking at the built-in list...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.