|
-
Nov 4th, 2011, 08:49 AM
#1
Thread Starter
New Member
[RESOLVED] Auto incriment alpha-numeric
Good afternoon,
I have a problem (the latest) which I have not a clue where to start.
Scenario:
Four character suffix to a part number starting '0001', when it gets to '9999' it needs to start consuming alpha characters, so starts again at 'A001'.
Then when it gets to 'Z999' the next value needs to use 'AA01' and so on until the last possible value it can spit out is 'ZZZZ'.
Question:
How?
While ever it only uses numeric characters to start with it's not a problem, that I can do, how though do I get it to start using the alpha characters once these are all exhausted?
Thanks in advance,
Paul
-
Nov 4th, 2011, 10:17 AM
#2
Re: Auto incriment alpha-numeric
Wow, that's backwards, but I certainly understand why you are doing it that way. It makes it much harder, though. If you were to do it like this:
0000 through 0009, then 000A, 000B, etc.
that would be relatively easy, because you are effectively using a base-36 number with digits 0-9, A-Z. Otherwise, the sequence is like any other number. However, you are using a base-10 until you have reached 1000, then the most significant digit becomes base 36, and so forth.
I guess the first question is whether or not the order of those numbers justifies the pain of the implementation?
Assuming that you don't have any flexibility, this is a very clear case where you need a class (all it AlphaNumber) with a method called GetNext, because there's going to be a bunch of housekeeping involved figuring out what the next number is. I'd also have a base-36 class that would return the next value in base-36 (you don't need a base-10 class, because that one's too easy). I'd then have the AlphaNumber class maintain an integer counter variable and a base-36. There would also have to be a variable to keep track of where the base-10 integer rolls into the base-36 number. It would look interesting, but before I go any further, decide whether or not you really need the numbering system to work that way.
My usual boring signature: Nothing
 
-
Nov 4th, 2011, 10:24 AM
#3
Re: Auto incriment alpha-numeric
I agree with Shaggy that this would be much easier as a radix 36 number.
-
Nov 4th, 2011, 10:25 AM
#4
Re: Auto incriment alpha-numeric
 Originally Posted by Shaggy Hiker
I guess the first question is whether or not the order of those numbers justifies the pain of the implementation?
I think his ideas was so that they would always be in order on a standard alphabetical computer list:
9998
9999
a001
vs
001a
...
9998
9999
I think he may not have thought of mixing the letters into each base
0000-0009, 000a-000z
0010-0019, 001a-001z
because that would also work correctly in alphanumeric order on a computer screen.
Just some thoughts.
-
Nov 4th, 2011, 10:40 AM
#5
Re: Auto incriment alpha-numeric
I can see it both ways. The appearance of what he described would have a certain beauty, though it comes at the cost of being somewhat harder to implement. Therefore, he should decide whether the beauty is worth the cost, but the cost isn't prohibitive. A class could be written to handle this, and it would be a really nice use of a class, too, because it would be compartmentalizing the details of getting that next number. The interface would be simple, the application straightforward, and the details....messy, but hidden.
My usual boring signature: Nothing
 
-
Nov 4th, 2011, 10:51 AM
#6
Thread Starter
New Member
Re: Auto incriment alpha-numeric
I think the appearance is quite highly prized by the project manager, but if it does become a might pain in the rear to impliment then he will just have to be convinced otherwise.
In my humble opinion though, not that it matters, if you know you're going to need that many part numbers (which is what this will be used for) then just allow for it in the number of numeric characters used to uniquely identify each part. I did suggest that but it was considered too costly in that part numbers would be what, another 2 or 3 characters longer - go figure!
Any suggestions on where to start will be greatly appreciated.
Many thanks for all advice so far and that which may be to come,
Paul
-
Nov 4th, 2011, 10:54 AM
#7
Re: Auto incriment alpha-numeric
Try this code, it is use a normal counter until you reach 9999 after that it is convert the counter to base 36 and add a shift value to jump from 9999 to A001.
vb Code:
Public Class Form1 Private mdblCounter As Double = 9990 Private Const mintShiftUp As Integer = (((36 ^ 3) * 10) - 9999) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click mdblCounter += 1 If mdblCounter <= 9999 Then Me.Text = mdblCounter.ToString Else ' mintShiftUp is a constant used to jump from 9999 to A001 Me.Text = ConvertBase10ToAnyBase(mdblCounter + mintShiftUp, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") End If End Sub Private Function ConvertBase10ToAnyBase(ByVal dblBase10 As Double, ByVal strNewBaseDigits As String) As String Dim strRet As String = vbNullString Dim dblTemp As Double Dim intDigit As Integer Dim intEndDigit As Integer Dim intBaseSize As Integer intBaseSize = strNewBaseDigits.Length Do While dblBase10 <> 0 dblTemp = dblBase10 intDigit = 0 Do While dblTemp >= intBaseSize intDigit = intDigit + 1 dblTemp = dblTemp / intBaseSize Loop If intDigit <> intEndDigit - 1 And intEndDigit <> 0 Then strRet = strRet & New String(strNewBaseDigits.Substring(0, 1), intEndDigit - intDigit - 1) End If dblTemp = Int(dblTemp) strRet = strRet + Mid(strNewBaseDigits, dblTemp + 1, 1) dblBase10 = dblBase10 - dblTemp * (intBaseSize ^ intDigit) intEndDigit = intDigit Loop Return strRet & New String(strNewBaseDigits.Substring(0, 1), intDigit) End Function End Class
-
Nov 4th, 2011, 10:59 AM
#8
Re: Auto incriment alpha-numeric
It is not clear what the boundaries are?
0 - 9999, then A001. What happens at A999, B001 and so on to Z999? Then AA01? Is that the correct pattern?
-
Nov 4th, 2011, 11:05 AM
#9
Re: Auto incriment alpha-numeric
 Originally Posted by dbasnett
It is not clear what the boundaries are?
0 - 9999, then A001. What happens at A999, B001 and so on to Z999? Then AA01? Is that the correct pattern?
That sounds about correct based on the other posts I've seen, but now that's just throwing another stone at that counting method. You're throwing out thousands of possibilities. because instead of having 36 possibilities between 0axx and 1axx you'll only have 26 because you're throwing out the 0-9 on the first digit...
/further analysis
-
Nov 4th, 2011, 11:16 AM
#10
Thread Starter
New Member
Re: Auto incriment alpha-numeric
 Originally Posted by dbasnett
It is not clear what the boundaries are?
0 - 9999, then A001. What happens at A999, B001 and so on to Z999? Then AA01? Is that the correct pattern?
Yes this is the desired pattern, correct. Sorry for any ambiguity I may have inferred.
Thanks,
Paul.
-
Nov 4th, 2011, 11:42 AM
#11
Re: Auto incriment alpha-numeric
 Originally Posted by MacDaddy
Yes this is the desired pattern, correct. Sorry for any ambiguity I may have inferred.
Oh ok. I misread. sorry
-
Nov 4th, 2011, 12:00 PM
#12
Re: Auto incriment alpha-numeric
I put together this class:
Code:
Public Class PartNumber
Private intPortion As Integer = 1
Private base26Portion As New Base26
Private maxVal As Integer = 10000
Public Function GetNext() As String
intPortion += 1
If intPortion = maxVal Then
intPortion = 0
If base26Portion.Increment() Then
'This means that the number of characters has to expand.
maxVal \= 10
If maxVal < 10 Then
Windows.Forms.MessageBox.Show("There aren't any part numbers left using this design.", "You're HOSED!!")
Return String.Empty
End If
End If
End If
Return base26Portion.GetString & PadIntPortion()
End Function
Private Function PadIntPortion() As String
If maxVal = 10000 Then
Return intPortion.ToString.PadLeft(4, "0"c)
ElseIf maxVal = 1000 Then
Return intPortion.ToString.PadLeft(3, "0"c)
ElseIf maxVal = 100 Then
Return intPortion.ToString.PadLeft(2, "0"c)
ElseIf maxVal = 10 Then
Return intPortion.ToString
Else
Return String.Empty
End If
End Function
#Region "Private Class"
Private Class Base26
Private cList As New List(Of Byte)
Public Function Increment() As Boolean
Dim carryFlag As Boolean = True
For x = cList.Count - 1 To 0 Step -1
If cList(x) = 90 Then
carryFlag = True
cList(x) = 65
Else
cList(x) += CByte(1)
carryFlag = False
Exit For
End If
Next
'If the carryFlag is still set, then there wasn't a character that could handle the overflow.
'This means a new character is needed.
If carryFlag Then
cList.Insert(0, 65)
End If
Return carryFlag
End Function
Public Function GetString() As String
Dim ret As String = String.Empty
For Each st1 In cList
ret &= Chr(st1)
Next
Return ret
End Function
End Class
#End Region
End Class
I'm currently running a test to see whether or not it works entirely.
It works, except that it ends with ZZZ9, rather than ZZZZ. I doubt that matters.
Last edited by Shaggy Hiker; Nov 4th, 2011 at 12:05 PM.
My usual boring signature: Nothing
 
-
Nov 8th, 2011, 03:40 AM
#13
Thread Starter
New Member
Re: Auto incriment alpha-numeric
OK, first of all a huge apology for the late reply, not intentional I assure you.
Secondly, a huge thanks for helping with the code, Shaggy. You're right, if it only goes to ZZZ9 then I don't really think that's an issue. It all seems to work well to me.
Thank you very much once again, your help is very much appreciated.
Paul
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|