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;
    }
    

      

  • 相关阅读:
    ES6模块开发+单文件组件
    Vue路由学习
    Vuex学习
    Vue组件
    Vue事件处理
    Git下载前后端代码步骤
    小黑记事本
    简单计算器
    ubuntu的基础命令
    拓扑排序以及求解关键路径
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/7356736.html
Copyright © 2011-2022 走看看