Solved

Do-Loop

Posted on 2007-03-28
10
206 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

16 Experts available now in Live!

Get 1:1 Help Now