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的更多相关文章

  1. 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日志: ...

  2. C++ STL, set用法。 待更新zzzzz

    set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值:另外,还 ...

  3. Android 5.0 到 Android 6.0 + 的深坑之一 之 .so 动态库的适配

    (原创:http://www.cnblogs.com/linguanh) 目录: 前序 一,问题描述 二,为何会如此"无情"? 三,目前存在该问题的知名SDK 四,解决方案,1 对 ...

  4. Android -- 真正的 高仿微信 打开网页的进度条效果

    (本博客为原创,http://www.cnblogs.com/linguanh/) 目录: 一,为什么说是真正的高仿? 二,为什么要搞缓慢效果? 三,我的实现思路 四,代码,内含注释 五,使用方法与截 ...

  5. Oracle补全日志(Supplemental logging)

    Oracle补全日志(Supplemental logging)特性因其作用的不同可分为以下几种:最小(Minimal),支持所有字段(all),支持主键(primary key),支持唯一键(uni ...

  6. 记一次使用 android 自带 WebView 做富文本编辑器之API、机型的兼容及各种奇葩bug的解决

    转载请声明出处(http://www.cnblogs.com/linguanh/) 目录 1,测试设备介绍 2,开源项目richeditor及CrossWalk的选择 3,遇到的bug及其解决方法 4 ...

  7. 一行代码引入 ViewPager 无限循环 + 页码显示

    (出处:http://www.cnblogs.com/linguanh) 前序: 网上的这类 ViewPager 很多,但是很多都不够好,体现在 bug多.对少页面不支持,例如1~2张图片.功能整合不 ...

  8. android 在 ListView 的 item 中插入 GridView 仿微信朋友圈图片显示。

    转载请声明出处(http://www.cnblogs.com/linguanh/) 先上张效果图: 1,思路简述 这个肯定是要重写 baseAdapter的了,这里我分了两个数据适配器,一个是自定义的 ...

  9. 一个难倒 3年 android开发经验 " 工程师 " 的 "bug"

    一个关于 imageView 设置 scaleType 的问题. 就在刚才 晚上9 点多的时候,我的一个外包伙伴发一个工程代码我,叫我去看下这样一个"bug",说折腾了很久,图片选 ...

随机推荐

  1. 读取MySQL中的数据并显示在JSP上

    <%@ page language="java" import="java.sql.*,java.io.*,java.util.*,java.sql.SQLExce ...

  2. ios开发--animation flash动画

        /**  *  showAnimationFlash  */ + (void)showAnimationFlashWithView:(UIView *)animationView durati ...

  3. Ubuntu--服务器版本系统安装图解教程

    附Ubuntu Server 13.04系统镜像下载地址: 32位:http://mirrors.163.com/ubuntu-releases/13.04/ubuntu-13.04-server-i ...

  4. Java实现Mysql数据导入导出

    package com.backup; import java.io.BufferedReader;import java.io.FileInputStream;import java.io.File ...

  5. Spring 异常 —— cvc-elt.1: Cannot find the declaration of element 'beans'

    有个使用 Spring 的项目,运行时报错: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 5 ...

  6. G-sensor 与M-sensor区别

    g-sensor是重力传感器,能感应芯片在三个方向(通常是)上的重力加速度.手机里的重力球用的就是这个技术,m-sensor如果是motion sensor的简称的话,基本上指的和g-sensor是一 ...

  7. HTML 空格的表示符号 nbsp / ensp / emsp 的区别?

      半角的不断行的空白格(推荐使用)   半角的空格   全角的空格   半角的不断行的空白格(推荐使用)    半角的空格     全角的空格

  8. zoj 3329 One Person Game (有环 的 概率dp)

    题目链接 这个题看的别人的思路,自己根本想不出来这种设方程的思路. 题意: 有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和. 当 ...

  9. 【MySQL】ERROR 1045 (28000): Access denied for user的解决方法

    去官网下载压缩版的MySQL Server,解压配置path环境变量后.然后克隆my-default.ini创建my.ini文件,在文件中[mysqld]下面配置basedir和datadir bas ...

  10. Node Security

    发一个很早之前做的一个小东西-安全管理软件-可以对U盘进行管理,对后台程序进行扫描.分析!