Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Shared constants
Message
De
15/08/2005 06:36:37
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
14/08/2005 14:51:52
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01040828
Message ID:
01040896
Vues:
52
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform