2017-01-11 81 views
0

我在VB.Net中做了一個日曆,我決定給C#一個嘗試。我很困惑如何將我的代碼轉換爲C#,因爲Do Loop。 這是我在VB中的當前源代碼。什麼是C#的Do循環等價?

Public Sub LoadCal(ByVal ldate As Date, ByVal Selected As Integer) 


    M = ldate.Month 
    Y = ldate.Year 
    D = ldate.Day 

    clearall() 
    MonthName.Text = monthstr(ldate.Month) & " " & ldate.Year 
    Dim fdate As DayOfWeek = GetFirstOfMonthDay(ldate) 
    Dim idate As Integer = 1 
    Dim row As Integer = 1 

    Do 
     getlabel(fdate, row).Text = idate 
     getlabel(fdate, row).ForeColor = Label18.ForeColor 

     'Current Date 
     If idate = Selected And idate = Date.Now.Day And ldate.Month = Date.Now.Month Then 
      getlabel(fdate, row).ForeColor = Color.Red 
     End If 

     If fdate = DayOfWeek.Saturday Then 
      row += 1 
     End If 

     fdate = tdate(fdate) 
     idate += 1 

     If su1.Text.Length = 0 Then 
      psu1.BorderStyle = BorderStyle.None 
      psu1.Enabled = False 
     Else 
      psu1.BorderStyle = BorderStyle.FixedSingle 
      psu1.Enabled = True 
     End If 

     If idate = Date.DaysInMonth(ldate.Year, ldate.Month) + 1 Then 
      Exit Do 
     End If 
    Loop 
End Sub 

這裏就是我到C#,這似乎是正確的做出。 (我希望)

public void LoadCal(DateTime ldate, int Selected) { 
    M = ldate.Month; 
    Y = ldate.Year; 
    D = ldate.Day; 

    clearall(); 
    MonthName.Text = (monthstr(ldate.Month) + (" " + ldate.Year)); 
    DayOfWeek fdate = GetFirstOfMonthDay(ldate); 
    int idate = 1; 
    int row = 1; 

    ) { 
     getlabel(fdate, row).Text = idate; 
     getlabel(fdate, row).ForeColor = Label18.ForeColor; 
     // Current Date 
     Now.Month; 
     getlabel(fdate, row).ForeColor = Color.Red; 
     fdate = DayOfWeek.Saturday; 
     row++; 
     if ((fdate == tdate(fdate))) { 
      idate++; 
      if ((su1.Text.Length == 0)) { 
       psu1.BorderStyle = BorderStyle.None; 
       psu1.Enabled = false; 
      } 
      else { 
       psu1.BorderStyle = BorderStyle.FixedSingle; 
       psu1.Enabled = true; 
      } 

      (DaysInMonth(ldate.Year, ldate.Month) + 1); 
         }    
    }   
} 

我很確定C#不接受VB的For循環,我不知道如何以另一種方式將它合併。我感謝幫助和抱歉。

回答

5

這是do {...} while (...);

例子!

public class TestDoWhile 
    { 
     public static void Main() 
     { 
      int x = 0; 
      do 
      { 
       Console.WriteLine(x); 
       x++; 
      } while (x < 5); 
     } 
    } 
    /* 
     Output: 
     0 
     1 
     2 
     3 
     4 
    */ 

,如果你想走出循環的(就像你在你的代碼所做的),只需使用break;關鍵字。

在你的情況下,你會do {...} while (true);,因爲你實際上希望你的代碼不斷循環,直到你break

+0

哇哦,現在我覺得自己很蠢。我真的很感謝幫助。我還有一個問題,這有點相關。什麼是C#的模塊對應物?我似乎無法在任何地方找到它。 – SlicedBread

+0

@SlicedBread,沒有一個。靜態類是最接近你在C#中的VB模塊。 –

+0

我明白了。看起來過渡並不容易。非常感謝,@KirillShlenskiy。 – SlicedBread

2
do 
{ 
    // Body 
} while (condition); 

它在C#中被稱爲do..while

這裏,條件需要是一個表達式檢查,返回一個布爾值。有一件事這裏注意,是一個do循環將永遠執行至少一次。

+0

無論編程語言如何,總是運行一次,我相信;但對那些不知道這一點的人有效。 – Edward

+0

@愛德華只是說明明顯;-) –

0

我不知道,但可以@SlicedBread要求在C#作爲Yotam三文魚無限循環已經解釋,而語法:

'VB .Net 
Do 
Loop While condition 

//in C# 
do 
{ 
} while (condition); 

'VB .Net 
Do While condition 
Loop 

//in C# 
while (condition) 
{ 
} 


'and as asked - infinite loop 
'VB .Net 
Do 
Loop 
//in C# 
do 
{ 
} while (true); 

//and use break; for exit do but it is not exact replacement 

@SlicedBread你可以簡單地轉換任何網絡代碼轉換器來檢查代碼的語法

在這裏被轉換代碼---

public void LoadCal(System.DateTime ldate, int Selected) 
{ 

    M = ldate.Month; 
    Y = ldate.Year; 
    D = ldate.Day; 

    clearall(); 
    MonthName.Text = monthstr(ldate.Month) + " " + ldate.Year; 
    DayOfWeek fdate = GetFirstOfMonthDay(ldate); 
    int idate = 1; 
    int row = 1; 

    do { 
     getlabel(fdate, row).Text = idate; 
     getlabel(fdate, row).ForeColor = Label18.ForeColor; 

     //Current Date 
     if (idate == Selected & idate == System.DateTime.Now.Day & ldate.Month == System.DateTime.Now.Month) { 
      getlabel(fdate, row).ForeColor = Color.Red; 
     } 

     if (fdate == DayOfWeek.Saturday) { 
      row += 1; 
     } 

     fdate = tdate(fdate); 
     idate += 1; 

     if (su1.Text.Length == 0) { 
      psu1.BorderStyle = BorderStyle.None; 
      psu1.Enabled = false; 
     } else { 
      psu1.BorderStyle = BorderStyle.FixedSingle; 
      psu1.Enabled = true; 
     } 

     if (idate == System.DateTime.DaysInMonth(ldate.Year, ldate.Month) + 1) { 
      break; // TODO: might not be correct. Was : Exit Do 
     } 
    } while (true); 
} 

//======================================================= 
//Service provided by Telerik (www.telerik.com) 
//Conversion powered by NRefactory. 
//Twitter: @telerik 
//Facebook: facebook.com/telerik 
//=======================================================