4 Replies - 1407 Views - Last Post: 12 November 2013 - 10:49 AM Rate Topic: -----

#1 lennie_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-October 13

Dorm and Meal Plan Calculator

Posted 18 October 2013 - 05:39 PM

It's always something. My assignment is almost done. Everything calculates as it should EXCEPT... the total. It's very frustrating.

I am making a Dorm and Meal Plan Calculator. It has two forms, the main form contains dorm choices, as well as displaying the results. The second form holds meal plan choices and that choice must be used by the main form.
The calculation I need will not work regardless of how I type it. I cannot figure out where it should go. Any suggestions?

This is what I have had (in various different forms), but none of them seem to work.

 decTotal = decDCost + decFCost 
       lblTotal.Text = decTotal.ToString("C")



My code is as follows (minus the final output):
Public Class frmDormMeal

    'variables
    Dim decFCost As Decimal
    Dim decTotal As Decimal
    Dim decDCost As Decimal

    Private Sub lblTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotal.Click

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'Exit
        End
    End Sub
    Public Function CalcTotal(ByVal decTotal) As Decimal

        'calculate
        decTotal = frmMealPlan.FoodCost(decFCost) + decDCost

        Return decTotal
    End Function

    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

        'Dorm price
        If lstDorm.SelectedIndex = -1 Then
            MessageBox.Show("Please select a dormitory!", "Error")
        End If

        If lstDorm.SelectedIndex = 0 Then
            decDCost = 1500
        ElseIf lstDorm.SelectedIndex = 1 Then
            decDCost = 1600
        ElseIf lstDorm.SelectedIndex = 2 Then
            decDCost = 1200
        ElseIf lstDorm.SelectedIndex = 3 Then
            decDCost = 1800
        End If

        'dorm cost display
        lblDCost.Text = decDCost.ToString("C")

    End Sub

    'Opens ConferenceOptions form
    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click

        'Declaring new form
        Dim frmMealPlan As New frmMealPlan()  'declare local variable

        'showing new form
        frmMealPlan.ShowDialog()

    End Sub

    Private Sub lstDorm_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDorm.SelectedIndexChanged

    End Sub
End Class

'------------------------Second Form -------------------------

Public Class frmMealPlan

    Public Sub frmMealPlan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPlan.SelectedIndexChanged

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'cancels form
        Me.Close()

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        'calculates meal plan cost
        'produces meal cost output to main form

        'variable
        Dim decFCost As Decimal

        'meal plan cost dispay
        frmDormMeal.lblFCost.Text = FoodCost(decFCost).ToString("c")

        'close form
        Me.Close()
        frmDormMeal.Show()
    End Sub
    Public Function FoodCost(ByRef decFCost) As Decimal

        'processing
        If lstPlan.SelectedIndex = -1 Then
            MessageBox.Show("Please select a meal plan", "Error")
        End If

        If lstPlan.SelectedIndex = 0 Then
            decFCost = 560
        ElseIf lstPlan.SelectedIndex = 1 Then
            decFCost = 1095
        ElseIf lstPlan.SelectedIndex = 2 Then
            decFCost = 1500
        End If

        Return decFCost
    End Function


End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Dorm and Meal Plan Calculator

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,982
  • Joined: 03-December 12

Re: Dorm and Meal Plan Calculator

Posted 18 October 2013 - 06:16 PM

When you debug your code what values are you getting for:
decTotal?
decDCost?
decFCost?
Was This Post Helpful? 0
  • +
  • -

#3 lennie_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-October 13

Re: Dorm and Meal Plan Calculator

Posted 18 October 2013 - 06:37 PM

View Postastonecipher, on 18 October 2013 - 06:16 PM, said:

When you debug your code what values are you getting for:
decTotal?
decDCost?
decFCost?



For DCost and FCost I am getting the appropriate value for what I enter.

When I had the decTotal cost in the main form, I would get the value for whichever dorm I chose. It would never add the DCost and FCost together. I tried adding the calculation to both functions, I tried it in the calculate button, I even tried to put it on form 2 and call it from there.

I'm very frustrated.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,982
  • Joined: 03-December 12

Re: Dorm and Meal Plan Calculator

Posted 19 October 2013 - 11:32 AM

You have decFCost declared in both forms. So you may be getting the value you expect while looking at one while the the other says at default. For a function call do something like this from your second form (because you have the function in the first forms class statement.

'Use in something like a click event
  form1.DisplayLabel.text =  Form1.CalcTotal(decMeal)



and the code for the function something like:

    Public Function CalcTotal(decFcost As Decimal) As String

        'calculate
        decTotal = decFcost + decDCost

        Return decTotal.ToString("c")
    End Function



But this is a really poor way to do this!
Was This Post Helpful? 1
  • +
  • -

#5 lennie_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-October 13

Re: Dorm and Meal Plan Calculator

Posted 12 November 2013 - 10:49 AM

Thanks for your help. And sorry about the delay. I finished that class and dove right into two more. I got 100% on the assignment, so thanks again for your help. Both on this and the other program you assisted with!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1