无法将 int 隐式转换为 bool

0 c#

好的,所以我收到“无法将 int 转换为 bool”错误。

我正在尝试转换此 VB .net 代码:

Function GetChecksum(ByVal Source As String) As Long
    Dim iVal, Weight, CheckHold, CheckSum As Long
    Weight = 1
    CheckSum = 0
    For iVal = 1 To Len(Source)
        CheckHold = Asc(Mid$(Source, iVal, 1)) * Weight
        CheckSum = CheckSum + CheckHold
        Weight = Weight + 2
    Next iVal
    GetChecksum = CheckSum Mod &H7FFFFFFF
End Function
Run Code Online (Sandbox Code Playgroud)

我已经到这里了:

    public long getCheckSum(string source)
    {
        long iVal, weight, checkgold, checksum = new long();
        weight = 1;
        checksum = 0;
        for (iVal = 1; Strings.Len(source);)
        {

        }
    }
Run Code Online (Sandbox Code Playgroud)

问题是“For (iVal = 1; Strings.Len(source);)”代码。我正在使用“Microsoft.VisualBasic”。我只是不知道现在该怎么办。如果你能帮助我那就太好了。

Jor*_*dan 5

看来您需要正确设置循环。在 C# 中,for 循环(通常)遵循以下格式:

for(initializer; conditional check; evaluation)
Run Code Online (Sandbox Code Playgroud)
  • 初始化程序是您设置 iVal = 1 等变量的地方
  • 条件检查是确定 for 循环范围的地方
  • 评估通常是增加变量的地方

在您的代码中,您有一个整数 Strings.Len(source) 作为条件检查,它期望布尔响应,因此失败。

你的 for 循环开启器应该看起来像这样:

for (iVal = 1; iVal < source.Length; iVal++)
Run Code Online (Sandbox Code Playgroud)

假设您的逻辑是 0 < iVal < 源字符串的长度。

顺便说一句,在 C# 中检查字符串长度的方法是使用 .Length 属性,而不是使用 Strings.Len() 函数。