5 Replies - 341 Views - Last Post: 04 November 2011 - 10:09 AM Rate Topic: -----

#1 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Function question

Posted 04 November 2011 - 09:43 AM

Am I able to return a value that belongs in more than one result?

I mean, I want to return a value that is based off of 2 calculations and I was wondering if this is correct:

Function CalcParkingFees() As Decimal
        Dim dblDays As Double
        Dim dcmParkingTotal As Decimal
        Dim dblParkingFees As Decimal
        dcmParkingTotal = CDec(txtPark.Text)
        dblDays = CDbl(txtDays.Text)

        dblParkingFees = CDec(dcmParkingTotal * dblDays)
        dblParkingFees = CDec(dblDays * dblPARKING_FEES)

        Return dblParkingFees
    End Function


Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Function question

#2 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Function question

Posted 04 November 2011 - 09:57 AM

So, wait, you're trying to do what? If you're trying to return 2 results (i.e. the results of both calculations) you're going about it the wrong way, as line 9 up there will overwrite the result of line 8. If you're trying to get a function to return 2 results, there's a couple different ways of doing that.

Method 1: Return an array (or some other collection, or a custom object)
Function CalcParkingFees() As Decimal()
    ...
    Dim dblParkingFees(1) As Decimal
    ...

    dblParkingFees(0) = CDec(dcmParkingTotal * dblDays)
    dblParkingFees(1) = CDec(dblDays * dblPARKING_FEES)

    Return dblParkingFees
End Function

Method 2: Pass in a ref parameter to hold the 2nd value:
Function CalcParkingFees(ByRef dblMoreFees As Decimal) As Decimal
    Dim dblDays As Double
    Dim dcmParkingTotal As Decimal
    Dim dblParkingFees As Decimal
    dcmParkingTotal = CDec(txtPark.Text)
    dblDays = CDbl(txtDays.Text)

    dblParkingFees = CDec(dcmParkingTotal * dblDays)
    dblMoreFees = CDec(dblDays * dblPARKING_FEES)

    Return dblParkingFees
End Function

Was This Post Helpful? 1
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Function question

Posted 04 November 2011 - 09:58 AM

I'm not sure what you mean by return a value that is based off of 2 calculations.

In this case the whatever value of the dblParkingFees variable has when you return it is passed by to the caller. You can have multiple return statements statements depending on some condition, but in this general sense you can only return one specific value.

If you want to pass back multiple calculations then you may want to create an object with a couple of properties representing the calculation results, that can then be returned as a single variable.
Was This Post Helpful? 1
  • +
  • -

#4 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Function question

Posted 04 November 2011 - 10:05 AM

Thatnks guys! That really helped.

Am I doing it the correct way if I want to calculate the total based on the amount of days the user enters and based on the amount the user enters in the parking fees textbox?
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Function question

Posted 04 November 2011 - 10:07 AM

Yeah that implementation is fine to use.

You may however want to think about passing in the values of the textbox as a decimal as parameters instead of getting them from inside of the function. It just gives more flexibility that way.
Was This Post Helpful? 1
  • +
  • -

#6 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Function question

Posted 04 November 2011 - 10:09 AM

Will do...thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1