什么是C#的循环等效?

时间:2017-01-11 05:03:40

标签: c# vb.net

我在VB.Net上制作了一个日历,我决定试试C#。由于 Do Loop ,我对如何将代码转换为C#感到困惑。 这是我目前在VB中的源代码。

    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循环,我不知道如何以另一种方式合并它。我很感激帮助和抱歉。

3 个答案:

答案 0 :(得分: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之前一直循环。

答案 1 :(得分:2)

do
{
   // Body
} while (condition);

在C#中称为do..while

这里,条件需要是返回bool值的表达式检查。这里注意的一件事是,do循环总是至少执行一次。

答案 2 :(得分:0)

我不知道,但是@SlicedBread可能会要求c#AS Yotam Salmon在语法中已经解释过无限循环:

'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
//=======================================================