【发布时间】:2017-01-30 21:50:12
【问题描述】:
我目前正在尝试将我的 vb.net 代码的一部分转换为 c#,但我似乎无法获得正确的语法。
我的 vb.net 代码是
Private Sub board(days As Integer, name As String)
Dim dcount As Integer = 0
Dim counter As Integer = 0
If My.Computer.FileSystem.FileExists("pathway" + name) AndAlso Not File.ReadAllText("pathway" + name).Length = 0 Then
Dim d As List(Of String) = File.ReadAllLines("pathway" + name).ToList
Dim line As String = d(0)
While counter <> d.Count
line = d(counter)
If DateTime.Compare(line.Substring(0, line.LastIndexOf(",")), Now.AddDays(days).ToString("MM/dd/yyyy")) < 0 Then
dcount += 0
counter += 1
Else
dcount += 1
counter += 1
End If
End While
End If
vb.net 代码运行良好,但我的 c# 下面给出了错误:
运算符'
错误所在的行是:
if (DateTime.Compare(line.Substring(0, line.LastIndexOf(",")), DateTime.Now.AddDays(days).ToString("MM/dd/yyyy") < 0)) {
整个部分都在下面
private void board(int days, string name){
int dcount = 0;
int counter = 0;
if (File.Exists(@"pathway" + name) && File.ReadAllText(@"pathway" + name).Length != 0)
{
List<string> d = File.ReadAllLines(@"pathway" + name).ToList();
string line = d[0];
while (counter != d.Count)
{
line = d[counter];
// compares the current date to the amount of days you put in the days integer
if (DateTime.Compare(line.Substring(0, line.LastIndexOf(","), DateTime.Now.AddDays(days).ToString("MM/dd/yyyy") < 0) {
counter++;
} else
{
dcount++;
counter++;
}
}
}
}
感谢你们能给我的任何帮助
【问题讨论】:
-
您提供的代码甚至在语法上都不正确。有错误的行至少缺少一个右括号...我猜在
line.LastIndexOf(",")之后 -
请提供
line的示例。 -
试试这个` if(DateTime.Compare(DateTime.Parse(line.Substring(0, line.LastIndexOf(","))), DateTime.Now.AddDays(days))
-
有一个免费的 Telerik 在线工具,您可以使用它来将 VB 转换为 C#,您唯一需要做的就是在 C# 代码中将索引周围的
( )更改为[ ],非常简单
标签: c# vb.net operator-keyword