[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Do-Loop

Posted on 2007-03-28
10
Medium Priority
?
217 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

650 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