逐步生成唯一的序列号

时间:2015-01-21 16:14:08

标签: vb.net

我正在编写一个vb.net程序,用于生成一个三位数的序列号,用于打印条形码。

要求是计数器必须计算:

001 - 999,A00 - A99,B00 - B99,...,Z00 - Z99

我不能使用字母O和I

此代码只是将传递给它的值增加1.我首先检查值是否为< = 998,如果是,则返回3位数值。我不得不把它放在一个Try语句中,因为传递值'A00'会导致错误。

一旦我点击Z99,代码仍在破坏。

问题:如果下一个序列号= Z90并且用户想要打印35个条形码,我需要在操作开始之前停止操作并警告用户只剩下10个可用的序列号

此外,我也希望就如何以更好的方式完成这项工作提出建议,我们将非常感谢任何建议

    Public Shared Function NextSerial(ByVal value As String) As String

    Try
        If value <= 998 Then
            value += 1
            Return ZeroPad(value, 3)
        End If

    Catch ex As Exception

    End Try
    Const chars As String = "ABCDEFGHJKLMNPQRSTUVWXYZ"
    Dim threenumber As String = ZeroPad(value, 3) 'ensure value is 3 digits.
    Dim alpha As String = threenumber.Substring(0, 1).ToUpper() ' 1st digit
    Dim beta As String = threenumber.Substring(1, 2) 'remaining two digits
    Dim newNumber As String
    Dim nextletter As String

    If beta = "99" Then
        beta = "00"
        nextletter = chars.Substring((chars.IndexOf(alpha, System.StringComparison.Ordinal) + 1), 1)
        newNumber = nextletter + beta
        Return newNumber

    Else
        beta += 1
        newNumber = alpha + ZeroPad(beta, 2)
        Return newNumber

    End If
End Function

Private Shared Function ZeroPad(ByVal number As String, ByVal toLength As Integer) As String
    ZeroPad = number

    'add the necessary leading zeroes to build it up to the desired length.
    Do Until Len(ZeroPad) >= toLength
        ZeroPad = "0" & ZeroPad
    Loop
End Function

3 个答案:

答案 0 :(得分:1)

我认为你可以通过假设你的第一个角色是数百个&#39;来做到这一点。并转换为数字并递增:

Private Function NextSerial(value As String) As String
    Const chars As String = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"

    Dim numericValue As Integer = 100 * (chars.IndexOf(value.Substring(0, 1))) + Integer.Parse(value.Substring(1, 2))
    numericValue += 1

    Return chars.Substring(numericValue \ 100, 1) + (numericValue Mod 100).ToString.PadLeft(2, "0")
End Function

当然,您应该在函数开始时执行一些错误检查,以确保已将有效的序列号传递给该函数。我还会将这个函数放入一个类中,并添加一些函数,如isValid,SerialsRemaining,也许还有一个函数来检索多个连续符的列表。

答案 1 :(得分:0)

我创建了代表每个数字位置中每个可用字符的常量字符串。然后我使用索引来查找当前序列号和位置的位置。向前移动了一个数字以获得下一个序列号。这将始终提供下一个序列,直到您用完数字。

注意:这段代码可以很容易地变得更紧凑,但我认为它可能更清晰。

Const charString1 As String = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
Const charString2 As String = "0123456789"
Const charString3 As String = "0123456789"

Public Function NextSerial(ByVal value As String) As String

    ' ensures the input is three chars long
    Dim threenumber As String = Right("000" & value, 3)

    Dim char1 As String = threenumber.Substring(0, 1)
    Dim char2 As String = threenumber.Substring(1, 1)
    Dim char3 As String = threenumber.Substring(2, 1)
    Dim char1Pos As Integer = charString1.IndexOf(char1)
    Dim char2Pos As Integer = charString2.IndexOf(char2)
    Dim char3Pos As Integer = charString3.IndexOf(char3)

    If char1Pos = -1 Or char2Pos = -1 Or char3Pos = -1 Then Throw New Exception("Invalid serial number format")

    ' move to next serial number
    char3Pos += 1

    If char3Pos > charString3.Length() - 1 Then
        char3Pos = 0
        char2Pos += 1
    End If
    If char2Pos > charString2.Length() - 1 Then
        char2Pos = 0
        char1Pos += 1
    End If
    If char1Pos > charString1.Length() - 1 Then Throw New Exception("Out of serial numbers!")

    Return charString1.Substring(char1Pos, 1) & charString2.Substring(char2Pos, 1) & charString3.Substring(char3Pos, 1)

End Function

答案 2 :(得分:0)

我建议您使用整数进行所有检查和计算,并仅转换为序列号进行显示。知道剩余的序列号会更容易。

您的序列号类似于整数,但超过100的所有内容都是字母而不是数字。

注意:添加错误检查非常重要,这假设所有输入都有效。

Module Module1

    Sub Main()

        Console.WriteLine(SerialNumber.ConvertSerialNumberToInteger("D22"))
        Console.WriteLine(SerialNumber.ConvertIntegerToSerialNumber(322))
        Console.WriteLine(SerialNumber.GetAvailableSerialNumber("Z90"))

        For Each sn As String In SerialNumber.GetNextSerialNumber("X97", 5)
            Console.WriteLine(sn)
        Next

        Console.ReadLine()

    End Sub

End Module

Class SerialNumber

    Private Const _firstPart As String = "ABCDEFGHJKLMNPQRSTUVWXYZ"

    Public Shared Function ConvertSerialNumberToInteger(ByVal serialNumber As String) As Integer

        Return (_firstPart.IndexOf(serialNumber(0)) * 100) + Integer.Parse(serialNumber.Substring(1, 2))
    End Function

    Public Shared Function ConvertIntegerToSerialNumber(ByVal value As Integer) As String

        Return _firstPart(value \ 100) & (value Mod 100).ToString("00")
    End Function

    Public Shared Function GetAvailableSerialNumber(ByVal serialNumber As String)

        Dim currentPosition As Integer
        Dim lastPosition As Integer

        currentPosition = ConvertSerialNumberToInteger(serialNumber)
        lastPosition = ConvertSerialNumberToInteger("Z99")

        Return lastPosition - currentPosition
    End Function

    Public Shared Function GetNextSerialNumber(ByVal serialNumber As String, ByVal amount As Integer) As List(Of String)

        Dim newSerialNumbers As New List(Of String)
        Dim currentPosition As Integer

        currentPosition = ConvertSerialNumberToInteger(serialNumber)

        For i As Integer = 1 To amount
            newSerialNumbers.Add(ConvertIntegerToSerialNumber(currentPosition + i))
        Next

        Return newSerialNumbers
    End Function

End Class