我在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循環,我不知道如何以另一種方式將它合併。我感謝幫助和抱歉。
哇哦,現在我覺得自己很蠢。我真的很感謝幫助。我還有一個問題,這有點相關。什麼是C#的模塊對應物?我似乎無法在任何地方找到它。 – SlicedBread
@SlicedBread,沒有一個。靜態類是最接近你在C#中的VB模塊。 –
我明白了。看起來過渡並不容易。非常感謝,@KirillShlenskiy。 – SlicedBread