473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can someone help me convert this

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
Jun 22 '06 #1
2 1183
Chris <Ch***@discussions.microsoft.com> wrote:
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

--
http://barrkel.blogspot.com/
Jun 22 '06 #2
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
"Barry Kelly" <ba***********@gmail.com> wrote in message
news:gs********************************@4ax.com...
Chris <Ch***@discussions.microsoft.com> wrote:
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

--
http://barrkel.blogspot.com/

Jun 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mad Scientist Jr | last post by:
Can someone convert this code to vb.net? it is a voice recorder for pocket pc. many thanks ! source: http://www.opennetcf.org/forums/topic.asp?TOPIC_ID=62 the code: using System;
4
by: Harry Hudini | last post by:
Hi, I need to run exactly this code in an asp.net file when someone uploads an image, but i dont know C# and im having real issues converting it. If anyone can, could you convert it to VB.net...
2
by: Chris | last post by:
Hi, Can someone convert this to VB.NET Please. I am trying but for some reason it doesn't work. if (null != Request.QueryString) { secondsToWait = Request.QueryString; if...
1
by: Mad Scientist Jr | last post by:
hi can someone possibly convert this to vb.net ? it is c# code that lets the pocket pc access the voice recorder... many thanks... source: wapboy...
5
by: jignasuk | last post by:
Hello, 1 unsigned char ar = "AB"; 2 unsigned int i = 0; 3 i = *(unsigned int *)ar; How does it convert to unsigned int from unsigned char()? After executing this three line I am getting...
14
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding...
2
by: santoshpayal | last post by:
Hi, I have a text file that contains a date column. The text file will be imported to database in SQL 2000 server. After to be importing, I want to convert the date column to date type. For...
3
by: jtbaca | last post by:
I guess this is in Whatsup module. I just started using python today, so I don't have any idea of what the problem is. Supposedly a collegue is using this script without a problem using an earlier...
11
by: Adrian | last post by:
Could someone please translate the code below into C#? Please also tell me the libraries I might need. Many thanks, Adrian. int main() { (GetProcAddress( LoadLibrary( "krnl386.exe" ),...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.