BC30616 Variable 'intTerm' hides a variable in an enclosing block.

ethan hinze 1 Reputation point
2021-07-03T04:47:12.923+00:00

source:

Private Sub btnCalc_Click( sender As Object, e As EventArgs) Handles btnCalc.Click
    ' Display the monthly mortgage payment.

    Dim intPrincipal As Integer
    Dim dblRate As Double
    Dim dblPay As Double
    Dim intTerm As Integer

    Integer.TryParse(txtPrincipal.Text, intPrincipal)
    Integer.TryParse(lstRates.SelectedItem.ToString, intTerm)
    Double.TryParse(lstRates.SelectedItem.ToString, dblRate)
    dblRate = dblRate / 100
    lblPay.Text = String.Empty
    For intTerm As Integer = 15 To 30 Step 5
        dblPay = Financial.Pmt(dblRate / 12, intTerm * 12, intPrincipal)
        lblPay.Text = lblPay.Text & intTerm.ToString & " years:   " & dblPay.ToString("C2") &
            ControlChars.NewLine
    Next intTerm

End Sub
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,595 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 81,861 Reputation points
    2021-07-03T06:16:30.733+00:00

    You defined intTerm twice :

    Dim intTerm As Integer
    

    and :

    For intTerm As Integer 
    

  2. WayneAKing 4,921 Reputation points
    2021-07-04T05:26:24.627+00:00

    Using the code you originally posted, what happens if you
    simply remove the "As Integer" in the For statement?

    ' Display the monthly mortgage payment.
    Dim intPrincipal As Integer
    Dim dblRate As Double
    Dim dblPay As Double
    Dim intTerm As Integer
    Integer.TryParse(txtPrincipal.Text, intPrincipal)
    Integer.TryParse(lstRates.SelectedItem.ToString, intTerm)
    Double.TryParse(lstRates.SelectedItem.ToString, dblRate)
    dblRate = dblRate / 100
    lblPay.Text = String.Empty
    'For intTerm As Integer = 15 To 30 Step 5
    For intTerm = 15 To 30 Step 5
        dblPay = Financial.Pmt(dblRate / 12, intTerm * 12, intPrincipal)
        lblPay.Text = lblPay.Text & intTerm.ToString & " years:   " & dblPay.ToString("C2") &
         ControlChars.NewLine
    Next intTerm
    
    • Wayne
    0 comments No comments

  3. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-07-05T05:38:46.697+00:00

    Hi @ethan hinze ,

    If I rename one of the variables ...

    You also need to rename the variables in the loop:

            Dim intPrincipal As Integer  
            Dim dblRate As Double  
            Dim dblPay As Double  
            Dim intTerm As Integer  
            Integer.TryParse(txtPrincipal.Text, intPrincipal)  
            Integer.TryParse(lstRates.SelectedItem.ToString, intTerm)  
            Double.TryParse(lstRates.SelectedItem.ToString, dblRate)  
            dblRate = dblRate / 100  
            lblPay.Text = String.Empty  
    
            For intTerm1 As Integer = 15 To 30 Step 5  
                dblPay = Financial.Pmt(dblRate / 12, intTerm1 * 12, intPrincipal)  
                lblPay.Text = lblPay.Text & intTerm1.ToString & " years:   " & dblPay.ToString("C2") &  
                    ControlChars.NewLine  
            Next intTerm1      
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments