Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. auto generate code number

auto generate code number

Scheduled Pinned Locked Moved C#
databasehelp
22 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Luc Pattyn

    then the only way left is to scan the existing coded names, find the most recent one, and "increment" it. I once did an IncrementString() method, which basically scans a string right-to-left for digits; replaces '0' by '1', or '1' by '2', or...; up to '9' by '0' which sets a carry, meaning you have to repeat the process for the next (R-to-L) digit. This way, there is no need to explicitly extract the number. It does rely on having some other means of finding the most recent coded name though. :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    I only read formatted code with indentation, so please use PRE tags for code snippets.


    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


    F Offline
    F Offline
    FEMDEV
    wrote on last edited by
    #10

    i was trying to do something like this as well.. stuck with how to pass a string as seperator in split function..!! frustrating syntax error

    H OriginalGriffO D 3 Replies Last reply
    0
    • F FEMDEV

      OH.. this will not work if the code is deleted in between then the count and max number at end will be an issue

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #11

      :wtf: I have no idea what you are talking about. :wtf:


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      • F FEMDEV

        i was trying to do something like this as well.. stuck with how to pass a string as seperator in split function..!! frustrating syntax error

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #12

        FEMDEV wrote:

        stuck with how to pass a string as seperator in split function.

        It may not be necessary to do this. According to the MSDN documentation for Int32.TryParse()[^]

        When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

        . I didn't think that it worked like that, however, it says that it does. From the int returned you will know how many characters to use in a String.Replace() call to insert the new value.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        L 1 Reply Last reply
        0
        • H Henry Minute

          FEMDEV wrote:

          stuck with how to pass a string as seperator in split function.

          It may not be necessary to do this. According to the MSDN documentation for Int32.TryParse()[^]

          When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

          . I didn't think that it worked like that, however, it says that it does. From the int returned you will know how many characters to use in a String.Replace() call to insert the new value.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #13

          Sorry Henry, it does not work as the text seems to suggest. The string has to contain a number, nothing but a number, that is. int.Parse("123abc") does throw a FormatException, int.TryParse("123abc", out i) does return false. ditto for "abc123". :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          H 1 Reply Last reply
          0
          • F FEMDEV

            i was trying to do something like this as well.. stuck with how to pass a string as seperator in split function..!! frustrating syntax error

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #14

            Use a regex: "(?[a-zA-Z]+)(?\d*)" splits it into the Name and any number.

            private Regex splitNameAndNumber = new Regex(
            "(?[a-zA-Z]+)(?\\d*)",
            RegexOptions.ExplicitCapture
            | RegexOptions.IgnorePatternWhitespace
            | RegexOptions.Compiled
            );

            I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in. Apathy Error: Don't bother striking any key.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            L 1 Reply Last reply
            0
            • L Luc Pattyn

              Sorry Henry, it does not work as the text seems to suggest. The string has to contain a number, nothing but a number, that is. int.Parse("123abc") does throw a FormatException, int.TryParse("123abc", out i) does return false. ditto for "abc123". :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #15

              I thought that was how it worked. Couldn't test it as the machine I'm using at the moment doesn't have VS on it.

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              L 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Use a regex: "(?[a-zA-Z]+)(?\d*)" splits it into the Name and any number.

                private Regex splitNameAndNumber = new Regex(
                "(?[a-zA-Z]+)(?\\d*)",
                RegexOptions.ExplicitCapture
                | RegexOptions.IgnorePatternWhitespace
                | RegexOptions.Compiled
                );

                I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in. Apathy Error: Don't bother striking any key.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #16

                or use a new separator such as '#', or a fixed number of digits, or just locate the first digit using IndexOfAny(), or... :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                OriginalGriffO 1 Reply Last reply
                0
                • L Luc Pattyn

                  or use a new separator such as '#', or a fixed number of digits, or just locate the first digit using IndexOfAny(), or... :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #17

                  You can tell .NET is a Microsoft product - a dozen different ways to do the same thing (none of them that obvious when you are getting started!) It took me ages to work out how to format a floppy in Win95, then I found about six in as many minutes...

                  I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in. Apathy Error: Don't bother striking any key.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • H Henry Minute

                    I thought that was how it worked. Couldn't test it as the machine I'm using at the moment doesn't have VS on it.

                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #18

                    VS is the first thing I install on any PC I touch! And do you really need VS to test a single method? Notepad.exe + csc.exe are a mighty pair. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    H 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      VS is the first thing I install on any PC I touch! And do you really need VS to test a single method? Notepad.exe + csc.exe are a mighty pair. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                      H Offline
                      H Offline
                      Henry Minute
                      wrote on last edited by
                      #19

                      Shhhh! It's not my PC.

                      Luc Pattyn wrote:

                      Notepad.exe + csc.exe are a mighty pair

                      If both were on this thing (and if I'd thought of it, which is doubtful) I might have. :)

                      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                      L 1 Reply Last reply
                      0
                      • H Henry Minute

                        Shhhh! It's not my PC.

                        Luc Pattyn wrote:

                        Notepad.exe + csc.exe are a mighty pair

                        If both were on this thing (and if I'd thought of it, which is doubtful) I might have. :)

                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #20

                        Notepad should be there, unless it got removed. csc.exe is part of .NET, of which some versions are always present on Vista/Win7 AFAIK. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        I only read formatted code with indentation, so please use PRE tags for code snippets.


                        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                        1 Reply Last reply
                        0
                        • F FEMDEV

                          i tried to put the string of the latest code in an array and parse it for int. put the integers in a integer array. then convert them to form a string of int values and add 1 to this string converted to int. but i couldn't split the initial string!!! what should be the split criteria? string would be always of form fname.lnamexxx where x could be any number 1, 99,999,9999 anything. don't need anyone to write a code just an immediate help, if possible for split criteria of the initial string[fname.lnamexx] .

                          realJSOPR Offline
                          realJSOPR Offline
                          realJSOP
                          wrote on last edited by
                          #21

                          Why don't you just have a identity column in your table. At that point, how many instances of a name (or any column value for that matter) you have is inconsequential...

                          .45 ACP - because shooting twice is just silly
                          -----
                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                          -----
                          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                          1 Reply Last reply
                          0
                          • F FEMDEV

                            i was trying to do something like this as well.. stuck with how to pass a string as seperator in split function..!! frustrating syntax error

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #22

                            Lyuc is right in saying that's pretty much you're last option. But, what he didn't say is that this method will also get slower and slower as you add more and more ID's to scan. The solution works, but it isn't scalable. You may want to take this information back and say that "it can be done, but..."

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                 2006, 2007, 2008
                            But no longer in 2009...

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups