Page 1 of 1

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

Posted: Saturday 24th February 2024 11:08pm
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

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

Posted: Sunday 25th February 2024 1:37am
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

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

Posted: Sunday 25th February 2024 1:51am
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

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

Posted: Sunday 25th February 2024 1:05pm
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.

:)

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

Posted: Sunday 25th February 2024 1:46pm
by BruceSteers
But Total is an Integer not a String so why are you using it on an integer value?

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

Posted: Sunday 25th February 2024 2:12pm
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

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

Posted: Sunday 25th February 2024 2:18pm
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.

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

Posted: Sunday 25th February 2024 11:11pm
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 :)