?
Solved

Do-Loop

Posted on 2007-03-28
10
Medium Priority
?
216 Views
Last Modified: 2013-11-27
How can I change this code - to a (Do-Loop) statement?
Dim rates() As Decimal = {6.5D, 8.3D, 4, 2, 10.5D}
        For Each rate As Decimal In rates
            ratesLabel.Text = ratesLabel.Text _
            & rate.ToString("N1") _
            & ControlChars.NewLine
        Next rate
0
Comment
Question by:dawber39
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 18811145
why do want to change to a Do-Loop ?
0
 

Author Comment

by:dawber39
ID: 18811400
I actually have to write it three different ways for an exercise - and I am stuck on the do loop. I did the for, and the for each, now I am stuck.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18811432
homework... well, that's what I guessed :-)

let me give you a hint:
do loop  is until a condition, yes?
so, assuming you keep the array, the loop has to START at the first item, and loop until the last item in the array.
that should be enough to find the code.
post your findings here, so we can help if there is some issue...



0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:dawber39
ID: 18811730
well - not really homework - but I will except your definition. This is what I have  - and it does not work.

Dim rates() As Decimal = {6.5D, 8.3D, 4, 2, 10.5D}
        Dim counter As Integer
        Dim rate As String
        Do Until counter = rates.Length
            rate = rates(counter)
            ratesLabel.Text = ratesLabel.Text _
            & rate.ToString("N1") _
            & ControlChars.NewLine
        Loop
0
 
LVL 1

Assisted Solution

by:juniorDev
juniorDev earned 100 total points
ID: 18812239
The only thing wrong with the code as you have it is that
you are not incrementing the counter variable!
you need:

counter += 1

within the loop!
Here is an extra tip.  It is good practice to always initialize your variables even though VB automatically initizalizes all variables to "0" for you.

Dim counter as Integer = 0

If you start programming in other languages (such as C++) you'll find that they are not as forgiving as VB.
0
 

Author Comment

by:dawber39
ID: 18813395
It still does not work -  I was doing some much more complicated than this - this little one is racking my brain.
Dim rates() As Decimal = {6.5D, 8.3D, 4, 2, 10.5D}
        Dim counter As Integer
        Dim rate As String
        Do Until counter = rates.Length
            rate = rates(counter)
            ratesLabel.Text = ratesLabel.Text _
            & rate.ToString("N1") _
            & ControlChars.NewLine
            counter = +1
        Loop
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18814338
did you initialize the counter variable correctly?
also, do you increment the counter variable correctly (do a debug and watch the value of counter) when running through the loop
0
 

Author Comment

by:dawber39
ID: 18817329
I didn't increment the counter correctly -:
Dim rates() As Decimal = {6.5D, 8.3D, 4, 2, 10.5D}
Dim counter As Integer
Dim rate As Decimal
Do Until counter = rates.Length
rate = rates(counter)
ratesLabel.Text = ratesLabel.Text _
& rate.ToString("N1") _
& ControlChars.NewLine
counter = counter + 1
Loop
This one works - thanks for the direction
0
 
LVL 1

Expert Comment

by:juniorDev
ID: 18826519
You also had
counter =+1
This is not correct.  It should be
counter +=1 : This is the same thing as counter = counter + 1
This also works for:
counter *= variable
counter \= variable
counter -= variable
0
 

Author Comment

by:dawber39
ID: 18826763
Thank you - I also realized that I could do away with the rate variable and just work with (rates & counter) and insert this:

rates(counter).ToString("N1")

I wasn't aware that - counter +=1 is the same thing as counter = counter + 1 - that is so cool


Thank you for your help
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question