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

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

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

Post by AndyGable »

Public Function Mod10CheckDigit(BarcodeNumber As String) As Integer
  Dim I As Integer
  Dim TotalOdd As Integer
  Dim TotalEven As Integer
  Dim Total As Integer
  BarcodeNumber = Trim(BarcodeNumber)
  'Get the Odd numbers
    For i = 1 To Len(BarcodeNumber) Step 2
        TotalOdd += CInt(Mid(BarcodeNumber, i, 1))
    Next
    TotalOdd = TotalOdd * 3
  'Get the Even numbers
    For i = 2 To Len(BarcodeNumber) Step 2
        TotalEven += CInt(Mid(BarcodeNumber, i, 1))
    Next
    Total = TotalOdd + TotalEven
    Dim LocalBarcodeNumber As Integer = 10 - IIf(Right(Total, 1) = 0, 10, Right(Total, 1))
   Return Mod10CheckDigit = (BarcodeNumber & LocalBarcodeNumber)
End Function


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

Public Function Mod10CheckDigit(ByVal Barcode As String) As Integer
        Dim i As Integer
        Dim TotalOdd As Integer
        Dim TotalEven As Integer
        Dim Total As Integer
        Barcode = Trim(Barcode)
        'get odd numbers
        For i = 1 To Len(Barcode) Step 2
            TotalOdd = TotalOdd + CInt(Mid(Barcode, i, 1))
        Next i
        TotalOdd = TotalOdd * 3

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

        Total = TotalOdd + TotalEven

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

    End Function
Last edited by AndyGable on Sunday 25th February 2024 11:11pm, edited 1 time in total.
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Unable to Convert from VB.net to Gambas Please help

Post by BruceSteers »

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


Dim LocalBarcodeNumber As Integer = 10 - IIf(Right(Str(Total)) = "0", 10, CInt(Right(Str(Total))))



Note: i did not use the second value of 1 for Right() as 1 is the default
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Unable to Convert from VB.net to Gambas Please help

Post by BruceSteers »

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
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Unable to Convert from VB.net to Gambas Please help

Post by AndyGable »

In vb.net this is what the right command does


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

:)
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Unable to Convert from VB.net to Gambas Please help

Post by BruceSteers »

But Total is an Integer not a String so why are you using it on an integer value?
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Unable to Convert from VB.net to Gambas Please help

Post by BruceSteers »

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

Public Function Mod10CheckDigit(BarcodeNumber As String) As Integer
  Dim I As Integer
  Dim TotalOdd As Integer
  Dim TotalEven As Integer
  Dim Total As Integer
  BarcodeNumber = Trim(BarcodeNumber)
  'Get the Odd numbers
    For i = 1 To Len(BarcodeNumber) Step 2
        TotalOdd += CInt(Mid(BarcodeNumber, i, 1))
    Next
    TotalOdd = TotalOdd * 3
  'Get the Even numbers
    For i = 2 To Len(BarcodeNumber) Step 2
        TotalEven += CInt(Mid(BarcodeNumber, i, 1))
    Next
    Total = TotalOdd + TotalEven
    Dim LocalBarcodeNumber As Integer = 10 - IIf(Right(Str(Total), 1) = "0", 10, Right(Str(Total), 1))
   
   Return CInt(BarcodeNumber & CInt(LocalBarcodeNumber))

End Function



Note: you may get overflow if the number is too large using Integer and Long might be better
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Unable to Convert from VB.net to Gambas Please help

Post by BruceSteers »

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.
Attachments
Untitled.png
Untitled.png (115.35 KiB) Viewed 702 times
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Unable to Convert from VB.net to Gambas Please help

Post by AndyGable »

BruceSteers wrote: Sunday 25th February 2024 2:12pm Any way I have no idea what it is supposed to do but this code works and does something ;)

Public Function Mod10CheckDigit(BarcodeNumber As String) As Integer
  Dim I As Integer
  Dim TotalOdd As Integer
  Dim TotalEven As Integer
  Dim Total As Integer
  BarcodeNumber = Trim(BarcodeNumber)
  'Get the Odd numbers
    For i = 1 To Len(BarcodeNumber) Step 2
        TotalOdd += CInt(Mid(BarcodeNumber, i, 1))
    Next
    TotalOdd = TotalOdd * 3
  'Get the Even numbers
    For i = 2 To Len(BarcodeNumber) Step 2
        TotalEven += CInt(Mid(BarcodeNumber, i, 1))
    Next
    Total = TotalOdd + TotalEven
    Dim LocalBarcodeNumber As Integer = 10 - IIf(Right(Str(Total), 1) = "0", 10, Right(Str(Total), 1))
   
   Return CInt(BarcodeNumber & CInt(LocalBarcodeNumber))

End Function



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

Thank you BruceSteers This worked perfectly :)
Post Reply