Can someone help me convert this

G

Guest

Can someone help me convert thi to c# or vb.net

Calculate()

{
int i, nLen, nSum = 0, nItem;
nLen = m_csMessage.GetLength();

for (i = 0 ; i < nLen ; i++){
nItem = i%2 ? ((int)m_csMessage.GetAt(i)-48)*1 :
((int)m_csMessage.GetAt(i)-48)*3;
nSum += nItem;
}

nSum %= 10;
return (10-nSum)%10;
}

Thanks
 
B

Barry Kelly

Chris said:
Can someone help me convert thi to c# or vb.net

Calculate()

C# needs a return type on its method declarations:

public static int Calculate()
{
int i, nLen, nSum = 0, nItem;
nLen = m_csMessage.GetLength();

for (i = 0 ; i < nLen ; i++){
nItem = i%2 ? ((int)m_csMessage.GetAt(i)-48)*1 :
((int)m_csMessage.GetAt(i)-48)*3;
nItem = (i%2 != 0) ? ((int)m_csMessage.GetAt(i)-48)*1 :
((int)m_csMessage.GetAt(i)-48)*3;
nSum += nItem;
}

nSum %= 10;
return (10-nSum)%10;
}

The bulk of the body of this function is syntactically valid C# code,
except that use of an int expression as a predicate or boolean
expression isn't valid in C#.

Without knowing what m_csMessage is, it's hard to say more.

-- Barry
 
S

Stephany Young

And ... for the VB.Net version ...

Public Shared Function Calculate() As Integer

Dim i, nLen, nSum, nItem As Integer

nLen = m_csMessage.GetLength()

For i = 0 To nLen -1
If i Mod 2 <> 0 Then
nItem = (CType(m_csMessage.GetAt(i), Integer) - 48) * 1
Else
nItem = (CType(m_csMessage.GetAt(i), Integer) - 48) * 3
End If
nSum += nItem
Next

nSum = nSum Mod 10

Return (10 - nSum) Mod 10

End Function

And for the compact variation ...

Public Shared Function Calculate() As Integer

Dim _sum As Integer = 0

For _i As Integer = 0 To m_csMessage.GetLength() - 1
_sum += (CType(m_csMessage.GetAt(_i), Integer) * 3 * (-(_i Mod 2) + 1)
Next

Return (10 - (_sum Mod 10)) Mod 10

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top