Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Global Variables C#
Message
De
12/12/2009 04:14:08
 
 
À
11/12/2009 18:02:10
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01438724
Message ID:
01438748
Vues:
62
>What's the best way to handle global variables that are established when the app startes - e.g. user id, company id, connection string, etc in a Windows form or WPF application as the app moves from form to form?
>I've been rolling properties forward from one form to the other and it seems that must be a better way?
>I read somewhere about copying a dictionary and that seemed to make sense.
>Any suggestions?

Where the settings are not known at compile time I favour a static class. Something like:
    public static class AppSettings
    {
        public static readonly string ConnectionString;

        static AppSettings()
        {
            ConnectionString = GetConnectionString();
        }

        static string GetConnectionString()
        {
            //Some real method to lookup the string
            return "SQL...";
        }
    }
Using this then the first time AppSettings.ConnectionString is referenced anywhere in code the static constructor runs automtically and sets the ConnectionString value (note that although marked readonly the ConnectionString value can be set via the constructor).

If you may later want to change the ConnectionString value whilst the app is running then use it without the readonly modifier.....

HTH,
Viv
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform