Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Shared constants
Message
From
15/08/2005 06:36:37
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
14/08/2005 14:51:52
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01040828
Message ID:
01040896
Views:
53
This message has been marked as the solution to the initial question of the thread.
>How can I define constants that are shared amongst several forms? In the same project, for a start.
>
>For example, I have several forms that access different tables, in the same Access database, so for simplicity, I would like to have a shared constant for a connection string (so that I can quick switch between two different connection strings between home and the academy where I am learning .NET).
>
>TIA,
>
>Hilmar.

Hilmar,
Besides ConfigurationSettings there is of course const class member. You could have your constants in a namespace like:
namespace myApplication
{
 class definitions
 {
  public const string myConStr = "...";
 }
}
and refer to it as:
myApplication.definitions.myConStr
from anywhere. However like VFP if you change the value of myConStr, any other classes depending on the value should be recompiled. An alternative is readonly fields.
namespace myApplication
{
 class definitions
 {
  public static readonly string myConStr = "...";
 }
}
you again refer to it as:
myApplication.definitions.myConStr

However if value is changed it's only 'definitions' class that you need a recompile (other classes obtain value at runtime despite compile time if it's a field and not a constant). Also with constants and fields you're not limited to simple types and could get the value using a method (and say read from a txt,xml file or ask to user etc). ie:
namespace myApplication
{
 class definitions
 {
  public static readonly DataLocation myDataLoc = new DataLocation();
 }
 class DataLocation
 {
   // fields    
   public DataLocation() {...}
 }
}
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform