0

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.

1
  • 1
    Before anyone else adds or votes up anything containing iVal = 1, look at the docs for Mid, where it states that indexing with this function is 1-based. The C# equivalent must use iVal = 0 to be correct. Jul 14, 2011 at 12:59

8 Answers 8

5

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.

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

    }

The middle section is a condition.

1
  • 2
    This most likely is 1 iteration too short. Jul 14, 2011 at 19:39
3

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

0

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.

0

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

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.

0

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;
}
-1

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.
}
1
  • It came from the mystical land of "Typo" in the province of "typing too fast." I'll fix it.
    – AllenG
    Jul 13, 2011 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.