开发者

c# Consts in a public class

I have a class below:

I want to access these default strings but C# compiler doesn't like combining Const to create a Const.

public class cGlobals
{
    // Some Default Values

    public class Client
    {
        public const string DatabaseSDF = "database.sdf";
        publi开发者_如何学Pythonc const string DatabaseDir = "database";
        public const string DatabaseFullLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                                       DatabaseDir);
        public const string DataSource = Path.Combine(DatabaseDir, DatabaseSDF);
    }
}

Is there a better way instead of hard coding the strings? I want to make use of the Special Folders and Path.Combine.

Thanks


You must use static readonly instead of const, since const have to be a constant at compile-time.

Also, constants will actually be compiled into assemblies that are using them, so if you are referencing those fields from other assemblies you would have to recompile them if you changed the constants. This doesn't happen with static readonly fields. So either way, it's a better idea :)

I actually asked about this a while ago and I would recommend reading it and the accepted answer: static readonly vs const.


For a variable to be declared const, the assigned value has to be a compile-time constant; to use the result of a method call you need to change your variable declaration:

public static readonly string DataSource = ...;

If you think about it, this isn't a compile-time constant, in that it will give different results based on which OS you run it on. It's constant within a single execution but not a "general" constant.


Just to add to the other correct answers: a constant should logically be something that is unchanging for all time and all places. Things like the number of eggs in a dozen, the atomic number of lead, and so on. Values that change over time, like the number of countries in the European Union, or the price of a troy ounce of gold in Australian dollars, should not be modeled as constants.

In your case, the value is not logically constant for all time and all places, so don't try to use a constant.


You can use readonly instead of const.

You could also look at using app.config to store your configuration settings.


I'm afraid you will have to use

static public readonly string DatabaseFullLocation = Path.Combine(/* ... */);

instead;


Personally, I would not hard code the strings at all.

I'd put them into appSettings.

Why? Well, if you need to change those values, you'll need to recompile your code. Putting them in as appSettings will allow you to just change your config file.

You can still provide an accessor through your class.

public string DatabaseSDF 
{
  get
  {
    return System.Configuration.ConfigurationManager.AppSettings["DatabaseSDF"];
  }
}

Best of both worlds, imo.


const types have to be identified at compile time, in your code you're trying to call Path.Combine at run-time to figure out the actual path. And you cannot update const at runtime.


In Application Settings you can keep these kind of Constants .

In project properties ,select the settings tab. In the grid you can set your constants.

You can get as well as set these variables even during runtime.

 Message.show(My.Settings.YourVariable); //you will get the value

and set by

 My.Settings.YourVariable="Vibin";
    My.Settings.Save()

I think this is one of the best way.


  1. Define a class lets say GetDirectory.
  2. Make all methods static.
  3. Define a class for constants. delcate all variables with --> public static readonly string Name_Of_Var = "Any value"

  4. From GetDirectory methods - Define the code for folder you need to access Ex: Path.Combine(GetFolderPath(Environment.SpecialFolder. "Select name of folder"), name of constants)

Done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜