如何生成收据号码

时间:2011-08-12 21:34:37

标签: vb.net visual-studio-2010

在Stack Overflow上,我找到了问题 Generate a receipt number in this range

在回答中(来自Adam Maras)我发现这段代码(对我来说非常有趣):

Private Const FirstReceiptNumber As String = "GA00000"

Public Function GenerateReceiptNumber(ByVal lastNumber As String) As String

    If lastNumber.Length <> 7 Then
        Throw New ArgumentException("Incorrect length", "lastNumber")
    End If

    If lastNumber.StartsWith("G") = False Then
        Throw New ArgumentException("Incorrect start character", "lastNumber")
    End If

    Dim letterPortion As Char = lastNumber.Chars(1)

    If letterPortion < "A"c Or letterPortion > "Z"c Then
        Throw New ArgumentException("Non-letter second character", "lastNumber")
    End If

    If letterPortion = "I"c Or letterPortion = "O"c Then
        Throw New ArgumentException("Invalid second character", "lastNumber")
    End If

    Dim numericPortionString As String = lastNumber.Substring(2)
    Dim numericPortion As Integer

    If Integer.TryParse(numericPortionString, numericPortion) = False Then
        Throw New ArgumentException("Invalid numeric portion", "lastNumber")
    End If

    If numericPortion = 99999 Then
        If letterPortion = "Z"c Then
            Throw New ArgumentException("No more receipt numbers possible", "lastNumber")
        End If

        numericPortion = 0
        letterPortion = letterPortion + Chr(1)

        If letterPortion = "I"c Or letterPortion = "O"c Then
            letterPortion = letterPortion + Chr(1)
        End If
    Else
        numericPortion = numericPortion + 1
    End If

    Return String.Format("G{0}{1:00000}", letterPortion, numericPortion)
End Function

我将其粘贴到Visual Studio 2010表单中,然后尝试查看Form_Load上发生的情况。

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = GenerateReceiptNumber() '<--HERE IS THE ERROR
End Sub

但在GenerateReceiptNumber()显示错误。

当我打开此表单时,如何使用该代码在TextBox1中为我提供收据编号?

下次使用Form_Load时,收据编号必须为+1。

2 个答案:

答案 0 :(得分:0)

很高兴你找到了我对上一个问题的答案!

现在,通过我在另一个问题中提供mr_dunski的解决方案,他要求算法根据之前的数字创建收据编号。所以你需要从一个初始数开始才能开始序列:

GenerateReceiptNumber(FirstReceiptNumber)

GenerateReceiptNumber("GA00000")

答案 1 :(得分:0)

您可以在此模式下书写:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = GenerateReceiptNumber("GA00000")
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = GenerateReceiptNumber(FirstReceiptNumber)
End Sub