Results 1 to 13 of 13

Thread: [RESOLVED] Auto incriment alpha-numeric

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Location
    South Wales
    Posts
    14

    Resolved [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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Auto incriment alpha-numeric

    I agree with Shaggy that this would be much easier as a radix 36 number.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Auto incriment alpha-numeric

    Quote Originally Posted by Shaggy Hiker View Post
    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.

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Location
    South Wales
    Posts
    14

    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

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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:
    1. Public Class Form1
    2.  
    3.     Private mdblCounter As Double = 9990
    4.     Private Const mintShiftUp As Integer = (((36 ^ 3) * 10) - 9999)
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.  
    8.         mdblCounter += 1
    9.  
    10.         If mdblCounter <= 9999 Then
    11.             Me.Text = mdblCounter.ToString
    12.         Else
    13.             ' mintShiftUp is a constant used to jump from 9999 to A001
    14.             Me.Text = ConvertBase10ToAnyBase(mdblCounter + mintShiftUp, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    15.         End If
    16.  
    17.     End Sub
    18.  
    19.     Private Function ConvertBase10ToAnyBase(ByVal dblBase10 As Double, ByVal strNewBaseDigits As String) As String
    20.  
    21.         Dim strRet As String = vbNullString
    22.         Dim dblTemp As Double
    23.         Dim intDigit As Integer
    24.         Dim intEndDigit As Integer
    25.         Dim intBaseSize As Integer
    26.  
    27.         intBaseSize = strNewBaseDigits.Length
    28.  
    29.         Do While dblBase10 <> 0
    30.             dblTemp = dblBase10
    31.             intDigit = 0
    32.             Do While dblTemp >= intBaseSize
    33.                 intDigit = intDigit + 1
    34.                 dblTemp = dblTemp / intBaseSize
    35.             Loop
    36.             If intDigit <> intEndDigit - 1 And intEndDigit <> 0 Then
    37.                 strRet = strRet & New String(strNewBaseDigits.Substring(0, 1), intEndDigit - intDigit - 1)
    38.             End If
    39.  
    40.             dblTemp = Int(dblTemp)
    41.             strRet = strRet + Mid(strNewBaseDigits, dblTemp + 1, 1)
    42.             dblBase10 = dblBase10 - dblTemp * (intBaseSize ^ intDigit)
    43.             intEndDigit = intDigit
    44.         Loop
    45.  
    46.         Return strRet & New String(strNewBaseDigits.Substring(0, 1), intDigit)
    47.  
    48.     End Function
    49.  
    50. End Class



  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Auto incriment alpha-numeric

    Quote Originally Posted by dbasnett View Post
    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

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Location
    South Wales
    Posts
    14

    Re: Auto incriment alpha-numeric

    Quote Originally Posted by dbasnett View Post
    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.

  11. #11
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Auto incriment alpha-numeric

    Quote Originally Posted by MacDaddy View Post
    Yes this is the desired pattern, correct. Sorry for any ambiguity I may have inferred.
    Oh ok. I misread. sorry

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  13. #13

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Location
    South Wales
    Posts
    14

    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
  •  



Click Here to Expand Forum to Full Width