2013-12-09 55 views
0

我用asp.net程序,定義了兩個維數組,如何判斷數組值null?如何判斷2維數組在C#中有價值?

數組:

  #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 
        } 

我運行代碼,但它跳投添加假期和不添加傳統的日期,如何處理字符串後援.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()); 
       } 
      } 
     } 

回答

2

自嘆不如,你必須在你的代碼正確的意見,但不正確的執行。看看這個:你說

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

這裏「如果值不爲空」,但在if條件您覈對空字符串值。空字符串與null值不一樣。您還需要對null添加檢查你的代碼:

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

,將解決你的問題。

順便說一下,框架中還有一個預定義的方法來解決這個常見的場景 - string.IsNullOrEmpty(),它檢查字符串是否爲null和空值。

+0

速記版 - 'if(!string.IsNullOrEmpty(holiday)){}' – Gusdor

+0

謝謝,問題解決了。 – Dolphin