[Solved] Advice 0n converting this VB code

Post

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

I Hope someone can help me

I am trying to convert this code from Visual BASIC to Gambas

the code works out the check digit for a barcode number using Mod10CheckDigit (Based on https://www.freevbcode.com/ShowCode.asp?ID=1035)

Code

   Dim i As Integer = 0
    Dim TotalOdd As Integer = 0
    Dim TotalEven As Integer = 0
    Dim Total As Integer = 0

    Barcode = Trim(Barcode)
    'get odd numbers

    For i = 1 To Len(Barcode) Step 2
        TotalOdd = TotalOdd + CInt(Mid(Barcode, i, 1))
    Next

    TotalOdd = TotalOdd * 3

    'get even numbers
    i = 0
    For i = 2 To Len(Barcode) Step 2
        TotalEven = TotalEven + CInt(Mid(Barcode, i, 1))
    Next

    Total = TotalOdd + TotalEven

    Dim BarcodeLocal As Integer = 10 - IIf(Right(Total, 1) = 0, 10, Right(Total, 1))
    BarcodeNumberInHouse = Barcode & BarcodeLocal

but when the app runs I get on the following Line

Dim BarcodeLocal As Integer = 10 - IIf(Right(Total, 1) = 0, 10, Right(Total, 1))

Type Mismatch: wanted string, got integer instead in Global:274

I hope someone can advice me
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
as far as i can see at fast looking … you compare a char (right(Total, 1)) with an integer as you been told  b y the interpreter ;)

as you dim an integer you may should do something like -> val(right(total,1))
Online now: No Back to the top

Post

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

PJBlack said

as far as i can see at fast looking … you compare a char (right(Total, 1)) with an integer as you been told  b y the interpreter ;)

as you dim an integer you may should do something like -> val(right(total,1))

Hi PJ,

I tried that but I am still getting the same error. (Im struggling with this in Gambas hell I even struggled with this in VB and VB.net lol)
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
EDIT: changed to another version

Code (gambas)

  1. ' Gambas module file
  2.  
  3. Public Sub main()
  4.  
  5.     Print Test(4711)
  6.  
  7.  
  8. Public Function test(sBarcode As String) As String
  9.  
  10.     Dim iTotalOdd As Integer = 0
  11.     Dim iTotalEven As Integer = 0
  12.     Dim sTotal As String
  13.     Dim iBarcodeLocal As Integer
  14.     Dim sBarcodeNumberInHouse As String
  15.  
  16.     For i As Integer = 1 To Len(Trim(sBarcode)) Step 2
  17.         iTotalOdd += Val(String.Mid(sBarcode, i, 1))
  18.         iTotalEven += Val(String.Mid(sBarcode, i + 1, 1))
  19.     Next
  20.  
  21.     sTotal = Str((iTotalOdd * 3) + iTotalEven)
  22.  
  23.     iBarcodeLocal = 10 - IIf(Val(String.Right(sTotal, 1)) = 0, 10, Val(String.Right(sTotal, 1)))
  24.  
  25.     sBarcodeNumberInHouse = sBarcode & Str(iBarcodeLocal)
  26.  
  27.    Return sBarcodeNumberInHouse
  28.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
The function just checks the last digit is zero so there are other ways.
Like using % that gives the remainder of a division
eg.

Code (gambas)

  1. Dim BarcodeLocal As Integer = 10 - IIf(Total % 10 = 0, 10, Total % 10)
  2.  
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
reduced to max …

Code (gambas)

  1. '' Modulo10 Prüfziffernberchnung für u.a. EAN13 Barcodes
  2. Public Function CheckDigit(sBarcode As String) As String
  3.  
  4.     Dim iTotalOdd, iTotalEven, iTotal As Integer
  5.  
  6.     For i As Integer = 1 To Len(Trim(sBarcode)) Step 2
  7.         iTotalOdd += Val(String.Mid(sBarcode, i, 1))
  8.         iTotalEven += Val(String.Mid(sBarcode, i + 1, 1))
  9.     Next
  10.  
  11.     iTotal = ((iTotalOdd * 3) + iTotalEven) % 10
  12.  
  13.     Return sBarcode & Str(10 - IIf(iTotal = 0, 10, iTotal))
  14.  
  15.  
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Guru
cogier is in the usergroup ‘Guru’
Which type of barcode are you intending to use? If it is 128B then I have put the necessary code on the Farm called Own_Barcode.

EDIT

I have looked further into this and if you are trying to create codes for EAN barcodes your code needs to calculate the odd and even numbers from right to left see here. The following code seems to work for EAN barcodes used on books.

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Print Calc(978047008293) ''5
  4.   Print Calc(978156592400) ''0
  5.   Print Calc(978059600025) ''7
  6.  
  7.  
  8. Public Sub Calc(sBarcode As String) As String
  9.  
  10.   Dim iLoop, iTotal, iCalc As Integer
  11.  
  12.   For iLoop = Len(sBarcode) - 1 To 1 Step -2
  13.     iTotal += Val(sBarcode[iLoop]) * 3
  14.     iTotal += Val(sBarcode[iLoop - 1])
  15.   Next
  16.  
  17.   If Right(Str(iTotal)) <> "0" Then iCalc = Round((iTotal / 10) + 0.5) * 10 Else iCalc = Round(iTotal / 10) * 10
  18.  
  19.   Return sBarcode & Str(iCalc - iTotal)
  20.  
Online now: No Back to the top

Post

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

cogier said

Which type of barcode are you intending to use? If it is 128B then I have put the necessary code on the Farm called Own_Barcode.

EDIT

I have looked further into this and if you are trying to create codes for EAN barcodes your code needs to calculate the odd and even numbers from right to left see here. The following code seems to work for EAN barcodes used on books.

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Print Calc(978047008293) ''5
  4.   Print Calc(978156592400) ''0
  5.   Print Calc(978059600025) ''7
  6.  
  7.  
  8. Public Sub Calc(sBarcode As String) As String
  9.  
  10.   Dim iLoop, iTotal, iCalc As Integer
  11.  
  12.   For iLoop = Len(sBarcode) - 1 To 1 Step -2
  13.     iTotal += Val(sBarcode[iLoop]) * 3
  14.     iTotal += Val(sBarcode[iLoop - 1])
  15.   Next
  16.  
  17.   If Right(Str(iTotal)) <> "0" Then iCalc = Round((iTotal / 10) + 0.5) * 10 Else iCalc = Round(iTotal / 10) * 10
  18.  
  19.   Return sBarcode & Str(iCalc - iTotal)
  20.  


Thank-you cogier that works perfectly with both EAN8 and EAN13 barcode numbers
Online now: No Back to the top
1 guest and 0 members have just viewed this.