开发者

Cannot implicity convert int to bool

Okay, so I'm getting a "cannot convert int to bool" error.

I'm trying to convert this VB .net code:

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

I've gotten up to here:

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

        }
    }

The problem is the "For (iVal = 1; Strings.Len(source);)" code. I am using "Microsoft.VisualBasic". I just don't know what to do right now. If you could help me that'd be great.


Looks like you need to set your loop correctly. In C#, a for loop (generally) follows the following format:

for(initializer; conditional check; evaluation)
  • initializer is where you set variables like iVal = 1
  • conditional check is where you determine the bounds of the for loop
  • evaluation is usually where you increment a variable

In your code, you have an integer, Strings.Len(source), as the conditional check, which is expecting a boolean response so it's failing.

Your for loop opener should look something like this:

for (iVal = 1; iVal < source.Length; iVal++)

That's assuming your logic is 0 < iVal < length of source string.

As an aside, the way you check the length of a string in C# is with the .Length property, rather than using the Strings.Len() function.


    for (iVal = 1; iVal < source.Length; iVal++)
    {

    }

The middle section is a condition.


You will need:

  for (iVal = 1; iVal <= source.Length; ival += 1)

But be aware this loops through 1..source.Length,
not the more common (in C#) 0..source.Length-1


As the others have already solved your problem, I only want to add for future reference that you might want to check out Convert VB to C#.

I've used it myself on a number of occasions with pretty good results.


Standard syntax of for loop:

for(counter initialize; counter compare; counter increment) {}

The comparison expects a bool, you're providing an int with Strings.Len(source), which returns some number, not a Boolean value like true or false.

Try

for(iVal = 1; iVal < String.Len(source); iVal++)

You may want to use <= since your starting at 1 or set iVal to 0


Your For syntax should looks something like this :

For(ival = 1; source.Length; ival++)
{
  // your code here
}

ival++ will replace the "Next" in VB.


Rather than translate the for loop literally into C#, I'd use a foreach, as you're doing a straightforward iteration over the elements of a sequence (each char in the string):

public long getCheckSum(string source)
{
  long checkHold = 0, checkSum = 0, weight = 1;

  foreach (char ch in source)
  {
    checkHold = (long)ch * weight;
    checkSum += checkHold;
    weight += 2;
  }

  return checkSum % 0x7FFFFFFF;
}


you want

for (iVal = 1; iVal <= source.Length; iVal++)
{
    //your code here
}

Alternatively, if you want to leave iVal alone (because you need it "pure" for something later)

for(i = iVal; i <= source.Length; i++)
{
    //your code here.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜