Regarding hard code in C#

hi,

I am developing a web-based application which needs lot of conditions to be checked .

For Example : if x=5 ... instead of hard coding the value 5 ...is there any method so that i can access the hardcoded values globally in my entire application.

In Asp we can use .ini file but .net has left .ini file .

Please suggest me the right method ..to overcome this problem




Answer this question

Regarding hard code in C#

  • .NetProgrammer

    The above suggested solutions can be used. It basically depends on where you want to retrieve this information for. Ideal would be to combine both given solutions.

    For example you create a Configuration class for example a static CustomerConfiguration class that has a static property named MaximumAge. In the getter you retrieve the MaximumAge either from the configuration file, database or any other data source. Because you have just decoupled the datasource from the Customer class you could then use the Service Locater pattern or IoC to specify different customer configuration providers.

    But then again this is overhead if you know for sure that your Magic Number of 5 will never change. You could then use constants in either a static ApplicationContext class or a static CustomerConfiguration class.



  • SpeBeeTo

    I'd probably add a class that contains the parameters... eg:

    public class BusinessParameters {
    public const int MyLimitation = 5;
    }

    But since you mention ini files i presume that you want to use a configuration file... So add a Settings file to your project... And add an entry for MyLimitation.. Now you can use this instead of the hardcoded value above:

    public class BusinessParameters
    {
    public static int MyLimitation
    {
    get { return Settings.Default.MyLimitation; }
    }
    }


  • Paul Tew

    Hi,

    You can use .config files or use constant values in a static class. If you don't need to change the values, I would suggest using constant values.

    Example using constant values:
    declaration:

    public static class ConstantVariables
    {
    public const string ConstantVariable1 = "my string";
    }

    use:
    this.Text = ConstantVariables.ConstantVariable1;

    Example using config file:

    • Add a reference to System.Configuration
    • Add an "Application Configuration File" to your project
    • Add a configuration key to the configuration file like:
      < xml version="1.0" encoding="utf-8"
      >
      <
      configuration
      >
      <
      appSettings
      >
      <
      add key="myConfiguration" value="TestValue"
      />
      </
      appSettings
      >
      </
      configuration
      >
    • Within the code you can refer to the config file by using:
      string configValue = System.Configuration.ConfigurationManager.AppSettings["myConfiguration"];

    Greetz,

    Geert

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog



  • Regarding hard code in C#