Click here to Skip to main content
15,067,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

I have the code bellow to work out the last number of a barcode number but the issue i am having is that Excel gives me the correct last number but VB.NET does not, my code is bellow.

VB
Dim strMembershipNumber As String
'Clear the console Window
Console.Clear()
'Get the Membership number from user
Console.WriteLine("Membership number to Barcode")
Console.WriteLine("What is the membership number?")
Console.WriteLine("This is the number on the back of the card not the Account Number")
Console.WriteLine("---------------------------------------------------------")
strMembershipNumber = Console.ReadLine

Dim strNumbers(15) As String
Dim intFirstSet As Integer = "279"
Dim intCount As Integer = strMembershipNumber.Length
Dim i As Integer

Do Until i = intCount
    strNumbers(i) = strMembershipNumber.Substring(0, 1)
    strMembershipNumber = strMembershipNumber.Remove(0, 1)
    i = i + 1
Loop



Dim intSum1 As Integer = strNumbers(14) + strNumbers(12) + strNumbers(10) + strNumbers(8)+ strNumbers(6) + 7
Dim intSum2 As Integer = strNumbers(13) + strNumbers(11) + strNumbers(9) + strNumbers(7)  + 9 + 2

Dim intMath As Integer = (intSum1 * 3) + intSum2
Dim intMod As Integer = intMath Mod 10
intMath = 10 - intMod
intMod = intMath Mod 10

Dim strBarcode As String = intFirstSet & strNumbers(6) & strNumbers(7) & strNumbers(8) & strNumbers(9) & strNumbers(10) & strNumbers(11) & strNumbers(12)                                     & strNumbers(13) & strNumbers(14) & intMod
Console.WriteLine(strBarcode)
Console.ReadLine()


The above code returns 4 for number 6008943507588309.

The excel code is
MOD(10-MOD(SUM(Q21,O21,M21,K21,G21)*3+SUM(P21,N21,L21,J21,H21,F21),10)10)

this returns 7 for the same number 6008943507588309.

For the life of me i don't know where i have gone wrong, please help...
Posted
Comments
Sandeep Mewara 29-May-12 5:46am
   
You sure that the indexes iused here are correct:
Dim intSum1 As Integer = strNumbers(14) + strNumbers(12) + strNumbers(10) + strNumbers(8)+ strNumbers(6) + 7
Dim intSum2 As Integer = strNumbers(13) + strNumbers(11) + strNumbers(9) + strNumbers(7) + 9 + 2

I've gave you an answer in the previous post[^].

Because you have an answer, i shoudn't help you... But i decide to show you how to achieve that using class.

Steps to do:
1) Create new project (Windows application)
2) Place on the form 3 controls:
- Label1
- TextBox1
- Button1 and rename it Name as CmdVerify
3) Insert into project 3 items:
- new class (change its name to: TBarCodeVer)
- new interface (change its name to: IBarCodeVer)
- new module (change its name to: ModMain)
4) copy the below parts of code and paste it into destination modules
IBarCodeVer.vb
VB
Public Interface IBarCodeVer

    Property MemberShipNumber() As String
    Function BarCode() As String

End Interface


TBarCodeVer.vb
VB
Public Class TBarCodeVer
    Implements IBarCodeVer

    Private sNumber As String = String.Empty
    Private Const sFirstSet As String = "279"

    Public Sub New()
        'do nothing ;)
    End Sub

    Public Property MemberShipNumber() As String Implements IBarCodeVer.MemberShipNumber
        Get
            Return sNumber
        End Get
        Set(ByVal value As String)
            sNumber = value
        End Set
    End Property

    Function BarCode() As String Implements IBarCodeVer.BarCode
        Dim sNumbers() As String = Nothing, sTmpNumber As String = String.Empty
        Dim i As Integer = 0, iCount As Integer, sRetVal As String = String.Empty
        Dim iSum As Integer = 0

        Try
            iCount = sNumber.Length
            If iCount <> 16 Then
                MsgBox("The length of MemberShipNumber must be equal 16!", MsgBoxStyle.Exclamation, "Error!")
                Exit Try
            End If

            sTmpNumber = sNumber
            Do Until i = iCount
                ReDim Preserve sNumbers(i)
                sNumbers(i) = sTmpNumber.Substring(0, 1)
                sTmpNumber = sTmpNumber.Remove(0, 1)
                i = i + 1
            Loop

            '=MOD(10-MOD(SUM(Q21;O21;M21;K21;I21;G21)*3+Sum(P21;N21;L21;J21;H21;F21);10);10)
            iSum = (Integer.Parse(sNumbers(14)) + Integer.Parse(sNumbers(12)) + Integer.Parse(sNumbers(10)) + Integer.Parse(sNumbers(8)) + Integer.Parse(sNumbers(6)) + 7) * 3 'previous was:  + Integer.Parse(sNumbers(4)) + Integer.Parse(sNumbers(2))) * 3
            iSum = iSum + Integer.Parse(sNumbers(13)) + Integer.Parse(sNumbers(11)) + Integer.Parse(sNumbers(9)) + Integer.Parse(sNumbers(7)) + 11 'previous was: + Integer.Parse(sNumbers(5)) + Integer.Parse(sNumbers(3)) + Integer.Parse(sNumbers(1))
            iSum = (10 - (iSum Mod 10)) Mod 10

            sRetVal = sFirstSet & sNumbers(6) & sNumbers(7) & sNumbers(8) & sNumbers(9) & sNumbers(10) & sNumbers(11) & sNumbers(12) & sNumbers(13) & sNumbers(14) & iSum.ToString

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
        End Try

        Return sRetVal
    End Function

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class


ModMain.vb
VB
Module ModMain
    'declare variable to store your class object by the interface
    Public oBarCode As IBarCodeVer = Nothing
End Module



Form1.vb
VB
Public Class Form1

    Private Sub CmdVerify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdVerify.Click
        oBarCode.MemberShipNumber = Me.TextBox1.Text
        MsgBox(oBarCode.BarCode, MsgBoxStyle.Information, "BarCode for number: " & oBarCode.MemberShipNumber)
    End Sub


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'initilize class
         oBarCode = New TBarCodeVer
    End Sub

    Protected Overrides Sub Finalize()
        'destroy class
        oBarCode = Nothing
        MyBase.Finalize()
    End Sub
End Class

5) Compile project

The above code returns 7 for the number: 6008943507588309
   
Comments
Sandeep Mewara 29-May-12 14:13pm
   
5!
Maciej Los 29-May-12 14:35pm
   
Thank you, Sandeep ;)
Nelek 29-May-12 17:27pm
   
Nice answer. +5
Maciej Los 29-May-12 18:28pm
   
Thank you, Nelek ;)
Without the values in the cells referred to in the excel equation or the strNumbers array no one is going to be able to help you.

However the problem is one of simple methodical debugging and you should be able to figure it out for yourself.

It should be sufficient to separate the excel equation into the same sub calculations that are shown in the VB code, i.e. intSum1, intSum2, intMath, intMod. Compare the VB and excel results of each one and you'll probably spot the error immediately.

Alan.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900