Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Const intTERM As Integer = 5
        Dim dblPrincipal As Double
        Dim dblPayment As Double
        Dim blnIsConverted As Boolean

        lblPayments.Text = String.Empty
        blnIsConverted = _
            Double.TryParse(txtPrincipal.Text, dblPrincipal)

        If blnIsConverted Then
              For dblRate As Double = 0.05 To 0.1 Step 0.01
                dblPayment = -Financial.Pmt(dblRate / 12, intTERM * 12, dblPrincipal)
                lblPayments.Text = lblPayments.Text & _
                dblRate.ToString("P0") & "->" _
                & dblPayment.ToString("C2") & _
                ControlChars.NewLine
           Next dblRate

When I put in Do....Loop it says dblRate is not declared
Posted
Updated 6-Apr-14 12:17pm
v2
Comments
[no name] 6-Apr-14 18:19pm    
dblRate is only declared within the scope of the For loop. And there is no Do-While loop anywhere in your code.

That is because dblRate is NOT declared!
VB
Dim dblPrincipal As Double
Dim dblPayment As Double
Dim blnIsConverted As Boolean


Not there.

Declare it and see the difference!

You are declaring it within the if then statement, so it is declared if the conditions are met.
When you put this in a do loop it is not declared on a conditional basis.
Declare it outside the loop and have it available.
 
Share this answer
 
v2
In For loop, it is 3-in-1:
1. declaration and initialization of
dblRate As Double = 0.05

2. condition check
dblRate <= 0.1

3. increase by dblRate by 0.01 for each iteration

In the case of Do While loop, it only checks the condition, i.e. point 2. You have to take care of points 1 and 3 separately in your code. See example:
VB
If blnIsConverted Then
     ' must declare and initilise dblRate
     Dim dblRate As Double = 0.05

     Do While dblRate <= 0.1
        ' other code
        ' increase by the step value of 0.01
        dblRate = dblRate  + 0.01
     Loop

Next dblRate
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900