[Sloved] Unable to Convert from VB.net to Gambas Please help

Post

Posted
Rating:
#1 (In Topic #1193)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

Code (gambas)

  1. Public Function Mod10CheckDigit(BarcodeNumber As String) As Integer
  2.   Dim TotalOdd As Integer
  3.   Dim TotalEven As Integer
  4.   Dim Total As Integer
  5.   BarcodeNumber = Trim(BarcodeNumber)
  6.   'Get the Odd numbers
  7.     For i = 1 To Len(BarcodeNumber) Step 2
  8.         TotalOdd += CInt(Mid(BarcodeNumber, i, 1))
  9.     Next
  10.     TotalOdd = TotalOdd * 3
  11.   'Get the Even numbers
  12.     For i = 2 To Len(BarcodeNumber) Step 2
  13.         TotalEven += CInt(Mid(BarcodeNumber, i, 1))
  14.     Next
  15.     Total = TotalOdd + TotalEven
  16.     Dim LocalBarcodeNumber As Integer = 10 - IIf(Right(Total, 1) = 0, 10, Right(Total, 1))
  17.    Return Mod10CheckDigit = (BarcodeNumber & LocalBarcodeNumber)

I am getting a error on line Dim LocalBarcodeNumber As Integer = the error is "wanted sting got integer instead"

I though I had translated this right from VB.net but I must have missed something

Can some one spot where I have gone wrong as I have spent most of the day trying to figure out what I have done wrong.

If it works it should return a barcode number 10000007

Below is the VB Orginal code

Code (gambas)

  1. Public Function Mod10CheckDigit(ByVal Barcode As String) As Integer
  2.         Dim i As Integer
  3.         Dim TotalOdd As Integer
  4.         Dim TotalEven As Integer
  5.         Dim Total As Integer
  6.         Barcode = Trim(Barcode)
  7.         'get odd numbers
  8.         For i = 1 To Len(Barcode) Step 2
  9.             TotalOdd = TotalOdd + CInt(Mid(Barcode, i, 1))
  10.         Next i
  11.         TotalOdd = TotalOdd * 3
  12.  
  13.         'get even numbers
  14.         i = 0
  15.         For i = 2 To Len(Barcode) Step 2
  16.             TotalEven = TotalEven + CInt(Mid(Barcode, i, 1))
  17.         Next i
  18.  
  19.         Total = TotalOdd + TotalEven
  20.  
  21.         Dim BarcodeLocal As Integer = 10 - IIf(Right(Total, 1) = 0, 10, Right(Total, 1))
  22.         Mod10CheckDigit = Barcode & BarcodeLocal
  23.  
  24.  
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
You will have to see what the VB Right() function does.

In gambas it is a String function and can only be used on strings not integers.
you can use Str() and CInt() to convert to string and back to integer

Maybe something like this works…

Code (gambas)

  1.  
  2. Dim LocalBarcodeNumber As Integer = 10 - IIf(Right(Str(Total)) = "0", 10, CInt(Right(Str(Total))))
  3.  
  4.  

Note: i did not use the second value of 1 for Right() as 1 is the default
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
and that end line…

 Return Mod10CheckDigit = (Barcode & BarcodeLocal)

what's that about?  you can't use the function name in the return value like that in gambas.

might be something more like this…

 Return CInt(Str(Barcode & BarcodeLocal))

 :D
Online now: No Back to the top

Post

Posted
Rating:
#4
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
In vb.net this is what the right command does


returns a specified number of characters from the right side of a string.

:)
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
 But Total is an Integer not a String so  why are you using it on an integer value?
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
Any way I have no idea what it is supposed to do but this code works and does something ;)

Code (gambas)

  1. Public Function Mod10CheckDigit(BarcodeNumber As String) As Integer
  2.   Dim TotalOdd As Integer
  3.   Dim TotalEven As Integer
  4.   Dim Total As Integer
  5.   BarcodeNumber = Trim(BarcodeNumber)
  6.   'Get the Odd numbers
  7.     For i = 1 To Len(BarcodeNumber) Step 2
  8.         TotalOdd += CInt(Mid(BarcodeNumber, i, 1))
  9.     Next
  10.     TotalOdd = TotalOdd * 3
  11.   'Get the Even numbers
  12.     For i = 2 To Len(BarcodeNumber) Step 2
  13.         TotalEven += CInt(Mid(BarcodeNumber, i, 1))
  14.     Next
  15.     Total = TotalOdd + TotalEven
  16.     Dim LocalBarcodeNumber As Integer = 10 - IIf(Right(Str(Total), 1) = "0", 10, Right(Str(Total), 1))
  17.    
  18.    Return CInt(BarcodeNumber & CInt(LocalBarcodeNumber))
  19.  
  20.  
  21.  

Note: you may get overflow if the number is too large using Integer and Long might be better
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
 Maybe check out the gambas barcode readers on the farm and forget VB code translating.

Cogier who runs this site wrote 3 of them so i'm sure he will be great for any help you may need.

Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#8
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said

Any way I have no idea what it is supposed to do but this code works and does something ;)

Code (gambas)

  1. Public Function Mod10CheckDigit(BarcodeNumber As String) As Integer
  2.   Dim TotalOdd As Integer
  3.   Dim TotalEven As Integer
  4.   Dim Total As Integer
  5.   BarcodeNumber = Trim(BarcodeNumber)
  6.   'Get the Odd numbers
  7.     For i = 1 To Len(BarcodeNumber) Step 2
  8.         TotalOdd += CInt(Mid(BarcodeNumber, i, 1))
  9.     Next
  10.     TotalOdd = TotalOdd * 3
  11.   'Get the Even numbers
  12.     For i = 2 To Len(BarcodeNumber) Step 2
  13.         TotalEven += CInt(Mid(BarcodeNumber, i, 1))
  14.     Next
  15.     Total = TotalOdd + TotalEven
  16.     Dim LocalBarcodeNumber As Integer = 10 - IIf(Right(Str(Total), 1) = "0", 10, Right(Str(Total), 1))
  17.    
  18.    Return CInt(BarcodeNumber & CInt(LocalBarcodeNumber))
  19.  
  20.  
  21.  

Note: you may get overflow if the number is too large using Integer and Long might be better


Thank you BruceSteers This worked perfectly :)
Online now: No Back to the top
1 guest and 0 members have just viewed this.