Solved

Do-Loop

Posted on 2007-03-28
10
209 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
  • 5
  • 3
  • 2
10 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 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 142

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
 

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 25 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and 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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now