• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

vb.net lesson one - how to creat array data structures

My form:  http://www.stevetout.com/current.jpg

My code: http://www.stevetout.com/code.txt

I need some code to accomplish the following tasks.

1.) Allow the user to select which calculation method to use
2.) Load the predefined rates from a file into an array structure
3.) Calculate (in a single event handler) the monthly payment based on the users calculation method choice

Please only list the code samples.  Do not repost all of the code from my app.
0
gmahler5th
Asked:
gmahler5th
  • 2
1 Solution
 
platinumbayCommented:
   'declare your variables here
    Dim PVal, APR, FVal, Payment, TotPmts, dblMonth, dblRateMonth As Double
    Dim Fmt As String
    Dim iRates As New Rates

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        If CT1.Checked = True Then
            If PDR1.Checked = True Then
                APR = iRates.Item(0).Num2
            ElseIf PDR2.Checked = True Then
                APR = iRates.Item(1).Num2
            ElseIf PDR3.Checked = True Then
                APR = iRates.Item(2).Num2
            End If
        ElseIf CT2.Checked = True Then
            APR = txtRate.Text
        End If

        'assign the values from textboxes to the variables
        PVal = txtPrincipal.Text
        'APR = txtRate.Text
        If APR > 1 Then APR = APR / 100 ' Ensure proper form.
        TotPmts = txtYears.Text
        dblMonth = TotPmts * 12
        dblRateMonth = APR / 12


        'calculate the final amount as principal + interest
        Fmt = "###,###,##0.00"   ' Define money format.
        FVal = 0   ' Usually 0 for a loan.

        Payment = PVal / ((1 - (1 + dblRateMonth) ^ -dblMonth) / dblRateMonth)

        'Payment = Pmt(APR / 12, TotPmts, -PVal, FVal) Tried using the builtin payment function.  Couldn't get it to work.

        'put the calculated amount in Payment Amount textbox
        txtAmount.Text = Format(Payment, Fmt)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim oFile As System.IO.File
        Dim oRead As System.IO.StreamReader
        Dim strLine As String
        Dim isEnd As Boolean = False
        Dim strRegExp As String = "PDR(?:\[|\()(?<Seq>\d{1})(?:\]|\))\s\((?<Num1>\d+)(?:,|\.)(?<Num2>[0-9\.]+)\)"

        oRead = oFile.OpenText("C:\rates.txt")
        Do While isEnd = False
            If oRead.Peek = -1 Then
                isEnd = True
                Exit Do
            End If
            strLine = oRead.ReadLine

            Dim iMatch As Match = Regex.Match(strLine, strRegExp)

            'MsgBox(iMatch.Groups("").Value.ToString & ":" & iMatch.Groups(1).Value.ToString & ":" & iMatch.Groups(2).Value.ToString)
            Dim seq As String = iMatch.Groups("Seq").Value.ToString
            Dim num1 As String = iMatch.Groups("Num1").Value.ToString
            Dim num2 As String = iMatch.Groups("Num2").Value.ToString

            Dim iRate As New Rate(seq, num1, num2)
            iRates.add(iRate)
            Select Case seq
                Case "0"
                    Me.PDR1.Text = num1 & " Years " & num2 & "%"
                Case "1"
                    Me.PDR2.Text = num1 & " Years " & num2 & "%"
                Case "2"
                    Me.PDR3.Text = num1 & " Years " & num2 & "%"
            End Select
        Loop
    End Sub

    Public Class Rates
        Inherits CollectionBase

        Public Sub add(ByVal r As Rate)
            MyBase.InnerList.Add(r)
        End Sub

        Public Overloads Function Item(ByVal Index As Integer) As Rate
            ' To retrieve an item from the InnerList, pass the index of that item to the .Item property.
            Return CType(Me.InnerList.Item(Index), Rate)
        End Function

        Public Overloads Sub Remove(ByVal Index As Integer)
            ' This Remove expects an index.
            Dim iRate As Rate
            iRate = CType(Me.InnerList.Item(Index), Rate)
            If Not iRate Is Nothing Then
                Me.InnerList.Remove(iRate)
            End If
        End Sub
    End Class

    Public Class Rate
        Public Seq As Integer
        Public Num1 As Double
        Public Num2 As Double

        Public Sub New(ByVal oseq As Integer, ByVal onum1 As Double, ByVal onum2 As Double)
            Me.Seq = oseq
            Me.Num1 = onum1
            Me.Num2 = onum2
        End Sub
    End Class
0
 
platinumbayCommented:
Found a bug, try this:

'declare your variables here
    Dim iRates As New Rates

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim Fmt As String
        Dim PVal, APR, FVal, Payment, TotPmts, dblMonth, dblRateMonth As Double

        If CT1.Checked = True Then ' Choose own rates
            APR = txtRate.Text
            TotPmts = txtYears.Text
        ElseIf CT2.Checked = True Then ' Predefined
            If PDR1.Checked = True Then
                APR = iRates.Item(0).Num2
                TotPmts = iRates.Item(0).Num1
            ElseIf PDR2.Checked = True Then
                APR = iRates.Item(1).Num2
                TotPmts = iRates.Item(1).Num1
            ElseIf PDR3.Checked = True Then
                APR = iRates.Item(2).Num2
                TotPmts = iRates.Item(2).Num1
            End If
        End If

        'assign the values from textboxes to the variables
        PVal = txtPrincipal.Text
        'APR = txtRate.Text
        If APR > 1 Then APR = APR / 100 ' Ensure proper form.
        dblMonth = TotPmts * 12
        dblRateMonth = APR / 12


        'calculate the final amount as principal + interest
        Fmt = "###,###,##0.00"   ' Define money format.
        FVal = 0   ' Usually 0 for a loan.

        Payment = PVal / ((1 - (1 + dblRateMonth) ^ -dblMonth) / dblRateMonth)

        'Payment = Pmt(APR / 12, TotPmts, -PVal, FVal) Tried using the builtin payment function.  Couldn't get it to work.

        'put the calculated amount in Payment Amount textbox
        txtAmount.Text = Format(Payment, Fmt)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim oFile As System.IO.File
        Dim oRead As System.IO.StreamReader
        Dim strLine As String
        Dim isEnd As Boolean = False
        Dim strRegExp As String = "PDR(?:\[|\()(?<Seq>\d{1})(?:\]|\))\s\((?<Num1>\d+)(?:,|\.)(?<Num2>[0-9\.]+)\)"

        oRead = oFile.OpenText("C:\rates.txt")
        Do While isEnd = False
            If oRead.Peek = -1 Then
                isEnd = True
                Exit Do
            End If
            strLine = oRead.ReadLine

            Dim iMatch As Match = Regex.Match(strLine, strRegExp)

            'MsgBox(iMatch.Groups("").Value.ToString & ":" & iMatch.Groups(1).Value.ToString & ":" & iMatch.Groups(2).Value.ToString)
            Dim seq As String = iMatch.Groups("Seq").Value.ToString
            Dim num1 As String = iMatch.Groups("Num1").Value.ToString
            Dim num2 As String = iMatch.Groups("Num2").Value.ToString

            Dim iRate As New Rate(seq, num1, num2)
            iRates.add(iRate)
            Select Case seq
                Case "0"
                    Me.PDR1.Text = num1 & " Years " & num2 & "%"
                Case "1"
                    Me.PDR2.Text = num1 & " Years " & num2 & "%"
                Case "2"
                    Me.PDR3.Text = num1 & " Years " & num2 & "%"
            End Select
        Loop
    End Sub

    Public Class Rates
        Inherits CollectionBase

        Public Sub add(ByVal r As Rate)
            MyBase.InnerList.Add(r)
        End Sub

        Public Overloads Function Item(ByVal Index As Integer) As Rate
            ' To retrieve an item from the InnerList, pass the index of that item to the .Item property.
            Return CType(Me.InnerList.Item(Index), Rate)
        End Function

        Public Overloads Sub Remove(ByVal Index As Integer)
            ' This Remove expects an index.
            Dim iRate As Rate
            iRate = CType(Me.InnerList.Item(Index), Rate)
            If Not iRate Is Nothing Then
                Me.InnerList.Remove(iRate)
            End If
        End Sub
    End Class

    Public Class Rate
        Public Seq As Integer
        Public Num1 As Double
        Public Num2 As Double

        Public Sub New(ByVal oseq As Integer, ByVal onum1 As Double, ByVal onum2 As Double)
            Me.Seq = oseq
            Me.Num1 = onum1
            Me.Num2 = onum2
        End Sub
    End Class
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now