C # Consts in open class

I have a class below:

I want to access these lines by default, but the C # compiler does not like to combine Const to create Const.

public class cGlobals { // Some Default Values public class Client { public const string DatabaseSDF = "database.sdf"; public 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 strings? I want to use special folders and Path.Combine.

thanks

+4
source share
9 answers

You should use static readonly instead of const , since const must be constant at compile time.

In addition, the constants will actually be compiled into collections that use them, so if you reference these fields from other assemblies, you will have to recompile them if you change the constants. This does not happen with static readonly fields. So, anyway, this is the best idea :)

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

+8
source

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

 public static readonly string DataSource = ...; 

If you think about it, this is not a compile-time constant, as it will give different results based on which OS you run it on. It is a constant within the same execution, but it is not a โ€œgeneral" constant.

+4
source

Just add other correct answers: the constant should logically be that which is invariable for all time and in all places. Things like the number of eggs in the top ten, the atomic number of lead, etc. Values โ€‹โ€‹that change over time, such as 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 at all times and in all places, so do not try to use a constant.

+4
source

You can use readonly instead of const .

You can also look at app.config to save your configuration settings.

+3
source

I'm afraid you will have to use

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

instead

+2
source

Personally, I would not hardcode the strings.

I would put them in appSettings.

Why? Well, if you need to change these values, you will need to recompile your code. Entering them into the application, you can simply change your configuration file.

You can still provide the accessory through your class.

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

The best of both worlds, imo.

+2
source

const types must be identified at compile time, in your code that you are trying to call Path.Combine at runtime to figure out the actual track. And you cannot update const on runtime.

+1
source

In the application settings you can save such constants.

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

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

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

and install

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

I think this is one of the best ways.

0
source
  • A class definition allows you to use GetDirectory.
  • Make all methods static.
  • Define a class for constants. share all variables with -> public static readonly string Name_Of_Var = "Any value"

  • From the GetDirectory methods. Define the code for the folder you want to access. Example: Path.Combine (GetFolderPath (Environment.SpecialFolder.) Select the folder name "), the name of the constant)

Done.

0
source

Source: https://habr.com/ru/post/1298982/


All Articles