How to judge 2-demension array has value in C#?

I am program with asp.net ,define two demension array,how to judge the array value null?

the array:

            #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

There is my code:

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
                    }

I run the code but it aways add holiday and don't add tradition date,how to deal it.The string.Empty has no effect.

This is the whole code:

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());
                }
            }
        }

Answers


Strange enough, you have correct comments in your code, but incorrect implementation. Look at this:

if (holiday != string.Empty)
{
     //If holiday value not null,add value
}

Here you are saying 'if value not null' but in if condition you check value against empty string. Empty string is not the same as null value. You also need to add check against null to your code:

if (holiday != null && holiday != string.Empty)
{
     //If holiday value not null,add value
}

That will solve your problem.

BTW, there is also predefined method in framework to address this common scenario - string.IsNullOrEmpty(), it checks string against null and empty value.


Need Your Help

Hibernate return empty result if using LIKE condition with univarchar columns (Sybase)

sql hibernate unicode

Let's suppose I have a table: Person(Name: univarchar) and this table contains a row "abc".

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.