zzzzz
Extension Method: Return another string if string is null or empty
Just a tiny little extension method. You may know the ?? operator for null checks:
var something = objectThatCouldBeNull ?? anotherObject;
Basically if objectThatCouldBeNull is null, then anotherObject is used instead. Unfortunately, this does not work with empty strings, so here is a quick extension method for that:
public static string IfEmpty(this string input, string otherString)
{
if (string.IsNullOrEmpty(input)) return otherString;
return input;
}
call it like
var myString = someString.IfEmpty("someOtherString");
The nice thing on extension methods is that you can call them on null instances of an object, so a call to
((string)null).IfEmpty("SomethingElse");
is safe.
A simple Even/Odd Cycler for .net
If you want a table with different CSS Styles for even/odd rows, that is reasonably easy, usually you have code like this:
bool evenRow = true;
foreach (var item in someList)
{
evenRow = !evenRow;
SomeTable.Rows.Add(SomeFunctionThatReturnsATableRow(item,evenRow?"evenRow":"oddRow"));
}
So we have a bool that we switch on every iteration and then we derive the CSS Class Name from that. If you do that in one place, it's okay, but if you have to do that in many places, it can become quite a bit tedious. Sure, if you happen to use a for-loop you can just use "index % 2 == 0" to find out if you are on an even row, but I instead created a class:
public class EvenOddCycler
{
private readonly string _oddClassName;
private readonly string _evenClassName;
private int _numCycles;
public EvenOddCycler() : this("evenRow","oddRow"){}
public EvenOddCycler(string evenClassName, string oddClassName)
{
_evenClassName = evenClassName;
_oddClassName = oddClassName;
_numCycles = 0;
}
public string Cycle()
{
_numCycles++;
return IsCurrentlyEven ? _evenClassName : _oddClassName;
}
public void Reset()
{
_numCycles = 0;
}
public bool IsCurrentlyEven
{
get { return (_numCycles % 2 == 0); }
}
}
Really simple stuff here: The constructor takes two strings, one for even and for odd. It has a Cycle function that increases a counter and returns one of the two strings. Also included a Reset() function to re-use the cycler in case you have more than one table on a page.The usage looks like this:
var cycler = new EvenOddCycler();
foreach (var item in someList)
{
SomeTable.Rows.Add(SomeFunctionThatReturnsATableRow(item,cycler.Cycle()));
}
What did we gain?•No need to constantly repeat the class names over and over again. You shouldn't hardcode them, but even if you put it in a Resource class of some kind, you still have to have the boolean switch somewhere to get the correct class name. No need here anymore.
•No need to copy/paste this if you have multiple tables on a Page. Just call cycler.Reset()
Granted. it's a small thing, but every little thing I don't have to worry about is good. This is not Thread-safe because I see no scenario where you would share one cycler. Making it Thread-safe is a simple matter of adding a lock object and locking all three method bodies though.
zzzzz的更多相关文章
- INFO org.apache.hadoop.ipc.RPC: Server at master/192.168.200.128:9000 not available yet, Zzzzz...
hadoop 启动时namenode和datanode可以启动,使用jps命令也可以看到进程,但是在浏览器中输入master:50070却没有显示datanode 查看datanode的log日志: ...
- C++ STL, set用法。 待更新zzzzz
set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值:另外,还 ...
- Android 5.0 到 Android 6.0 + 的深坑之一 之 .so 动态库的适配
(原创:http://www.cnblogs.com/linguanh) 目录: 前序 一,问题描述 二,为何会如此"无情"? 三,目前存在该问题的知名SDK 四,解决方案,1 对 ...
- Android -- 真正的 高仿微信 打开网页的进度条效果
(本博客为原创,http://www.cnblogs.com/linguanh/) 目录: 一,为什么说是真正的高仿? 二,为什么要搞缓慢效果? 三,我的实现思路 四,代码,内含注释 五,使用方法与截 ...
- Oracle补全日志(Supplemental logging)
Oracle补全日志(Supplemental logging)特性因其作用的不同可分为以下几种:最小(Minimal),支持所有字段(all),支持主键(primary key),支持唯一键(uni ...
- 记一次使用 android 自带 WebView 做富文本编辑器之API、机型的兼容及各种奇葩bug的解决
转载请声明出处(http://www.cnblogs.com/linguanh/) 目录 1,测试设备介绍 2,开源项目richeditor及CrossWalk的选择 3,遇到的bug及其解决方法 4 ...
- 一行代码引入 ViewPager 无限循环 + 页码显示
(出处:http://www.cnblogs.com/linguanh) 前序: 网上的这类 ViewPager 很多,但是很多都不够好,体现在 bug多.对少页面不支持,例如1~2张图片.功能整合不 ...
- android 在 ListView 的 item 中插入 GridView 仿微信朋友圈图片显示。
转载请声明出处(http://www.cnblogs.com/linguanh/) 先上张效果图: 1,思路简述 这个肯定是要重写 baseAdapter的了,这里我分了两个数据适配器,一个是自定义的 ...
- 一个难倒 3年 android开发经验 " 工程师 " 的 "bug"
一个关于 imageView 设置 scaleType 的问题. 就在刚才 晚上9 点多的时候,我的一个外包伙伴发一个工程代码我,叫我去看下这样一个"bug",说折腾了很久,图片选 ...
随机推荐
- 为什么要关闭360云盘:新来的美工嫌我们logo太丑,所以就决定关闭了。这个理由怎么样
新来的美工嫌我们logo太丑,所以就决定关闭了.这个理由怎么样曾经拥有的不要忘记:不能得到的更要珍惜:属于自己的不要放弃:已经失去的留作回忆.我刚来~~~嘿嘿~~ 久经考验的,忠诚的国际宅男主义战士, ...
- 汇编语言---call和ret指令
汇编语言--call和ret指令 call和ret指令 call和ret指令都是转移指令,它们都修改IP,或同时修改CS和IP. 它们经常被共同用来实现子程序的设计. ret和retf ret指令用栈 ...
- Java:IO流与IO设备
打印流:PrintWriter和PrintStream 特点:可以直接操作输入流和文件 //例子1:使用PrintStream将格式化的日期打印到文件中 import java.io.*; impor ...
- swift:创建表格UITableView
用swift创建单元格和用iOS创建单元格形式基本相同,就是语法上有些异样.swift中调用成员方法不再使用[ ]来发送消息,而是使用.成员方法的形式调用成员函数.这种格式非常类似于java中的点成员 ...
- Linux命令-date
[root@localhost ~]# date 2016年 09月 07日 星期三 :: CST [root@localhost ~]# date "+%Y" [root@loc ...
- 【图像算法】七种常见阈值分割代码(Otsu、最大熵、迭代法、自适应阀值、手动、迭代法、基本全局阈值法)
图像算法:图像阈值分割 SkySeraph Dec 21st 2010 HQU Email:zgzhaobo@gmail.com QQ:452728574 Latest Modified Da ...
- sleep和wait到底什么区别
wait是在当前线程持有wait对象锁的情况下,暂时放弃锁,并让出CPU资源,并积极等待其它线程调用同一对象的notify或者notifyAll方法.注意,即使只有一个线程在等待,并且有其它线程调用了 ...
- NAND Flash【转】
转自:http://www.cnblogs.com/lifan3a/articles/4958224.html 以Micron公司的MT29F2G08为例介绍NAND Flash原理和使用. 1. 概 ...
- windows 7 ssh server for scp
Software: BvSshServe. (个人用免费,商业收费) scp localfile.txt user_tst@11.111.12.170:'E:\downloads\SSH\auto.p ...
- [2014-03-13 08:46:42 - Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace. 问题提示 ...