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. For Every Ten Digits

For Every Ten Digits

Scheduled Pinned Locked Moved C#
helpquestion
12 Posts 7 Posters 2 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.
  • M MasterSharp

    How would I, for every 10 numbers in an int, do an event? What I need help with is the foreach part? I'm confused as of what exactly to put: foreach (myInt in ???) { } Thanks.

    - I love D-flat!

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #2

    Every 10 digits in what int ? You mean as in the int 12345678901 fires one event ? When ? When it's being entered ? If you have a string you can use the length property to check how long it is.

    Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

    M 1 Reply Last reply
    0
    • C Christian Graus

      Every 10 digits in what int ? You mean as in the int 12345678901 fires one event ? When ? When it's being entered ? If you have a string you can use the length property to check how long it is.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      M Offline
      M Offline
      MasterSharp
      wrote on last edited by
      #3

      int But I don't undertand how to calculate this in the language. foreach (myInt in (what goes here?)

      - I love D-flat!

      P 1 Reply Last reply
      0
      • M MasterSharp

        int But I don't undertand how to calculate this in the language. foreach (myInt in (what goes here?)

        - I love D-flat!

        P Offline
        P Offline
        pmarfleet
        wrote on last edited by
        #4

        Convert the number to a string, then loop through the characters in that string.

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        M 1 Reply Last reply
        0
        • P pmarfleet

          Convert the number to a string, then loop through the characters in that string.

          Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

          M Offline
          M Offline
          Matthew Butler 0
          wrote on last edited by
          #5

          int.MaxValue is 2147483647. (Which is 10 digits). Without using a 'bigger' space to store an integer (Int64) this will only 'fire' once if at all. If this is true and you are using an int then if (myInt > 999999999)... will be sufficient. If the integer is in string form then myStr.Length / 10 will give you the number of 'ten digits' there are in the string.

          Matthew Butler

          1 Reply Last reply
          0
          • M MasterSharp

            How would I, for every 10 numbers in an int, do an event? What I need help with is the foreach part? I'm confused as of what exactly to put: foreach (myInt in ???) { } Thanks.

            - I love D-flat!

            G Offline
            G Offline
            Gareth H
            wrote on last edited by
            #6

            MasterSharp, I think you've misunderstood the foreach slightly. Foreach (int currentInt in myInt) currentInt is the current int the foreach is at in the iteration. myInt is the collection you want to iterate through. So, if you wanted to fire an event every 10 digits, you could do: int myInt = 100; int tenDigit = 0; foreach (int currentInt in myInt) { if (tenDigit == 10) { tenDigit = 0; If (Event != null) Event(this, new EventArgs()); } tenDigit++; } Hope this helps. Regards, Gareth.

            M M J 3 Replies Last reply
            0
            • G Gareth H

              MasterSharp, I think you've misunderstood the foreach slightly. Foreach (int currentInt in myInt) currentInt is the current int the foreach is at in the iteration. myInt is the collection you want to iterate through. So, if you wanted to fire an event every 10 digits, you could do: int myInt = 100; int tenDigit = 0; foreach (int currentInt in myInt) { if (tenDigit == 10) { tenDigit = 0; If (Event != null) Event(this, new EventArgs()); } tenDigit++; } Hope this helps. Regards, Gareth.

              M Offline
              M Offline
              MasterSharp
              wrote on last edited by
              #7

              Yes, thank you all.

              - I love D-flat!

              1 Reply Last reply
              0
              • G Gareth H

                MasterSharp, I think you've misunderstood the foreach slightly. Foreach (int currentInt in myInt) currentInt is the current int the foreach is at in the iteration. myInt is the collection you want to iterate through. So, if you wanted to fire an event every 10 digits, you could do: int myInt = 100; int tenDigit = 0; foreach (int currentInt in myInt) { if (tenDigit == 10) { tenDigit = 0; If (Event != null) Event(this, new EventArgs()); } tenDigit++; } Hope this helps. Regards, Gareth.

                M Offline
                M Offline
                Malcolm Smart
                wrote on last edited by
                #8

                gareth111 wrote:

                int myInt = 100; int tenDigit = 0; foreach (int currentInt in myInt)

                myInt is an integer. Foreach requires a collection, or some object that has an enumerator defined.

                Small angry dogs

                My 1000th post!!

                G 1 Reply Last reply
                0
                • M Malcolm Smart

                  gareth111 wrote:

                  int myInt = 100; int tenDigit = 0; foreach (int currentInt in myInt)

                  myInt is an integer. Foreach requires a collection, or some object that has an enumerator defined.

                  Small angry dogs

                  My 1000th post!!

                  G Offline
                  G Offline
                  Gareth H
                  wrote on last edited by
                  #9

                  Malcolm Smart, Ye. My mistake, was thinking of just a normal for(); Regards, Gareth.

                  1 Reply Last reply
                  0
                  • G Gareth H

                    MasterSharp, I think you've misunderstood the foreach slightly. Foreach (int currentInt in myInt) currentInt is the current int the foreach is at in the iteration. myInt is the collection you want to iterate through. So, if you wanted to fire an event every 10 digits, you could do: int myInt = 100; int tenDigit = 0; foreach (int currentInt in myInt) { if (tenDigit == 10) { tenDigit = 0; If (Event != null) Event(this, new EventArgs()); } tenDigit++; } Hope this helps. Regards, Gareth.

                    J Offline
                    J Offline
                    Jordanwb
                    wrote on last edited by
                    #10

                    In regards to Gareth's post, you can't use foreach on non-array objects.

                    G 1 Reply Last reply
                    0
                    • J Jordanwb

                      In regards to Gareth's post, you can't use foreach on non-array objects.

                      G Offline
                      G Offline
                      Gareth H
                      wrote on last edited by
                      #11

                      Jordanwb, See above. Regards, Gareth.

                      J 1 Reply Last reply
                      0
                      • G Gareth H

                        Jordanwb, See above. Regards, Gareth.

                        J Offline
                        J Offline
                        Jordanwb
                        wrote on last edited by
                        #12

                        Sorry My bad. Jordanwb

                        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