"M.M Ansari" <mudasar_ansari (AT) hotmail (DOT) com> wrote
Quote:
hi all;
can any body tell me how i would generate sequential number in vb.net.....
like A001, A002, A003 or BA01, BA02 ...
Any suggestion or link for this sort of algorithm ?
Thanks in advance..
Ansari
Before offering you a crude solution, I want to make it clear that I am
|
inferring from the wording in your question that there will be several
"knowns" in your scenario.
1. That the alpha portion of the string in known and constant
2. That the maximum range of you sequence is known (i.e. that A001, A002,
A003, ... implies a range of A000 through A999).
3. That you need the arithmetic done in decimal, rather than in (say) hex.
Here is a function that works for A001, etc.
The code should reindent itself when you put it in the editor.
Private Function GenerateNextInSequence(ByVal current As String) As String
'This function procedure assumes a sequence of A001 through A999
If current = "A999" Then
Return "Sequence exhausted"
Else
Dim numberPart As String
Dim convertNumber As Integer
Dim alphaPart As String
Dim returnString As String
'Split the string into alpha and numeric portions
alphaPart = current.Substring(0, 1)
numberPart = current.Substring(1, 3)
'Convert the numeric portion to an integer
convertNumber = Convert.ToInt32(numberPart.Trim("0"))
'Add 1
convertNumber += 1
numberPart = Convert.ToString(convertNumber)
'Reassemble the string
If convertNumber < 10 Then
returnString = alphaPart & "00" & numberPart
Else
If convertNumber < 100 Then
returnString = alphaPart & "0" & numberPart
Else
returnString = alphaPart & numberPart
End If
End If
Return returnString
End If
End Function
Here is an experiment using hexadecimal:
Private Function doItInHex(ByVal input As String) As String
'This version assumes that the letters in your sequences will always be
'in the range A through F, so as to be translatable into Hexadecimal numbers
'It also assumes you don't mind hex arithmetic
Dim hexString As String
Dim decNumber As Long
'set up the hex string from the input
hexString = "&h" & input & "&"
'convert to a long integer
decNumber = Val(hexString)
'increment the number
decNumber += 1
'convert back to a hex string
hexString = Hex(decNumber)
Return hexString
End Function
--
Peter [MVP Visual Developer]
Jack of all trades, master of none.