HighTechTalks DotNet Forums  

How to generate Sequential String ?

Dotnet Academic General Discussions microsoft.public.dotnet.academic


Discuss How to generate Sequential String ? in the Dotnet Academic General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
M.M Ansari
 
Posts: n/a

Default How to generate Sequential String ? - 06-09-2004 , 11:39 PM






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









Reply With Quote
  #2  
Old   
Peter van der Goes
 
Posts: n/a

Default Re: How to generate Sequential String ? - 06-13-2004 , 02:21 PM







"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.




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.