http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 1474|回复: 0

[C#语言基础] C#通用无限个开关通用类(SwitchHelper)

[复制链接]
发表于 2021-8-18 17:34:36 | 显示全部楼层 |阅读模式
C#通用无限个开关通用类(SwitchHelper)

我们平时在做项目时,很多时候会用到很多开关,或者是正负,对错的字段

如果每一个都去定义一个字段,是非常的浪费,而且成本会越来越高,

有没有办法可以一个字段直接完成的,其他是有

比如   1011010这样一串数据

第一个1是系统开关
第二个0是电源开关
以此类推

有多少个开关我们就写一个多长的字符串即可
不同的开关只要取自己对应的位置就能知道是开还是关,有数字的好处是还可以设置不仅是开和关,还可以是多种选项的

那么这个我们只需要写三个方法即可

第一个方法是根据输入的位置得取开关的当前值
[C#] 纯文本查看 复制代码
  /// <summary>
        /// 根据整数得到字符串开关
        /// </summary>
        /// <param name="Switch">整数,数据库取值</param>
        /// <returns></returns>
        public static string GetStringSwitch(int Switch)
        {
            return Switch.ToString().PadLeft(3, '0');
        }


第二个方法验证开关是否正确
[C#] 纯文本查看 复制代码
   /// <summary>
        /// 验证开关状态
        /// </summary>
        /// <param name="strSwitch">开关字符串</param>
        /// <param name="index">要验证的开关所在位置 从0开始</param>
        /// <returns>开为True,关为False</returns>
        public static Boolean CheckSwitch(string strSwitch, int index)
        {
            if (strSwitch.Length > index && strSwitch.Length == 3 && strSwitch.Substring(index, 1) == "0")
            {
                return true;
            }
            return false;
        }


第三个,就是更新开关的方法了
[C#] 纯文本查看 复制代码
  /// <summary>
        /// 修改开关字符串
        /// </summary>
        /// <param name="strSwitch">开关字符串</param>
        /// <param name="index">要修改的位置</param>
        /// <param name="Switch">要修改为的值</param>
        /// <returns></returns>
        public static SwitchReslt UpdateStrSwitch(string strSwitch, int index, int Switch)
        {
            SwitchReslt result = null;
            try
            {
                string newSwithc = string.Empty;
                for (int i = 0; i < strSwitch.Length; i++)
                {
                    string oneStr = strSwitch.Substring(i, 1);
                    if (i == index)
                    {
                        oneStr = Switch.ToString();
                    }

                    newSwithc += oneStr;
                }
                if (newSwithc != strSwitch)
                {
                    result = new SwitchReslt()
                    {
                        Status = true,
                        StrSwitch = newSwithc,
                        Switch = Convert.ToInt32(newSwithc)
                    };
                }

            }
            catch { }
            return result;
        }


用到的Model类如下
[C#] 纯文本查看 复制代码
    public class SwitchReslt
    {
        public Boolean Status { get; set; }
        public string StrSwitch { get; set; }
        public int Switch { get; set; }
    }


好了,大家有需要用到的直接拿走不谢
[C#] 纯文本查看 复制代码
    /// <summary>
    /// 开关类解析算法
    /// </summary>
    public class SwitchHelper
    {
        /// <summary>
        /// 根据整数得到字符串开关
        /// </summary>
        /// <param name="Switch">整数,数据库取值</param>
        /// <returns></returns>
        public static string GetStringSwitch(int Switch)
        {
            return Switch.ToString().PadLeft(3, '0');
        }
        /// <summary>
        /// 验证开关状态
        /// </summary>
        /// <param name="strSwitch">开关字符串</param>
        /// <param name="index">要验证的开关所在位置 从0开始</param>
        /// <returns>开为True,关为False</returns>
        public static Boolean CheckSwitch(string strSwitch, int index)
        {
            if (strSwitch.Length > index && strSwitch.Length == 3 && strSwitch.Substring(index, 1) == "0")
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 修改开关字符串
        /// </summary>
        /// <param name="strSwitch">开关字符串</param>
        /// <param name="index">要修改的位置</param>
        /// <param name="Switch">要修改为的值</param>
        /// <returns></returns>
        public static SwitchReslt UpdateStrSwitch(string strSwitch, int index, int Switch)
        {
            SwitchReslt result = null;
            try
            {
                string newSwithc = string.Empty;
                for (int i = 0; i < strSwitch.Length; i++)
                {
                    string oneStr = strSwitch.Substring(i, 1);
                    if (i == index)
                    {
                        oneStr = Switch.ToString();
                    }

                    newSwithc += oneStr;
                }
                if (newSwithc != strSwitch)
                {
                    result = new SwitchReslt()
                    {
                        Status = true,
                        StrSwitch = newSwithc,
                        Switch = Convert.ToInt32(newSwithc)
                    };
                }

            }
            catch { }
            return result;
        }
    }

    public class SwitchReslt
    {
        public Boolean Status { get; set; }
        public string StrSwitch { get; set; }
        public int Switch { get; set; }
    }





1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-29 21:00

© 2014-2021

快速回复 返回顶部 返回列表