Jump to content

Need some help with collections

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
digink

digink

    Newbie

  • Members
  • Pip
  • 4 posts
Hey guys,

For an assignment I am trying to take code I already have and edit it to use collections instead of module scoped variables. Now I am new to VB, I have a good grasp of the basics but I can not for the life of me figure out how to implement collections and get this to work. I am unsure if I need multiple collections or just 1. Basically this code is calculator which tells you the miles your car has traveled (from the time of purchase) the amount you have paid for fuel, MPG, and other info. I have commented the code but if anyone else needs clarification let me know. Any point in the right direction would be GREATLY appreciated.

Public Class GasD

    '*****************************************************************
    '* Gas-D v3.0                                                    
    '* By digink                                          
    '* Homework 3                                                    
    '* Description: This program is a more complex version of homework
    '* 2. In this homework the user gets to input the same data as done
    '* in homework 2 (price paid, MPG, historical data, etc.) but along
    '* with that the user also gets to input the gas tax data based upon
    '* the type of fuel they used and also the county in which the fuel
    '* was purchased.
    '*****************************************************************

    '* Module level variables which are used in the Record button     
    '* to store data for historical computations.                     
    Dim intAccumMiles As Integer = 0
    Dim intAccumGallon As Single = 0
    Dim intAccumPrice As Single = 0
    Dim intAccumTax As Single = 0


    Private Sub RecordButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnRecord.Click
        '********************************************************
        '* Loads local variables and also initializes the module
        '* level variables for us. This subroutine handles all of
        '* the calculations for fill-up and also for the historical
        '* data that is stored.
        '********************************************************

        '* Local fuel and price variables
        Dim intGallons As Single
        Dim intPricePerGallon As Single
        Dim intFillingCost As Single
        Dim intMPG As Single
        Dim intPircePaidWithTax As Single

        '* Variables for local mileage information
        Dim intMilesSincePreviousFill As Integer
        Dim intStartingMiles As Integer
        Dim intCurrentMiles As Integer

        '* load local variables with info and clear textboxes
        '* Gallons
        intGallons = Convert.ToSingle(txtGallonsUsed.Text)
        lblCurrentGallon.Text = FormatNumber(intGallons, 1)
        txtGallonsUsed.Clear()

        '* Price per Gallon
        intPricePerGallon = Convert.ToSingle(txtPricePerGallon.Text)
        lblShowPrice.Text = FormatNumber(intPricePerGallon, 2)
        txtPricePerGallon.Clear()

        '* Fill-up date
        lblShowDate.Text = dtpDate.Text

        '* Starting Miles
        intStartingMiles = Convert.ToInt32(txtStartMiles.Text)

        '* Current Miles
        intCurrentMiles = Convert.ToInt32(txtCurrentMiles.Text)
        txtCurrentMiles.Clear()

        '* Run calculations and display current values
        '* Price paid this fill-up
        intFillingCost = intGallons * intPricePerGallon
        lblShowPricePaid.Text = FormatNumber(intFillingCost, 2)

        '* Calculate and display MPG since last fill up
        intMilesSincePreviousFill = intCurrentMiles - intStartingMiles - _
                                                      intAccumMiles
        intMPG = intMilesSincePreviousFill / intGallons
        lblMPG.Text = Format(intMPG, "##0.0")

        '* Total taxes paid
        intPircePaidWithTax = lbFuelTax.Text * intGallons
        lbGasWithTax.Text = _
        FormatCurrency(Convert.ToSingle(intPircePaidWithTax))

        '* Calculate and display cumulative values and load
        '* accum variables
        '* Total miles
        intAccumMiles = intCurrentMiles - intStartingMiles
        lblAccumMile.Text = Format(intAccumMiles, "##,##0.")

        '* Total fuel used
        intAccumGallon += intGallons
        lblTotalFuel.Text = Format(intAccumGallon, "#,##0.0")

        '* Total Cost
        intAccumPrice += intFillingCost
        lblTotalGas.Text = FormatCurrency(intAccumPrice)

        '* Long run MPG
        intMPG = intAccumMiles / intAccumGallon
        lblAccumMPG.Text = Format(intMPG, "##0.0")

        '* Long run tax
        intAccumTax += intPircePaidWithTax
        lbTotalTaxAccum.Text = FormatCurrency(intAccumTax)

    End Sub

    Private Sub GetTaxes()
        '***********************************************************************
        '* Sample logic that will calculate the total tax owed per gallon from
        '* the two components of the tax used in homework assignments for
        '* ISM 3253 Section 01, Fall 2008.
        '* Note that this solution is creating a locally scoped variable with
        '* the result. The logic would have to be modified to enable the
        '* result to be available where needed in the program logic.
        '***********************************************************************
        Dim sglTaxRate As Single
        '* Determine fuel-type tax component
        If lbGasType.SelectedIndex = 0 Then '* Gasoline
            sglTaxRate = 0.259
            '* If the fuel is not gasoline it is Diesel
        Else '* Diesel
            sglTaxRate = 0.29
        End If
        '* Add the county component of the tax to the fuel-type component
        Select Case lbCounty.Text
            Case "Orange"
                sglTaxRate += 0.021
            Case "Seminole"
                sglTaxRate += 0.031
            Case "Brevard"
                sglTaxRate += 0.021
            Case "Osceola"
                sglTaxRate += 0.031
            Case "Volusia"
                sglTaxRate += 0.081
            Case "Lake"
                sglTaxRate += 0.031
        End Select

        lbFuelTax.Text = sglTaxRate

    End Sub

    Private Sub GasD_Load(ByVal sender As System.Object, ByVal e As  _
                          System.EventArgs) Handles MyBase.Load
        '*****************************************************************
        '* Sets the Selected index of the ListBox gasType to 0, which is
        '* the first index. This is to make Gasoline the defealt selection
        '* for fuel type when the program is loaded.
        '*****************************************************************

        lbGasType.SelectedIndex = 0

    End Sub

    Private Sub lbCounty_SelectedIndexChanged(ByVal sender As  _
    System.Object, ByVal e As System.EventArgs) Handles _
    lbCounty.SelectedIndexChanged

        'comment later
        Call GetTaxes()

    End Sub

    Private Sub lbGasType_SelectedIndexChanged(ByVal sender As  _
    System.Object, ByVal e As System.EventArgs) Handles _
    lbGasType.SelectedIndexChanged

        'comment later
        Call GetTaxes()

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnExit.Click

        '*****************************************************************
        '* Ends the program when Exit button is pressed or hotkeyed/ESC  
        '*****************************************************************
        Application.Exit()

    End Sub

    Private Sub ValidateInput(ByRef theTextBox As TextBox, _
                             ByVal ValidationString As String)
        '*******************************************************************************
        '* Receives a text box control by reference and a validation string
        '* Loops through each character in the text box .Text property to ensure
        '*  that the character exists in the validation string.
        '* Builds an intermediate string of valid characters found in the text box
        '*  but does not add invalid characters to the intermediate string.
        '* Replaces the text box .Text property with the intermediate string
        '* Also checks to see if the text property is a zero length string after
        '*  validation and replaces the string with a zero if so.
        '* Also strips any leading zeros from the text property if there is more than
        '*  one digit
        '*******************************************************************************
        Dim x As Integer
        Dim strNew As String = ""

        '* Review all characters in the passed text box to ensure that they are 
        '*  numeric(digits)
        '* Loop through all characters and pass numeric digits to a new string
        For x = 0 To Len(theTextBox.Text) - 1
            '* Test for numeric character. True result means character is numeric.
            If InStr(ValidationString, theTextBox.Text.Substring(x, 1)) > 0 Then
                strNew += theTextBox.Text.Substring(x, 1)
            Else                                          '* Character not numeric
                Beep()                                      '* Warn the user
            End If
        Next

        '* Display a zero if no legal quantity has been entered
        If strNew.Length = 0 Then
            strNew = "0"
        End If


        '* Remove leading zero if present.  (Since this code will run on each
        '* keypress there can only be one leading zero at a time.)
        If Len(strNew) > 1 And strNew.Substring(0, 1) = "0" Then
            strNew = strNew.Substring(1)
        End If

        '* Update Qty text box with numeric value
        theTextBox.Text = strNew
    End Sub

    Private Sub txtStartMiles_TextChanged(ByVal sender As  _
    System.Object, ByVal e As System.EventArgs) Handles _
    txtStartMiles.TextChanged

        'comment later
        Call ValidateInput(txtStartMiles, "0123456789")

    End Sub

    Private Sub txtCurrentMiles_TextChanged(ByVal sender As  _
    System.Object, ByVal e As System.EventArgs) Handles _
    txtCurrentMiles.TextChanged

        'comment later
        Call ValidateInput(txtCurrentMiles, "0123456789")

    End Sub

    Private Sub txtGallonsUsed_TextChanged(ByVal sender As  _
    System.Object, ByVal e As System.EventArgs) Handles _
    txtGallonsUsed.TextChanged

        'comment later
        Call ValidateInput(txtGallonsUsed, "0123456789.")

    End Sub

    Private Sub txtPricePerGallon_TextChanged(ByVal sender As  _
    System.Object, ByVal e As System.EventArgs) Handles _
    txtPricePerGallon.TextChanged

        'comment later
        Call ValidateInput(txtPricePerGallon, "0123456789.")

    End Sub

End Class

Edited by Jaan, 19 October 2008 - 09:37 PM.
Please use code tags when you're posting your codes!


#2
digink

digink

    Newbie

  • Members
  • Pip
  • 4 posts
Sorry about not using code tags, I just read the rules was sort of in a rush to post it up here to get any help with it as soon as possible. Apologize for that, will not happen again.

#3
digink

digink

    Newbie

  • Members
  • Pip
  • 4 posts
Say just for the code below: What if I wanted in a label to display the LAST entry to the module level collection and also in another label sum up ALL of the entrys within the collection to get the average of them? Also the variable is a local level variable. I am trying to get this easier to look at for you guys for help.

'load local variables with info and clear textboxes

'gallons

intGallons = Convert.ToSingle(txtGallonsUsed.Text)

With colGallons

  .Add(intGallons)

End With

Edited by digink, 20 October 2008 - 08:00 AM.