当前位置:  开发笔记 > 编程语言 > 正文

什么是C#的循环等效?

如何解决《什么是C#的循环等效?》经验,为你挑选了1个好方法。

我在VB.Net上制作了一个日历,我决定试试C#.由于Do循环,我对如何将代码转换为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循环,我不知道如何以另一种方式合并它.我很感激帮助和抱歉.



1> Yotam Salmon..:

就是这样 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.

推荐阅读
落单鸟人
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有