我用asp.net编程,定义两个维数组,如何判断数组值为空?
数组:
#region inital Festival
string[,] holidays = new string[13, 32];
holidays[1, 1] = "元旦";
holidays[2, 14] = "情人节";
holidays[3, 8] = "妇女节";
holidays[3, 12] = "植树节";
holidays[4, 1] = "愚人节";
holidays[5, 1] = "劳动节"
holidays[5, 4] = "青年节";
holidays[5, 12] = "护士节";
holidays[5, 14] = "母亲节";
holidays[5, 14] = "助残日";
holidays[6, 1] = "国际儿童节";
holidays[6, 5] = "环境保护日";
holidays[6, 18] = "父亲节";
holidays[6, 26] = "国际禁毒日";
holidays[9, 10] = "教师节";
holidays[11, 23] = "感恩节";
holidays[12, 1] = "艾滋病日";
holidays[12, 25] = "圣诞节";
#endregion这是我的密码:
string holiday = holidays[e.Day.Date.Month, e.Day.Date.Day];
DateTime LunDay = e.Day.Date;
LunDay ld = new LunDay();
if (holiday != string.Empty)
{
//If holiday value not null,add value
}
else
{
//If holiday is null,add tradition date in carlendar
}我运行代码,但它总是添加假日和不添加传统日期,如何处理it.The string.Empty没有任何影响。
以下是整个代码:
protected void portalCalendar_DayRender(object sender, DayRenderEventArgs e)
{
t.CurrentCulture = this.oldCulture;
#region inital Festival
string[,] holidays = new string[13, 32];
holidays[1, 1] = "元旦";
holidays[2, 14] = "情人节";
holidays[3, 8] = "妇女节";
holidays[3, 12] = "植树节";
holidays[4, 1] = "愚人节";
holidays[5, 1] = "劳动节";
holidays[5, 4] = "青年节";
holidays[5, 12] = "护士节";
holidays[5, 14] = "母亲节";
holidays[5, 14] = "助残日";
holidays[6, 1] = "国际儿童节";
holidays[6, 5] = "环境保护日";
holidays[6, 18] = "父亲节";
holidays[6, 26] = "国际禁毒日";
holidays[7, 1] = "中共诞辰";
holidays[8, 1] = "建军节";
holidays[9, 10] = "教师节";
holidays[10, 1] = "国庆节";
holidays[11, 23] = "感恩节";
holidays[12, 1] = "艾滋病日";
holidays[12, 12] = "西安事变";
holidays[12, 25] = "圣诞节";
#endregion
CalendarDay calDay = ((DayRenderEventArgs)e).Day;
TableCell tableCell = ((DayRenderEventArgs)e).Cell;
if (e.Day.IsOtherMonth)
{
e.Cell.Controls.Clear();
}
else
{
try
{
string holiday = holidays[e.Day.Date.Month, e.Day.Date.Day];
DateTime LunDay = e.Day.Date;
LunDay ld = new LunDay();
if (holiday != string.Empty)
{
e.Cell.Controls.Add(new LiteralControl("<br><font color=blue size=2>" + holiday + "</font>"));
//e.Cell.Controls.Add(new LiteralControl("<br>" + ld.GetLunarCalendar(LunDay)));
}
else
{
Response.Write("null holiday");
}
}
catch (Exception exception)
{
Response.Write(exception.ToString());
}
}
}发布于 2013-12-09 08:31:32
奇怪的是,您的代码中有正确的注释,但是实现不正确。看看这个:
if (holiday != string.Empty)
{
//If holiday value not null,add value
}这里您说的是'if value null‘,但是在if条件下,您会根据空字符串检查值。空字符串与null值不相同。您还需要在代码中添加对null的检查:
if (holiday != null && holiday != string.Empty)
{
//If holiday value not null,add value
}这会解决你的问题。
顺便说一句,框架中也有预定义的方法来解决这个常见的场景-- string.IsNullOrEmpty(),它检查字符串和空值。
https://stackoverflow.com/questions/20465593
复制相似问题