提升Boolean和out相结合的用户体验
在我们编写代码的过程中经常有这样的需求,比如添加一条数据,我们想要的结果是如果添加成功了就返回true,如果添加失败了就返回false,在返回false的同时携带错误信息,我们通常的做法是定义这样的方法
public bool Add(string name,out string error)
虽然说这样写可以符合我们的需求,当是总觉得不对劲,而且很麻烦,在接收时还有定义一个接收error的变量,对于我们这些coder来说就是用户体验不好,于是我左思右想,写了个结构来提供这个用户体验,下面是结构代码:
public struct Can
{
private bool _isTrue; public string Error { get; set; } public static bool operator true(Can param)
{
return param._isTrue == true;
} public static bool operator false(Can param)
{
return param._isTrue == false;
} public static bool operator !(Can param)
{
return !param._isTrue;
} public static bool operator ==(Can arg1, bool arg2)
{
return arg1._isTrue == arg2;
} public static bool operator !=(Can arg1, bool arg2)
{
return !(arg1._isTrue == arg2);
} public static implicit operator Can(bool arg)
{
return new Can { _isTrue = arg };
} public static implicit operator bool(Can arg)
{
return arg._isTrue;
} public static implicit operator Can(string arg)
{
return new Can { _isTrue = false, Error = arg };
} public override bool Equals(object obj)
{
if (!(obj is Can))
return false; Can can = (Can)obj; return can._isTrue == this._isTrue && can.Error == this.Error;
} public override int GetHashCode()
{
return this._isTrue.GetHashCode() ^ this.Error.GetHashCode();
}
}
下面是用例代码:
public static Can Add(int number)
{
if (number < )
return "number 不能小于0";
return true;
}
可以同时返回String类型和Boolean类型,返回String是默认的Boolean为false,主要是通过public static implicit operator Can(string arg)这个指针重写的。
下面来看看测试结果:
public static void Main()
{
Can can = Add(); if (can == true)
Console.WriteLine("添加成功"); if (!can)
Console.WriteLine(can.Error); can = Add(-); if (can)
Console.WriteLine("添加成功"); if (can == false)
Console.WriteLine(can.Error);
}
是不是很方便,嘻嘻。。。。
如果大家有更好的方法,不妨拿出来分享下呗!
提升Boolean和out相结合的用户体验的更多相关文章
- 界面优化--如何提升用户体验(Velocity.js和GSAP)
Velocity.js和GSAP 我们需要提升代码质量来留住用户.作为用户界面的建设者,我们的工作是迅速引导和引导用户的注意力,指导他们如何有效地使用我们的应用程序. 1. 如何提升代码质量 定向聚焦 ...
- jQuery 浮动标签插件,帮助你提升表单用户体验
浮动标签模式(Float Label Pattern)是最新流行的一种表单输入域的内容提示方式,当用户在输入框输入内容的时候,原先占位符的内容向上移动,显示在输入的内容的上面.这里推荐的这款 jQue ...
- Atitit.hybrid混合型应用 浏览器插件,控件的实现方式 浏览器运行本地程序的解决方案大的总结---提升用户体验and开发效率..
Atitit.hybrid混合型应用 浏览器插件,控件的实现方式 浏览器运行本地程序的解决方案大的总结---提升用户体验and开发效率.. 1. hybrid App 1 1.1. Hybrid Ap ...
- paip.提升用户体验--radio图片选择器 easyui 实现..
#paip.提升用户体验--radio图片选择器 easyui 实现.. =================================== ##原因... ------------------- ...
- paip.提升用户体验--提升java的热部署热更新能力
paip.提升用户体验--提升java的热部署热更新能力 想让java做到php那么好的热部署能力 "fix online"/在线修复吗??直接在服务器上修改源码生效,无需重启应 ...
- paip.提升用户体验----gcc c++ JIT-debugging 技术
paip.提升用户体验----gcc c++ JIT-debugging 技术 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http ...
- 提升网站用户体验—WebP 图片的高效使用
一.WebP 的由来 现代图像压缩技术对我们的生活方式影响很大.数码相机能将上千张高质量图片存储到一张内存卡里.智能手机可以与邻近设备快速分享高分辨率的图片.网站与手机等移动设备能快速展示各种富媒体. ...
- paip.提升用户体验-----c++ gcc 命令在notepad++扩展中的配置..
paip.提升用户体验-----c++ gcc 命令在notepad++扩展中的配置.. 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址: ...
- paip.提升用户体验---c++ qt 取消gcc编译的警告信息.txt
paip.提升用户体验---c++ qt 取消gcc编译的警告信息.txt 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http:// ...
随机推荐
- sublime快捷键收藏
快速查找(ctrl + P)输入@+函数名可以快速找到函数.输入#+文本可以快速进行文件内文本匹配.3. 多行游标功能(ctrl + D,非常实用)如何将文件中的某个单词更改为另一个?方法一:利用查找 ...
- Python中for\while的用法
代码示例 board = [] for i in range(5): board.append(i) print board board = [] i = 0 while i < 5: boar ...
- python保留指定文件、删除目录其他文件的功能(2)
在(1)中脚本实现了保留指定文件的功能,但不能删除空目录,在此补上删除空目录的方法 def DeleteEmptyDir(path): for i in range(1,100): for paren ...
- webService设置超时时间
在客户端配置文件中设置: <bindings> <basicHttpBinding> <binding name="UrlCrawler ...
- RHEL 7.0 修改防火墙配置
RHEL 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 关闭firewall: systemctl stop firewalld.service #停止firewal ...
- Linux下MySQL 5.6的修改字符集编码为UTF8(彻底解决中文乱码问题)
一.登录MySQL查看用SHOW VARIABLES LIKE ‘character%’;下字符集,显示如下:+--------------------------+----------------- ...
- 软件测试-nextDate问题
NextDate 函数包含三个变量:month . day 和 year ,函数的输出为输入日期后一天的日期. 例如,输入为 2006年3月 7日,则函数的输出为 2006年3月8日 .要求输入变量 ...
- H.264 RTP 封包格式
H.264 视频 RTP 负载格式 1. 网络抽象层单元类型 (NALU) NALU 头由一个字节组成, 它的语法如下: +---------------+ |0|1|2|3|4|5|6|7 ...
- cf478A Initial Bet
A. Initial Bet time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Rock the Tech Interview
Today, Infusion held a talk in Columbia University about tech interview. Talker: Nishit Shah @ Infus ...