2017-01-11 38 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 
//=======================================================