## [Solved] Advice 0n converting this VB code

Post your Gambas programming questions here.
AndyGable
Posts: 66
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

### [Solved] Advice 0n converting this VB code

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: Select all

``````   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
Last edited by AndyGable on Tuesday 20th April 2021 7:40pm, edited 1 time in total.

PJBlack
Posts: 131
Joined: Tuesday 09th June 2020 10:26pm

### Re: Advice 0n converting this VB code

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))

AndyGable
Posts: 66
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

### Re: Advice 0n converting this VB code

PJBlack wrote:
Monday 19th April 2021 8:58pm
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)

PJBlack
Posts: 131
Joined: Tuesday 09th June 2020 10:26pm

### Re: Advice 0n converting this VB code

EDIT: changed to another version
```' Gambas module file

Public Sub main()

Print Test(4711)

End

Public Function test(sBarcode As String) As String

Dim iTotalOdd As Integer = 0
Dim iTotalEven As Integer = 0
Dim sTotal As String
Dim iBarcodeLocal As Integer
Dim sBarcodeNumberInHouse As String

For i As Integer = 1 To Len(Trim(sBarcode)) Step 2
iTotalOdd += Val(String.Mid(sBarcode, i, 1))
iTotalEven += Val(String.Mid(sBarcode, i + 1, 1))
Next

sTotal = Str((iTotalOdd * 3) + iTotalEven)

iBarcodeLocal = 10 - IIf(Val(String.Right(sTotal, 1)) = 0, 10, Val(String.Right(sTotal, 1)))

sBarcodeNumberInHouse = sBarcode & Str(iBarcodeLocal)

Return sBarcodeNumberInHouse

End```

BruceSteers
Posts: 474
Joined: Thursday 23rd July 2020 5:20pm

### Re: Advice 0n converting this VB code

The function just checks the last digit is zero so there are other ways.
Like using % that gives the remainder of a division
eg.
```Dim BarcodeLocal As Integer = 10 - IIf(Total % 10 = 0, 10, Total % 10)
```
Wishing well
Bruce

If at first you don't succeed , try it differently.

PJBlack
Posts: 131
Joined: Tuesday 09th June 2020 10:26pm

### Re: Advice 0n converting this VB code

reduced to max ...
```'' Modulo10 Prüfziffernberchnung für u.a. EAN13 Barcodes
Public Function CheckDigit(sBarcode As String) As String

Dim iTotalOdd, iTotalEven, iTotal As Integer

For i As Integer = 1 To Len(Trim(sBarcode)) Step 2
iTotalOdd += Val(String.Mid(sBarcode, i, 1))
iTotalEven += Val(String.Mid(sBarcode, i + 1, 1))
Next

iTotal = ((iTotalOdd * 3) + iTotalEven) % 10

Return sBarcode & Str(10 - IIf(iTotal = 0, 10, iTotal))

End
```

cogier
Posts: 690
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

### Re: Advice 0n converting this VB code

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.
```Public Sub Form_Open()

Print Calc(978047008293) ''5
Print Calc(978156592400) ''0
Print Calc(978059600025) ''7

End

Public Sub Calc(sBarcode As String) As String

Dim iLoop, iTotal, iCalc As Integer

For iLoop = Len(sBarcode) - 1 To 1 Step -2
iTotal += Val(sBarcode[iLoop]) * 3
iTotal += Val(sBarcode[iLoop - 1])
Next

If Right(Str(iTotal)) <> "0" Then iCalc = Round((iTotal / 10) + 0.5) * 10 Else iCalc = Round(iTotal / 10) * 10

Return sBarcode & Str(iCalc - iTotal)

End```

AndyGable
Posts: 66
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

### Re: Advice 0n converting this VB code

cogier wrote:
Tuesday 20th April 2021 9:01am
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.
```Public Sub Form_Open()

Print Calc(978047008293) ''5
Print Calc(978156592400) ''0
Print Calc(978059600025) ''7

End

Public Sub Calc(sBarcode As String) As String

Dim iLoop, iTotal, iCalc As Integer

For iLoop = Len(sBarcode) - 1 To 1 Step -2
iTotal += Val(sBarcode[iLoop]) * 3
iTotal += Val(sBarcode[iLoop - 1])
Next

If Right(Str(iTotal)) <> "0" Then iCalc = Round((iTotal / 10) + 0.5) * 10 Else iCalc = Round(iTotal / 10) * 10

Return sBarcode & Str(iCalc - iTotal)

End```

Thank-you cogier that works perfectly with both EAN8 and EAN13 barcode numbers