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. Visual Basic
  4. Basic looping problem

Basic looping problem

Scheduled Pinned Locked Moved Visual Basic
helpquestion
13 Posts 6 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.
  • K Offline
    K Offline
    kanchoette
    wrote on last edited by
    #1

    I need to populate a combobox with a list of the past 20 years. So I get 2009 2008 2007 ... I have For x As Integer = Date.Now.Year - 20 To Date.Now.Year Step -1 form.YearFromComboBox.Items.Add(x.ToString()) Next but the loop never runs in code. What am I doing wrong please (apart from being in charge of a computer :-D )?

    H M N G 4 Replies Last reply
    0
    • K kanchoette

      I need to populate a combobox with a list of the past 20 years. So I get 2009 2008 2007 ... I have For x As Integer = Date.Now.Year - 20 To Date.Now.Year Step -1 form.YearFromComboBox.Items.Add(x.ToString()) Next but the loop never runs in code. What am I doing wrong please (apart from being in charge of a computer :-D )?

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

      The first problem with the code, as posted is that your list will be reversed. i.e. it will give 1989 1990 1991 ........

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

      K 1 Reply Last reply
      0
      • H Henry Minute

        The first problem with the code, as posted is that your list will be reversed. i.e. it will give 1989 1990 1991 ........

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

        K Offline
        K Offline
        kanchoette
        wrote on last edited by
        #3

        Of course. I've been staring at this darn thing for far too long today. :-O Thanks.

        H 1 Reply Last reply
        0
        • K kanchoette

          Of course. I've been staring at this darn thing for far too long today. :-O Thanks.

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

          kanchoette wrote:

          I've been staring at this darn thing for far too long today

          I know that feeling.

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

          1 Reply Last reply
          0
          • K kanchoette

            I need to populate a combobox with a list of the past 20 years. So I get 2009 2008 2007 ... I have For x As Integer = Date.Now.Year - 20 To Date.Now.Year Step -1 form.YearFromComboBox.Items.Add(x.ToString()) Next but the loop never runs in code. What am I doing wrong please (apart from being in charge of a computer :-D )?

            M Offline
            M Offline
            Mr Oizo
            wrote on last edited by
            #5

            Dim X as integer for x = Cint(date.Now.year) to (Cint(Date.Now.Year)-20) step - 1 YearFromCoomboBox.items.add(x) next

            G 1 Reply Last reply
            0
            • M Mr Oizo

              Dim X as integer for x = Cint(date.Now.year) to (Cint(Date.Now.Year)-20) step - 1 YearFromCoomboBox.items.add(x) next

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Curious why you are using CInt for this? The Year property already is an Integer. Or you should be consistent and convert every value:

              Dim x As Integer
              For x = CInt(Cdate(CDate(Date).Now).Year) to (CInt(CDate(CDate(Date).Now).Year) - CInt(20)) step CInt(-CInt(1))
              YearFromCoomboBox.items.add(CStr(CInt(x)))
              Next

              ;)

              Despite everything, the person most likely to be fooling you next is yourself.

              1 Reply Last reply
              0
              • K kanchoette

                I need to populate a combobox with a list of the past 20 years. So I get 2009 2008 2007 ... I have For x As Integer = Date.Now.Year - 20 To Date.Now.Year Step -1 form.YearFromComboBox.Items.Add(x.ToString()) Next but the loop never runs in code. What am I doing wrong please (apart from being in charge of a computer :-D )?

                N Offline
                N Offline
                nlarson11
                wrote on last edited by
                #7

                For i As Integer = 0 To 20 MsgBox(Now.AddYears(i * -1).ToString("yyyy")) Next

                'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

                1 Reply Last reply
                0
                • K kanchoette

                  I need to populate a combobox with a list of the past 20 years. So I get 2009 2008 2007 ... I have For x As Integer = Date.Now.Year - 20 To Date.Now.Year Step -1 form.YearFromComboBox.Items.Add(x.ToString()) Next but the loop never runs in code. What am I doing wrong please (apart from being in charge of a computer :-D )?

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Don't call the Now method repeatedly, that is bad practice. In this specific case it's quite unlikely, but the reason is that the value returned by Now changes, so you can get different values from one call to the next. Get the value into a variable, and use the variable repeatedly. When you loop from a higher value to a lower, you have to put the higher value first in the For statement:

                  Dim year As Integer = DateTime.Now.Year
                  For x As Integer = year To year - 20 Step -1
                  form.YearFromComboBox.Items.Add(x.ToString())
                  Next

                  Despite everything, the person most likely to be fooling you next is yourself.

                  S 1 Reply Last reply
                  0
                  • G Guffa

                    Don't call the Now method repeatedly, that is bad practice. In this specific case it's quite unlikely, but the reason is that the value returned by Now changes, so you can get different values from one call to the next. Get the value into a variable, and use the variable repeatedly. When you loop from a higher value to a lower, you have to put the higher value first in the For statement:

                    Dim year As Integer = DateTime.Now.Year
                    For x As Integer = year To year - 20 Step -1
                    form.YearFromComboBox.Items.Add(x.ToString())
                    Next

                    Despite everything, the person most likely to be fooling you next is yourself.

                    S Offline
                    S Offline
                    Samir Ibrahim 0
                    wrote on last edited by
                    #9

                    Hi Gufa See the code below

                    For I = Now.Year To Now.Year - 20 Step -1
                    Me.ComboBox1.Items.Add(CStr(I))
                    Next

                    I am not posting to show you my answer, but to ask you some question I am still learning VB.Net so I am searching for answers #1 You mention above in previous reply that using Now recursively is not best practice, Can i know why. #2 Is using .net convert function such as x.ToString is better than CStr(x) ? and why. TIA

                    Samir R. Ibrahim

                    G 1 Reply Last reply
                    0
                    • S Samir Ibrahim 0

                      Hi Gufa See the code below

                      For I = Now.Year To Now.Year - 20 Step -1
                      Me.ComboBox1.Items.Add(CStr(I))
                      Next

                      I am not posting to show you my answer, but to ask you some question I am still learning VB.Net so I am searching for answers #1 You mention above in previous reply that using Now recursively is not best practice, Can i know why. #2 Is using .net convert function such as x.ToString is better than CStr(x) ? and why. TIA

                      Samir R. Ibrahim

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #10

                      Thecaptin wrote:

                      #1 You mention above in previous reply that using Now recursively is not best practice, Can i know why.

                      The value returned from Now changes, so it could for example be 2008-12-31 23:59:59 when you call it the first time and 2009-01-01 00:00:00 the next. In your case there is a very low risk for this to happen, and it would only result in the wrong number of items in the list, but there are situations where this is much more likely to happen, and where it will have a much more serious result. That is why it's a bad practice to call the Now method repeatedly.

                      Thecaptin wrote:

                      #2 Is using .net convert function such as x.ToString is better than CStr(x) ? and why.

                      The CStr function only does some type checking and then it calls the ToString method, so the difference it quite small. Some will say that you should use the functions in VB, some will say that you should use the function in the framework. I lean towards the later. VB is now an object oriented language, so the Integer type has a method for converting it to a string. You don't need another function that in the end just calls the method in the Integer class anyway.

                      Despite everything, the person most likely to be fooling you next is yourself.

                      S 1 Reply Last reply
                      0
                      • G Guffa

                        Thecaptin wrote:

                        #1 You mention above in previous reply that using Now recursively is not best practice, Can i know why.

                        The value returned from Now changes, so it could for example be 2008-12-31 23:59:59 when you call it the first time and 2009-01-01 00:00:00 the next. In your case there is a very low risk for this to happen, and it would only result in the wrong number of items in the list, but there are situations where this is much more likely to happen, and where it will have a much more serious result. That is why it's a bad practice to call the Now method repeatedly.

                        Thecaptin wrote:

                        #2 Is using .net convert function such as x.ToString is better than CStr(x) ? and why.

                        The CStr function only does some type checking and then it calls the ToString method, so the difference it quite small. Some will say that you should use the functions in VB, some will say that you should use the function in the framework. I lean towards the later. VB is now an object oriented language, so the Integer type has a method for converting it to a string. You don't need another function that in the end just calls the method in the Integer class anyway.

                        Despite everything, the person most likely to be fooling you next is yourself.

                        S Offline
                        S Offline
                        Samir Ibrahim 0
                        wrote on last edited by
                        #11

                        Hi, Sorry for getting late to answer, I was in vacation.

                        Guffa wrote:

                        The value returned from Now changes, so it could for example be 2008-12-31 23:59:59 when you call it the first time and 2009-01-01 00:00:00 the next.

                        That is a clever remark from your side, but my answer is "No Comment" because there is to many variables required which is necessary to use the new or old "Now" Value.

                        Guffa wrote:

                        You don't need another function that in the end just calls the method in the Integer class anyway.

                        I had make some tests. -As you said above, thet CStr is a wrapping function to .net ToString function. and from my test result, that is not true. -I had made a 2 loop

                        Dim TimeStart, TimeElapse
                        TimeStart = Now.TimeOfDay
                        For I = 1 to 10000000
                        strTemp = CStr(I)
                        Next I
                        TimeElapse = Now.TimeOfDay - TimeStart
                        MessageBox.Show(TimeElapse)

                        TimeStart = Now.TimeOfDay
                        For I = 1 to 10000000
                        strTemp = I.ToString
                        Next I
                        TimeElapse = Now.TimeOfDay - TimeStart
                        MessageBox.Show(TimeElapse)

                        - I compare the execution time of both codes - The result was they are in the same speed until counter "I" is over 1000000 when I is over, CStr is 17 to 30% faster than .ToString Can you confirm? if that so, then CStr is a VB Function and not in .net framework? TIA

                        Samir R. Ibrahim

                        G 1 Reply Last reply
                        0
                        • S Samir Ibrahim 0

                          Hi, Sorry for getting late to answer, I was in vacation.

                          Guffa wrote:

                          The value returned from Now changes, so it could for example be 2008-12-31 23:59:59 when you call it the first time and 2009-01-01 00:00:00 the next.

                          That is a clever remark from your side, but my answer is "No Comment" because there is to many variables required which is necessary to use the new or old "Now" Value.

                          Guffa wrote:

                          You don't need another function that in the end just calls the method in the Integer class anyway.

                          I had make some tests. -As you said above, thet CStr is a wrapping function to .net ToString function. and from my test result, that is not true. -I had made a 2 loop

                          Dim TimeStart, TimeElapse
                          TimeStart = Now.TimeOfDay
                          For I = 1 to 10000000
                          strTemp = CStr(I)
                          Next I
                          TimeElapse = Now.TimeOfDay - TimeStart
                          MessageBox.Show(TimeElapse)

                          TimeStart = Now.TimeOfDay
                          For I = 1 to 10000000
                          strTemp = I.ToString
                          Next I
                          TimeElapse = Now.TimeOfDay - TimeStart
                          MessageBox.Show(TimeElapse)

                          - I compare the execution time of both codes - The result was they are in the same speed until counter "I" is over 1000000 when I is over, CStr is 17 to 30% faster than .ToString Can you confirm? if that so, then CStr is a VB Function and not in .net framework? TIA

                          Samir R. Ibrahim

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #12

                          Samir Ibrahim wrote:

                          That is a clever remark from your side

                          In what way do you think that it's clever?

                          Samir Ibrahim wrote:

                          but my answer is "No Comment"

                          As in "I'm in to it up to my ears, and in that situation it's best keep your mouth shut"? ;)

                          Samir Ibrahim wrote:

                          because there is to many variables required which is necessary to use the new or old "Now" Value.

                          That doesn't make sense... What variables are you talking about?

                          Samir Ibrahim wrote:

                          - I compare the execution time of both codes - The result was they are in the same speed until counter "I" is over 1000000 when I is over, CStr is 17 to 30% faster than .ToString Can you confirm?

                          Of course not. With as few iterations as you used, you are way below the resolution of the system clock. you should use something that gives a better resolution:

                          Dim w1 As New Stopwatch, w2 As New Stopwatch
                          Dim strTemp As String

                          w1.Start()
                          For i = 1 To 100000000
                          strTemp = CStr(i)
                          Next i
                          w1.Stop()

                          w2.Start()
                          For i = 1 To 100000000
                          strTemp = i.ToString
                          Next i
                          w2.Stop()

                          TextBox1.Text = String.Format("CStr: {0} ms., ToString: {1} ms.", w1.ElapsedMilliseconds, w2.ElapsedMilliseconds)

                          Result of my test run: CStr: 12487 ms., ToString: 11406 ms. According to your test, CStr should be 17-30% faster, but instead ToString is about 9% faster. This is consistent with the code that is generated. If you take a look at the code in .Net Reflector, you see that this statement: strTemp = CStr(i) actually compiles into this code: strTemp = Conversions.ToString(i) If you look at the Conversions.ToString(Int32) method, it looks like this:

                          Public Shared Function ToString(ByVal Value As Integer) As String
                          Return Value.ToString(Nothing, Nothing)
                          End Function

                          If that's not a plain wrapper for the Int32.ToString method, I don't know what is...

                          Despite everything, the person most likely to be fooling you next is yourself.

                          S 1 Reply Last reply
                          0
                          • G Guffa

                            Samir Ibrahim wrote:

                            That is a clever remark from your side

                            In what way do you think that it's clever?

                            Samir Ibrahim wrote:

                            but my answer is "No Comment"

                            As in "I'm in to it up to my ears, and in that situation it's best keep your mouth shut"? ;)

                            Samir Ibrahim wrote:

                            because there is to many variables required which is necessary to use the new or old "Now" Value.

                            That doesn't make sense... What variables are you talking about?

                            Samir Ibrahim wrote:

                            - I compare the execution time of both codes - The result was they are in the same speed until counter "I" is over 1000000 when I is over, CStr is 17 to 30% faster than .ToString Can you confirm?

                            Of course not. With as few iterations as you used, you are way below the resolution of the system clock. you should use something that gives a better resolution:

                            Dim w1 As New Stopwatch, w2 As New Stopwatch
                            Dim strTemp As String

                            w1.Start()
                            For i = 1 To 100000000
                            strTemp = CStr(i)
                            Next i
                            w1.Stop()

                            w2.Start()
                            For i = 1 To 100000000
                            strTemp = i.ToString
                            Next i
                            w2.Stop()

                            TextBox1.Text = String.Format("CStr: {0} ms., ToString: {1} ms.", w1.ElapsedMilliseconds, w2.ElapsedMilliseconds)

                            Result of my test run: CStr: 12487 ms., ToString: 11406 ms. According to your test, CStr should be 17-30% faster, but instead ToString is about 9% faster. This is consistent with the code that is generated. If you take a look at the code in .Net Reflector, you see that this statement: strTemp = CStr(i) actually compiles into this code: strTemp = Conversions.ToString(i) If you look at the Conversions.ToString(Int32) method, it looks like this:

                            Public Shared Function ToString(ByVal Value As Integer) As String
                            Return Value.ToString(Nothing, Nothing)
                            End Function

                            If that's not a plain wrapper for the Int32.ToString method, I don't know what is...

                            Despite everything, the person most likely to be fooling you next is yourself.

                            S Offline
                            S Offline
                            Samir Ibrahim 0
                            wrote on last edited by
                            #13

                            Guffa wrote:

                            In what way do you think that it's clever?

                            You take care of the possibility that the date could change during this time of period "31/12/2008 11:59:00" when using Now() in a function.

                            Guffa wrote:

                            hat doesn't make sense... What variables are you talking about?

                            I mean Variation and demands to this application if it need to use the new Now() Value or still using the old Now() value. In some situation you need the New Now() value to be presented in the function you are creating and in sometime that is not important.

                            Guffa wrote:

                            Dim w1 As New Stopwatch

                            Thank you for this new Info

                            Guffa wrote:

                            CStr: 12487 ms., ToString: 11406 ms.

                            With your code, I had the same result but, with different numbers ;P

                            Guffa wrote:

                            If you take a look at the code in .Net Reflector,

                            I heard about .Net Reflector, never use it or know what is this used for. Do you notice that all my question ends with ? because I am not sure. I am still learning vb.net 2 Month old ,so don't be hard on me :) Thank you very much for your time and enplaning. very appreciated.

                            Samir R. Ibrahim

                            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