I really need a bit of guidance here - having trouble with functions

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 2593 Views - Last Post: 20 June 2011 - 12:23 PM Rate Topic: -----

#1 mike.crooks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 05:22 PM

Hi Everyone,

I am currently working on a class project using functions specifically in visual basic. I have a function that will not work, and I am stumped. Here is the code I have so far:


Public Class Form1

    Private Sub btnComputeAllCosts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeAllCosts.Click

        Dim squareFootage As Double 'Square footage of plumbing

        Dim bathroomCount As Integer 'Number of bathrooms

        Dim kitchenSize As String 'Size of kitchen

        Dim plumbing As Double

        InputData(squareFootage, bathroomCount, kitchenSize)

    End Sub

    Private Sub btnClearForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearForm.Click

        'Clear all textboxes and radio buttons for next calculation

        txtSquareFootageInput.Text = "0"
        txtBathroomCountInput.Text = "0"

        radioSmall.Checked = False
        radioMedium.Checked = False
        radioBig.Checked = False
        radioHuge.Checked = False

    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click

        'End the program and close the debugger

        End

    End Sub

    Sub InputData(ByVal squareFootage As Double, ByVal bathroomCount As Integer, ByVal kitchenSize As String)

        squareFootage = CDbl(txtSquareFootageInput.Text)
        bathroomCount = CInt(txtBathroomCountInput.Text)

    End Sub

    Function AllPlumbingCosts(ByVal squareFootage As Double, ByVal bathroomCount As Integer, ByVal kitchenSize As String, ByVal plumbing As Double) As Double

        If squareFootage <= 2000 Then

            Return squareFootage * 5.5

        Else

            Return squareFootage * 4.25

        End If

        If radioSmall.Checked = True Then
            kitchenSize = squareFootage + 0.0

            If radioMedium.Checked = True Then
                kitchenSize = squareFootage + 40.0

                If radioBig.Checked = True Then
                    kitchenSize = squareFootage + 90.0

                    If radioHuge.Checked = True Then
                        kitchenSize = squareFootage + 250.0

                    End If
                End If
            End If
        End If

        bathroomCount = 1500 * txtBathroomCountInput.Text

        plumbing = squareFootage + kitchenSize + bathroomCount

    End Function

    Sub ShowCostPerFunction(ByVal plumbing As Double)

        Dim fmtStr As String = "{0, 24}{1,-10:C}"

        lstDisplayAllCalculations.Items.Clear()

        'Display the cost of each function

        lstDisplayAllCalculations.Items.Add("Plumbing Costs:")
        lstDisplayAllCalculations.Items.Add("")
        lstDisplayAllCalculations.Items.Add(String.Format(fmtStr, "Total cost of plumbing ", plumbing))

    End Sub

End Class




Is This A Good Question/Topic? 0
  • +

Replies To: I really need a bit of guidance here - having trouble with functions

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 05:32 PM

Which function?
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 05:33 PM

You didnt explain what error you have but when I look a little at your code, i see you calling InputData(squareFootage, bathroomCount, kitchenSize) like this, passing to it those variables while they are empty, this is not how it should be, you need first to give them value then you can pass them as arguments.
Was This Post Helpful? 1
  • +
  • -

#4 mike.crooks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 05:37 PM

Sorry, the function I am having trouble with is "AllPlumbingCosts"

I am trying to get the total for the variable "plumbing" to display in the listbox below. I need to take squareFootage, bathroomCount and kitchenSize and add them up for the variable "plumbing".

View Postsmohd, on 14 June 2011 - 05:33 PM, said:

You didnt explain what error you have but when I look a little at your code, i see you calling InputData(squareFootage, bathroomCount, kitchenSize) like this, passing to it those variables while they are empty, this is not how it should be, you need first to give them value then you can pass them as arguments.


Would you be able to elaborate on this? Sorry, I have just recently started doing functions in visual basic.
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 05:48 PM

In the first if else statement, there is a return statement which means the function will stop there and other codes will not be executed.

If you want to add the values then dont return any there till to the end of the function.
Was This Post Helpful? 1
  • +
  • -

#6 mike.crooks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 05:52 PM

View Postsmohd, on 14 June 2011 - 05:48 PM, said:

In the first if else statement, there is a return statement which means the function will stop there and other codes will not be executed.

If you want to add the values then dont return any there till to the end of the function.



Hmm, could you highlight this section and elaborate? Do I move the IfElse statement?
Was This Post Helpful? 0
  • +
  • -

#7 mike.crooks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 06:01 PM

I forgot to add that this is a year end project. If I do horribly on this I will fail the class. I'm just not sure about functions.
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 06:13 PM

Ok lets go slowly and brief so that you can understand: look at where your function started(head of)
Function AllPlumbingCosts(ByVal squareFootage As Double, ByVal bathroomCount As Integer, ByVal kitchenSize As String, ByVal plumbing As Double) As Double

        If squareFootage <= 2000 Then

            Return squareFootage * 5.5

        Else

            Return squareFootage * 4.25

        End If

Look at this part, if statement will look at the squareFootage come from the caller subroutine/function, if it is <= 2000, the function will return to caller squareFootage * 5.5(means no more execution in this function will continue). Else, it will return squareFootage * 4.25 to caller(also means no more execution). This means for any squareFootage passed to this function only the first if..else will be executed.

TODO: Either assign the value to a variable and return it at the end of the function(if this is really what you meant) or find another alternative to make sure your following code get execution.
But when looking to what you said you want it to do you are far away from the solution.

Try to elaborate more from what I have told and tell me exactly what you need this function to return
Was This Post Helpful? 2
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 06:27 PM

Going again, I think what you are trying to do at the first if else is to change the value of squareFootage, if so you dont need to return it just assign it new value.
And take a note another thing, in both of your functions,you pass parameters which you then change their value before use(and sometime pass them without value), look which values really you need to pass as parameters, to give guidance you make parameters for only variables which you need to pass their value from the caller function to the called function, if you planned to give variable a value in the called function then no need to pass it as parameter. :)
Was This Post Helpful? 2
  • +
  • -

#10 mike.crooks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 06:27 PM

Here is the question sheet for the function:

Create a BuildingContractor Class with methods as follows:


Function PlumbingCost

Takes three arguments: square footage , bathroom count and kitchen size in that order. All arguments to be passed by value.
Returns the cost to plumb the house as a double financial amount.
Plumbing cost is computed as follows:

Initial cost: $5.50 per square foot for the first 2000 square feet and $4.25 per square foot for square footage over this amount.
Surcharge for kitchen size as follows:
Small no surcharge
Medium Extra $40
Big Extra $90
Huge Extra $250

Bathrooms:
$1500 per bathroom
Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 06:49 PM

Then you are about to get it, but dont return there ,you may say:
squareFootage = squareFootage * 5.5  'and like that

now you have amount for foots. Then you add other amount for other issues.

To get it good, you may have another variable for cost and initialize it to zero and add it for every more cost.

Another thing to care,

Quote

Initial cost: $5.50 per square foot for the first 2000 square feet and $4.25 per square foot for square footage over this amount.

The statement said, for the first 2000, this means if more that 2000 than you have the first 2000 which takes 5.5 and the remaining takes 4.25.
Was This Post Helpful? 2
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 07:52 PM

Making more clear: What you will do is the following scenario, the function will get square footage , bathroom and kitchen as parameters.
- In function, you may have a variable for saving price(not mandatory)
- First check for footage, if <= 2000 add 5.5 to price(which was initialized to zero)
- Else, add 5.5 + 4.24*(footage-2000) to price
- Then check for chicken size and add appropriately
- One before last add price for bathroom if any: 1500* bathroom
- Display the total price and end function
Was This Post Helpful? 1
  • +
  • -

#13 mike.crooks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 08:50 PM

View Postsmohd, on 14 June 2011 - 07:52 PM, said:

Making more clear: What you will do is the following scenario, the function will get square footage , bathroom and kitchen as parameters.
- In function, you may have a variable for saving price(not mandatory)
- First check for footage, if <= 2000 add 5.5 to price(which was initialized to zero)
- Else, add 5.5 + 4.24*(footage-2000) to price
- Then check for chicken size and add appropriately
- One before last add price for bathroom if any: 1500* bathroom
- Display the total price and end function


Ok, after looking over your advice, I modified the code to what is now below:

Public Class Form1

    Private Sub btnComputeAllCosts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeAllCosts.Click

        Dim squareFootage As Double 'Square footage of plumbing

        Dim bathroomCount As Integer 'Number of bathrooms

        Dim kitchenSize As String 'Size of kitchen

        Dim plumbing As Double

        InputData(squareFootage, bathroomCount, kitchenSize)

        squareFootage = CDbl(txtSquareFootageInput.Text)


    End Sub

    Private Sub btnClearForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearForm.Click

        'Clear all textboxes and radio buttons for next calculation

        txtSquareFootageInput.Text = "0"
        txtBathroomCountInput.Text = "0"

        radioSmall.Checked = False
        radioMedium.Checked = False
        radioBig.Checked = False
        radioHuge.Checked = False

    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click

        'End the program and close the debugger

        End

    End Sub

    Sub InputData(ByVal squareFootage As Double, ByVal bathroomCount As Integer, ByVal kitchenSize As String)

        squareFootage = CDbl(txtSquareFootageInput.Text)
        bathroomCount = CInt(txtBathroomCountInput.Text)

    End Sub

    Function AllPlumbingCosts(ByVal squareFootage As Double, ByVal bathroomCount As Integer, ByVal kitchenSize As String, ByVal plumbing As Double) As Double

        If radioSmall.Checked = True Then
            kitchenSize = squareFootage + 0.0

            If radioMedium.Checked = True Then
                kitchenSize = squareFootage + 40.0

                If radioBig.Checked = True Then
                    kitchenSize = squareFootage + 90.0

                    If radioHuge.Checked = True Then
                        kitchenSize = squareFootage + 250.0

                    End If
                End If
            End If
        End If

        If squareFootage <= 2000 Then

            squareFootage = squareFootage * 5.5

        Else

            squareFootage = 5.5 + 4.24 * (squareFootage - 2000)

        End If

        bathroomCount = 1500 * bathroomCount

        plumbing = squareFootage + kitchenSize + bathroomCount

    End Function

    Sub ShowCostPerFunction(ByVal plumbing As Double)

        Dim fmtStr As String = "{0, 24}{1,-10:C}"

        lstDisplayAllCalculations.Items.Clear()

        'Display the cost of each function

        lstDisplayAllCalculations.Items.Add("Plumbing Costs:")
        lstDisplayAllCalculations.Items.Add("")
        lstDisplayAllCalculations.Items.Add(String.Format(fmtStr, "Total cost of plumbing ", plumbing))

    End Sub

End Class



Was This Post Helpful? 0
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 09:13 PM

Ok lets go again: Parameters of function matters when a function is called, so you should really lookk what will pass there as a kitchenSize.But before going there lets look at what you have now:

- First you declare kitchenSize as string! this should be get from radioButtons, so you have that choice but the if statement should change. The process of chaecking which radioButton is checked should not be done here, instead should be done in the caller function(this will give the meaning of passing it). Then here you will only check for the string being passed.
- I will edit the function to be:

Function AllPlumbingCosts(ByVal squareFootage As Double, ByVal bathroomCount As Integer, ByVal kitchenSize As String, ByVal plumbing As Double) As Double
Dim price As Double = 0
        If kitchenSize = "Small" Then
            price += 0.0

         ElseIf kitchenSize = "Medium" Then
                price += 40.0
         ElseIf kitchenSize = "Big" Then
               price += 90.0

         ElseIf kitchenSize = "Huge" Then
                price +=250.0
        End If

        If squareFootage <= 2000 Then

            price += 5.5

        Else

            price += 5.5 + 4.24 * (squareFootage - 2000)

        End If

        price += 1500 * bathroomCount

'now here you just display price
   return price
    End Function


- Now to tell which Radio button is selected as how you did can be done in caller function like:
If radioSmall.Checked = True Then
    kitchenSize = "Small"
' and so on

Then you pass this string to the function when you call it.

Also in that another function, you need to take value from text box in button click event and pass them to the function
Was This Post Helpful? 1
  • +
  • -

#15 mike.crooks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-June 11

Re: I really need a bit of guidance here - having trouble with functions

Posted 14 June 2011 - 09:25 PM

View Postsmohd, on 14 June 2011 - 09:13 PM, said:

Ok lets go again: Parameters of function matters when a function is called, so you should really lookk what will pass there as a kitchenSize.But before going there lets look at what you have now:

- First you declare kitchenSize as string! this should be get from radioButtons, so you have that choice but the if statement should change. The process of chaecking which radioButton is checked should not be done here, instead should be done in the caller function(this will give the meaning of passing it). Then here you will only check for the string being passed.
- I will edit the function to be:

Function AllPlumbingCosts(ByVal squareFootage As Double, ByVal bathroomCount As Integer, ByVal kitchenSize As String, ByVal plumbing As Double) As Double
Dim price As Double = 0
        If kitchenSize = "Small" Then
            price += 0.0

         ElseIf kitchenSize = "Medium" Then
                price += 40.0
         ElseIf kitchenSize = "Big" Then
               price += 90.0

         ElseIf kitchenSize = "Huge" Then
                price +=250.0
        End If

        If squareFootage <= 2000 Then

            price += 5.5

        Else

            price += 5.5 + 4.24 * (squareFootage - 2000)

        End If

        price += 1500 * bathroomCount

'now here you just display price
   return price
    End Function


- Now to tell which Radio button is selected as how you did can be done in caller function like:
If radioSmall.Checked = True Then
    kitchenSize = "Small"
' and so on

Then you pass this string to the function when you call it.

Also in that another function, you need to take value from text box in button click event and pass them to the function


Ok, I am starting to understand this better. In the code:

If radioSmall.Checked = True Then
    kitchenSize = "Small"
' and so on


does it get placed within the function below return price? Or is it to be placed in a different section?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3