zoukankan      html  css  js  c++  java
  • 开发——硬编码

    可变变量的硬编码

    声明static类赋值,或者添加config文件

    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:

    For example:

    public static class Roles
    {
      public const string Customer = "Customer";
      public const string Branch = "Branch";
    } 

    Usage:

    if (role == Roles.Customer)
    {
    
    }
    else if (role == Roles.Branch)
    {
    
    }
    

      

    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:
        <?xmlversion="1.0"encoding="utf-8"
        ?>
        <
        configuration
        >
           <
        appSettings
        >
              <
        addkey="myConfiguration"value="TestValue"
        />
           </
        appSettings
        >
        </
        configuration
        >
      • Within the code you can refer to the config file by using:
        string configValue = System.Configuration.ConfigurationManager.AppSettings["myConfiguration"];

    使用枚举类

    use an enum to help with readability.

    enum MyTypes
    {
     URL = 1,
     UserName = 2,
     Id = 3,
    }
    
    switch (myType)
    {
        case MyTypes.URL:
            TidyUrl();
        case MyTypes.UserName:
            TidyUsername();
        case MyTypes.Id:
            TidyID();
        default:
            break;
    }
    

      

  • 相关阅读:
    平均值的最值化
    GCJ 2008 R3 C 二分图匹配
    BZOJ 1013 高斯消元
    NOIP2015 d2T3 二分+树上前缀和
    9.7集训 总结
    BZOJ 1026
    BZOJ 3223
    NOIP前夕:codevs,关路灯
    NOIP前夕:codevs,解药还是毒药
    NOIP前夕:codevs,修剪花卉
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/7356736.html
Copyright © 2011-2022 走看看