Results 1 to 9 of 9

Thread: Using Returned value of a function

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    55

    Using Returned value of a function

    Hey everyone,
    I have many functions that perform mathematical calculations.
    Here is one for example:
    Code:
    Public Function internaldiamofcasingpipecalculation(ByVal Externaldiameterofcarrierpipe As Double, ByVal insulationthickness As Double) As Double
    
            Dim InternalDiameterofCasingPipe = Externaldiameterofcarrierpipe + 2 * (insulationthickness / 1000)
    
            Return InternalDiameterofCasingPipe
        End Function
    Now in the next function I need to use the returned value of the previous function " InternalDiameterofCasingPipe":
    Code:
    Public Function externaldiamofcasingpipe(ByVal internaldiameterofcasingpipe As Double, ByVal jacketthickness As Double) As Double
    
            Dim ExternalDiameterofCasingPipe = internaldiameterofcasingpipe + 2 * (jacketthickness / 1000)
    
            Return ExternalDiameterofCasingPipe
        End Function
    So the problem is while I am debugging I put a break point and found that the first function taht calulates the InternalDiameterofCasingPipe works fine. But when I check the value of InternalDiameterofCasingPipe in the second function its value is 0... Why is this? Is there a specific way to use the values returned by a function?

    Here is the function that calls the calculations in order:
    Code:
    Public Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
    
            ' 1. Check if Metric or Imperial Units are selected.
            ' 2. Convert Entered Variables to opposite unit.
            ' 3. Determine what type of pipe is selected
            ' 4. Determine what type of casing is selected.
            ' 5. Perform Required Calculations & display them
    
            If optMetric.Checked Then
    
    
                Call ConvertVariableToImperial(SoilTemp, SupplyTemp, CarrierPipeWallThickness, InsulationThickness, JacketThickness, Externaldiameterofcarrierpipe, LayingDepthtoTopofPipe, TotalLengthofPipeRun,
                                               FlowRate, Temperaturetoreach)
    
                Call choosepipecomboboxcoding(cboChoosePipe,
                                                       HDPE,
                                                       PVC,
                                                       ABS,
                                                       FRP,
                                                       Copper,
                                                       Steel,
                                                       Ductileiron,
                                                       ImperialHDPE,
                                                       ImperialPVC,
                                                       ImperialABS,
                                                       ImperialFRP,
                                                       ImperialCopper,
                                                       ImperialSteel,
                                                       ImperialDuctileIron)
                choosecasingcomboboxcoding(cboChooseCasingPipe, HDPE, GalvanizedSteel, Aluminum, PVC, Steel, ImperialHDPE, ImperialGalvanizedSteel, ImperialAluminum, ImperialPVC, ImperialSteel)
    
    
                InternalDiamCarrierPipeCalculation(Externaldiameterofcarrierpipe, CarrierPipeWallThickness)
    
                internaldiamofcasingpipecalculation(Externaldiameterofcarrierpipe, InsulationThickness)
    
                externaldiamofcasingpipe(InternalDiameterofCasingPipe, JacketThickness)
    
                layingdepthtocenterlineofpipecal(LayingDepthtoTopofPipe, ExternalDiameterofCasingPipe)
    
                crosssectionalareaofpipecal(Externaldiameterofcarrierpipe, CarrierPipeWallThickness)
    
                thermalresistanceofinsulationcal(ThermalConductivityofInsulation, InternalDiameterofCasingPipe, Externaldiameterofcarrierpipe)
    
                thermalresistanceofthecarrierpipecal(ThermalConductivityofCarrierPipe, Externaldiameterofcarrierpipe, InternalDiameterofCarrierPipe)
    
                thermalresistanceofthejacketpipecal(ThermalConductivityofCasingPipe, ExternalDiameterofCasingPipe, InternalDiameterofCasingPipe)
    
                thermalresistanceofsoilcal(ThermalConductivityofSoil, LayingDepthToCenterlineOfPipe, ExternalDiameterofCasingPipe)
    
                timetoreachtempcal(Temperaturetoreach, SoilTemp, SupplyTemp, InternalDiameterofCarrierPipe, ThermalResistanceofTheInsulation, ThermalResistanceOfTheCarrierPipe,
                                   ThermalResistanceofTheJacketPipe, ThermalResistanceOfTheSoil)
    
                totalheatgainfromsoilonpiperuncal(TotalLengthofPipeRun, HeatGain)
    
                increaseinsupplytempatoutletcal(TotalHeatGainFromSoilonPipeRun, FlowRateImperial)
    
    
    
    
                Call HeatTransmissionCoefficientforAsinglePipeCalculation(ThermalResistanceofTheInsulation, ThermalResistanceOfTheCarrierPipe, ThermalResistanceofTheJacketPipe,
                                                                          ThermalResistanceOfTheSoil)
    
                Call HeatGainCalculation(HeatTransmissionCoefficientforASinglePipe,
                                         SoilTemp,
                                         SupplyTemp)
    
    
                Call VelocityCalculation(FlowRate, Externaldiameterofcarrierpipe, CarrierPipeWallThickness)
    
    
            ElseIf optImperial.Checked Then
    
                ImperialSoilTemp = SoilTemp
                ImperialSupplyTemp = SupplyTemp
                ImperialCarrierPipeWallThickness = CarrierPipeWallThickness
                ImperialInsulationThickness = InsulationThickness
                ImperialJacketThickness = JacketThickness
                ImperialExternaldiameterofcarrierpipe = Externaldiameterofcarrierpipe
                ImperialLayingDepthtoTopofPipe = LayingDepthtoTopofPipe
                ImperialTotalLengthofPipeRun = TotalLengthofPipeRun
                ImperialTemperaturetoreach = Temperaturetoreach
                FlowRateImperial = FlowRate
    
                Call ConvertVariablesToMetric(ImperialSoilTemp, ImperialSupplyTemp, ImperialCarrierPipeWallThickness, ImperialInsulationThickness, ImperialJacketThickness, ImperialExternaldiameterofcarrierpipe,
                                              ImperialLayingDepthtoTopofPipe, ImperialTotalLengthofPipeRun, ImperialTemperaturetoreach)
    
                Call choosepipecomboboxcoding(cboChoosePipe,
                                                      HDPE,
                                                      PVC,
                                                      ABS,
                                                      FRP,
                                                      Copper,
                                                      Steel,
                                                      Ductileiron,
                                                      ImperialHDPE,
                                                      ImperialPVC,
                                                      ImperialABS,
                                                      ImperialFRP,
                                                      ImperialCopper,
                                                      ImperialSteel,
                                                      ImperialDuctileIron)
                choosecasingcomboboxcoding(cboChooseCasingPipe, HDPE, GalvanizedSteel, Aluminum, PVC, Steel, ImperialHDPE, ImperialGalvanizedSteel, ImperialAluminum, ImperialPVC, ImperialSteel)
    
    
                InternalDiamCarrierPipeCalculation(Externaldiameterofcarrierpipe, CarrierPipeWallThickness)
    
                internaldiamofcasingpipecalculation(Externaldiameterofcarrierpipe, InsulationThickness)
    
                externaldiamofcasingpipe(InternalDiameterofCasingPipe, JacketThickness)
    
                layingdepthtocenterlineofpipecal(LayingDepthtoTopofPipe, ExternalDiameterofCasingPipe)
    
                crosssectionalareaofpipecal(Externaldiameterofcarrierpipe, CarrierPipeWallThickness)
    
                thermalresistanceofinsulationcal(ThermalConductivityofInsulation, InternalDiameterofCasingPipe, Externaldiameterofcarrierpipe)
    
                thermalresistanceofthecarrierpipecal(ThermalConductivityofCarrierPipe, Externaldiameterofcarrierpipe, InternalDiameterofCarrierPipe)
    
                thermalresistanceofthejacketpipecal(ThermalConductivityofCasingPipe, ExternalDiameterofCasingPipe, InternalDiameterofCasingPipe)
    
                thermalresistanceofsoilcal(ThermalConductivityofSoil, LayingDepthToCenterlineOfPipe, ExternalDiameterofCasingPipe)
    
                timetoreachtempcal(Temperaturetoreach, SoilTemp, SupplyTemp, InternalDiameterofCarrierPipe, ThermalResistanceofTheInsulation, ThermalResistanceOfTheCarrierPipe,
                                   ThermalResistanceofTheJacketPipe, ThermalResistanceOfTheSoil)
    
                totalheatgainfromsoilonpiperuncal(TotalLengthofPipeRun, HeatGain)
    
                increaseinsupplytempatoutletcal(TotalHeatGainFromSoilonPipeRun, FlowRateImperial)
    
    
    
                Call HeatTransmissionCoefficientforAsinglePipeCalculation(ThermalResistanceofTheInsulation, ThermalResistanceOfTheCarrierPipe, ThermalResistanceofTheJacketPipe,
                                                                          ThermalResistanceOfTheSoil)
    
                Call VelocityCalculation(FlowRate, Externaldiameterofcarrierpipe, CarrierPipeWallThickness)
    
    
            Else
                ' Error Message Box Signaling the user if he forgets to mention the units
                MessageBox.Show("Please Select The Units You Are Working With.")
    
            End If
    
    
    
    
    
        End Sub
    Thanks!!

  2. #2
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Using Returned value of a function

    You are not specifying a value type to store it in, replace:
    Code:
    Dim InternalDiameterofCasingPipe = Externaldiameterofcarrierpipe + 2 * (insulationthickness / 1000)
    With:
    Code:
    Dim InternalDiameterofCasingPipe As Double = Externaldiameterofcarrierpipe + 2 * (insulationthickness / 1000)
    Not sure what happens when not specifying a type, so I recommend avoiding this at all.

    Or, even better:
    Code:
    Return Externaldiameterofcarrierpipe + 2 * (insulationthickness / 1000)
    EDIT

    Your code is extremely hard to read because of all the long function names, but this could be the issue, but I am not sure.
    You have to use the result of the previous function in your next function:
    Code:
    InternalDiameterofCasingPipe = internaldiamofcasingpipecalculation(Externaldiameterofcarrierpipe, InsulationThickness)
    externaldiamofcasingpipe(InternalDiameterofCasingPipe, JacketThickness)

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    55

    Re: Using Returned value of a function

    Ok, I am not sure if I properly understood that last part but if im correct my code for the funciton that will be using the result from the previous function should look like this?:
    Code:
        Public Function externaldiamofcasingpipe(ByVal jacketthickness As Double) As Double
    
            internaldiameterofcasingpipe = internaldiamofcasingpipecalculation(Externaldiameterofcarrierpipe, InsulationThickness)
            Dim ExternalDiameterofCasingPipe As Double = internaldiameterofcasingpipe + 2 * (jacketthickness / 1000)
    
            Return ExternalDiameterofCasingPipe
        End Function

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Using Returned value of a function

    Uhhh... this was the first draft of the post below. Not sure how it got here; I guess I hit the wrong button. You didn't see this. Move along!

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Using Returned value of a function

    Let's pick some easier methods to keep things sane.

    Let's say you have an Add() function and a Subtract() function:
    Code:
    Public Sub Add(ByVal left As Integer, ByVal right As Integer) As Integer
        Return left + right
    End Sub
    
    Public Sub Subtract(ByVal left As Integer, ByVal right As Integer) As Integer
        Return left - right
    End Sub
    Now let's say you want to evaluate 2 + 3 - 4. Obviously Add() comes first, and Subtract() needs a way to subtract 4 from that result. You *could* add a variable to keep track of this:
    Code:
    Private lastAddResult As Integer = 0
    Public Sub Add(...)
        lastAddResult = left + right
        Return lastAddResult
    End Sub
    
    Public Sub Subtract(ByVal right As Integer)
        Return lastAddResult - right
    End Sub
    This is not a good idea. What happens when you want to try 2 - 3 + 4? Add() doesn't know about any subtraction results! But if you add it, then subtract has to be wrong because it needs to do left - right! You can add overrides to make this work. Then you find out you need to implement Multiply(). Oh no! How does Subtract(Integer) know whether it should use the result from Add() or Subtract()?

    When something starts getting hard, you're probably doing it wrong. So let's think about it again. How would you do 2 + 3 - 4 by hand?
    • Add 2 + 3 and store the result.
    • Subtract 4 from the result.

    Hmm... I had to store the result somewhere. That's an idea. What if I go back to the original Add() and Subtract() and use them this way:
    Code:
    Dim result As Integer
    result = Add(2, 3)
    result = Subtract(result, 4)
    That's what bergerkiller's saying. If you want to use a result from another function, you have to store that result in a variable that the second function can see. The best way to do that is to make it a parameter of the next function. I'd like to show you how to change your code to fix it, but there's a lot of things missing and I think it'd be some major changes. Let's say for a second that "internaldiamofcasingpipecalculation" is working fine. Here's how you might make your first examples work:
    Code:
    Dim internalDiameter As Double = internaldiamofcasingpipecalculation(...)
    Dim externalDiameter As Double = externaldiamofcasingpipe(internalDiameter, ...)
    See how I stored the result in a variable, then used that variable in the next function? That's how it should work.

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    55

    Re: Using Returned value of a function

    I think I got the idea. I am still new to programming so there is still ALOT to learn...
    My code still needs alot of debugging and fixing but I think I am on the right track because the results are finaly making some sence.
    Here is what I have just spent the last hour fixing:
    Code:
    ' Internal Diameter of Carrier Pipe Calculation
        Public Function InternalDiamCarrierPipeCalculation(ByVal Externaldiameterofcarrierpipe As Double, ByVal carrierpipewallthickness As Double) As Double
    
            Dim InternalDiameterofCarrierPipe As Double = Externaldiameterofcarrierpipe - 2 * (carrierpipewallthickness / 1000)
    
            Return InternalDiameterofCarrierPipe
    
        End Function
        ' Internal Diameter of Casing Pipe Calculation
        Public Function internaldiamofcasingpipecalculation(ByVal Externaldiameterofcarrierpipe As Double, ByVal insulationthickness As Double) As Double
    
            Dim InternalDiameterofCasingPipe As Double = Externaldiameterofcarrierpipe + 2 * (insulationthickness / 1000)
    
            Return InternalDiameterofCasingPipe
        End Function
        ' External Diameter of Casing Pipe Calculation
        Public Function externaldiamofcasingpipe(ByVal internaldiameterofcasingpipe As Double, ByVal jacketthickness As Double) As Double
    
            internaldiameterofcasingpipe = internaldiamofcasingpipecalculation(Externaldiameterofcarrierpipe, InsulationThickness)
            Dim ExternalDiameterofCasingPipe As Double = InternalDiameterofCasingPipe + 2 * (jacketthickness / 1000)
    
            Return ExternalDiameterofCasingPipe
        End Function
        ' Laying depth to center line of pipe calculations
        Public Function layingdepthtocenterlineofpipecal(ByVal layingdepthtotopofpipe As Double, ByVal externaldiameterofcasingpipe As Double) As Double
    
            externaldiameterofcasingpipe = externaldiamofcasingpipe(InternalDiameterofCasingPipe, JacketThickness)
            Dim LayingDepthToCenterlineOfPipe As Double = layingdepthtotopofpipe + externaldiameterofcasingpipe / 2
    
            Return LayingDepthToCenterlineOfPipe
        End Function
        ' Crosssectional area of pipe calculation
        Public Function crosssectionalareaofpipecal(ByVal externaldiameterofcarrierpipe As Double, ByVal carrierpipewallthickness As Double) As Double
    
    
            Dim CrossSectionalAreaOfPipe As Double = 3.1415926 * (((externaldiameterofcarrierpipe / 2) ^ 2) - ((externaldiameterofcarrierpipe - 2 * (carrierpipewallthickness / 1000)) / 2) ^ 2)
    
            Return CrossSectionalAreaOfPipe
        End Function
        ' Thermal Resistance of Insulation calculation
        Public Function thermalresistanceofinsulationcal(ByVal thermalconductivityofinsulation As Double, ByVal internaldiameterofcasingpipe As Double, ByVal externaldiameterofcarrierpipe As Double) As Double
    
            internaldiameterofcasingpipe = internaldiamofcasingpipecalculation(externaldiameterofcarrierpipe, InsulationThickness)
    
            Dim ThermalResistanceofTheInsulation As Double = ((1 / (2 * 3.1416 * thermalconductivityofinsulation)) * (Math.Log(internaldiameterofcasingpipe / externaldiameterofcarrierpipe)))
    
            Return ThermalResistanceofTheInsulation
        End Function
        ' Thermal Resistance of the carrier pipe calculation
        Public Function thermalresistanceofthecarrierpipecal(ByVal thermalconductivityofcarrierpipe As Double, ByVal externaldiameterofcarrierpipe As Double, ByVal internaldiameterofcarrierpipe As Double) As Double
            internaldiameterofcarrierpipe = InternalDiamCarrierPipeCalculation(externaldiameterofcarrierpipe, CarrierPipeWallThickness)
            Dim ThermalResistanceOfTheCarrierPipe As Double = ((1 / (2 * 3.1416 * thermalconductivityofcarrierpipe)) * Math.Log(externaldiameterofcarrierpipe / internaldiameterofcarrierpipe))
    
            Return ThermalResistanceOfTheCarrierPipe
        End Function
        ' Thermal Resistance of the Jacket Pipe Calculation
        Public Function thermalresistanceofthejacketpipecal(ByVal thermalconductivityofcasingpipe As Double, ByVal externaldiameterofcasingpipe As Double, ByVal internaldiameterofcasingpipe As Double) As Double
    
            internaldiameterofcasingpipe = internaldiamofcasingpipecalculation(Externaldiameterofcarrierpipe, InsulationThickness)
            externaldiameterofcasingpipe = externaldiamofcasingpipe(internaldiameterofcasingpipe, JacketThickness)
    
            Dim ThermalResistanceofTheJacketPipe As Double = ((1 / (2 * 3.1416 * thermalconductivityofcasingpipe))) * (Math.Log(externaldiameterofcasingpipe / internaldiameterofcasingpipe))
    
            Return ThermalResistanceofTheJacketPipe
        End Function
        ' Thermal resistance of soil calculation
        Public Function thermalresistanceofsoilcal(ByVal thermalconductivityofsoil As Double, ByVal layingdepthtocenterlineofpipe As Double, ByVal externaldiameterofcasingpipe As Double) As Double
    
            externaldiameterofcasingpipe = externaldiamofcasingpipe(InternalDiameterofCasingPipe, JacketThickness)
            Dim ThermalResistanceOfTheSoil As Double = ((1 / (2 * 3.1416 * thermalconductivityofsoil)) * (Math.Log(4 * (layingdepthtocenterlineofpipe + 0.0685 * thermalconductivityofsoil) / externaldiameterofcasingpipe)))
    
            Return ThermalResistanceOfTheSoil
        End Function
        ' Time to reach temperature calculation
        Public Function timetoreachtempcal(ByVal temperaturetoreach As Double, ByVal soiltemp As Double, ByVal supplytemp As Double, ByVal internaldiameterofcarrierpipe As Double, ByVal thermalresistanceoftheinsulation As Double, ByVal thermalresistanceofthecarrierpipe As Double, ByVal thermalresistanceofthejacketpipe As Double, ByVal thermalresistanceofthesoil As Double) As Double
    
            'Time to reach
            internaldiameterofcarrierpipe = InternalDiamCarrierPipeCalculation(Externaldiameterofcarrierpipe, CarrierPipeWallThickness)
            Dim TimeToReachTemperature As Double = -1 * Math.Log((temperaturetoreach - soiltemp) / (supplytemp - soiltemp)) * 998 * 3.1416 * (internaldiameterofcarrierpipe / 2) * (internaldiameterofcarrierpipe / 2) * (thermalresistanceoftheinsulation + thermalresistanceofthecarrierpipe + thermalresistanceofthejacketpipe + thermalresistanceofthesoil) *
            (4190 / 3600)
            Dim RoundedTimeToReachTemperature As Double = Math.Round(TimeToReachTemperature, 3)
            lblTimetoReachTemperature.Text = "Time To Reach Temperature : " & RoundedTimeToReachTemperature & " Hours"
    
            Return RoundedTimeToReachTemperature
        End Function

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    55

    Re: Using Returned value of a function

    Code:
    'Total Heat Gain From Soil On Pipe Run Calculaiton
        Public Function totalheatgainfromsoilonpiperuncal(ByVal totallengthofpiperun As Double, ByVal HeatGain As Double) As Double
    
    
            HeatGain = HeatGainCalculation(HeatTransmissionCoefficientforASinglePipe, SoilTemp, SupplyTemp)
    
            Dim TotalHeatGainFromSoilonPipeRun As Double = totallengthofpiperun * HeatGain
            Dim RoundedTotalHeatGainFromSoilonPipeRun As Double = Math.Round(TotalHeatGainFromSoilonPipeRun, 3)
    
            If RoundedTotalHeatGainFromSoilonPipeRun > 0 Then
                lblTotalHeatGainFromSoilOnPipeRun.Text = " Total Heat Gain From Soil On Pipe Run : " & RoundedTotalHeatGainFromSoilonPipeRun & " Watts "
            ElseIf RoundedTotalHeatGainFromSoilonPipeRun < 0 Then
                lblTotalHeatGainFromSoilOnPipeRun.Text = " Total Heat Loss From Soil On Pipe Run : " & RoundedTotalHeatGainFromSoilonPipeRun & " Watts "
            End If
    
            Return RoundedTotalHeatGainfromsoilonPipeRun
        End Function
        'HeatGain Calculation
        Public Function HeatGainCalculation(ByVal HeatTransmissionCoefficientforASinglePipe As Double,
                                        ByVal SoilTemp As Double,
                                        ByVal SupplyTemp As Double) As Double
    
            InternalDiameterofCasingPipe = internaldiamofcasingpipecalculation(Externaldiameterofcarrierpipe, InsulationThickness)
            ExternalDiameterofCasingPipe = externaldiamofcasingpipe(InternalDiameterofCasingPipe, JacketThickness)
            LayingDepthToCenterlineOfPipe = layingdepthtocenterlineofpipecal(LayingDepthtoTopofPipe, ExternalDiameterofCasingPipe)
            InternalDiameterofCarrierPipe = InternalDiamCarrierPipeCalculation(Externaldiameterofcarrierpipe, CarrierPipeWallThickness)
    
            ThermalResistanceofTheInsulation = thermalresistanceofinsulationcal(ThermalConductivityofInsulation, InternalDiameterofCasingPipe, Externaldiameterofcarrierpipe)
            ThermalResistanceOfTheCarrierPipe = thermalresistanceofthecarrierpipecal(ThermalConductivityofCarrierPipe, Externaldiameterofcarrierpipe, InternalDiameterofCarrierPipe)
            ThermalResistanceofTheJacketPipe = thermalresistanceofthejacketpipecal(ThermalConductivityofCasingPipe, ExternalDiameterofCasingPipe, InternalDiameterofCasingPipe)
            ThermalResistanceOfTheSoil = thermalresistanceofsoilcal(ThermalConductivityofSoil, LayingDepthToCenterlineOfPipe, ExternalDiameterofCasingPipe)
            HeatTransmissionCoefficientforASinglePipe = HeatTransmissionCoefficientforAsinglePipeCalculation(ThermalResistanceofTheInsulation, ThermalResistanceOfTheCarrierPipe,
                                                                                                             ThermalResistanceofTheJacketPipe, ThermalResistanceOfTheSoil)
    
            Dim HeatGain As Double = HeatTransmissionCoefficientforASinglePipe * (SoilTemp - SupplyTemp)
            Dim RoundedHeatGain As Double = Math.Round(HeatGain, 3)
    
            If RoundedHeatGain > 0 Then
                lblHeatGain.Text = " HeatGain : " & RoundedHeatGain & " W/m "
            ElseIf RoundedHeatGain < 0 Then
                lblHeatGain.Text = " HeatLoss : " & RoundedHeatGain & " W/m "
    
            End If
            Return HeatGain
    
        End Function
        ' Increase in supply temp at outlet
        Public Function increaseinsupplytempatoutletcal(ByVal totalheatgainfromsoilonpiperun As Double, ByVal flowrateimperial As Double) As Double
    
    
            totalheatgainfromsoilonpiperun = totalheatgainfromsoilonpiperuncal(TotalLengthofPipeRun, HeatGain)
            flowrateimperial = ConvertVariableToImperial(SoilTemp, SupplyTemp, CarrierPipeWallThickness, InsulationThickness, JacketThickness, Externaldiameterofcarrierpipe,
                                                         LayingDepthtoTopofPipe, TotalLengthofPipeRun, FlowRate, Temperaturetoreach)
            Dim IncreaseInSupplyTemperatureAtOutlet As Double = totalheatgainfromsoilonpiperun / (flowrateimperial * 998 * 4190 * 0.000063)
            Dim RoundedIncreaseInsupplyTemperatureAtOutlet As Double = Math.Round(IncreaseInSupplyTemperatureAtOutlet, 3)
    
            If RoundedIncreaseInsupplyTemperatureAtOutlet > 0 Then
                lblIncreaseinSupplyTemperatureatOutlet.Text = " Incease in supply temperature at outlet : " & RoundedIncreaseInsupplyTemperatureAtOutlet & " Degree C"
            ElseIf RoundedIncreaseInsupplyTemperatureAtOutlet < 0 Then
                lblIncreaseinSupplyTemperatureatOutlet.Text = " Decrease in supply temperature at outlet : " & RoundedIncreaseInsupplyTemperatureAtOutlet & " Degree C"
            End If
    
            Return RoundedIncreaseInSupplyTemperatureAtOutlet
        End Function
        ' Heat transmission coefficient for a single pipe calculation
        Public Function HeatTransmissionCoefficientforAsinglePipeCalculation(ByVal ThermalResistanceofTheInsulation As Double,
                                                                             ByVal ThermalResistanceOfTheCarrierPipe As Double,
                                                                             ByVal ThermalResistanceofTheJacketPipe As Double,
                                                                             ByVal ThermalResistanceOftheSoil As Double) As Double
    
            ThermalResistanceofTheInsulation = thermalresistanceofinsulationcal(ThermalConductivityofInsulation, InternalDiameterofCasingPipe, Externaldiameterofcarrierpipe)
            ThermalResistanceOfTheCarrierPipe = thermalresistanceofthecarrierpipecal(ThermalConductivityofCarrierPipe, Externaldiameterofcarrierpipe, InternalDiameterofCarrierPipe)
            ThermalResistanceofTheJacketPipe = thermalresistanceofthejacketpipecal(ThermalConductivityofCasingPipe, ExternalDiameterofCasingPipe, InternalDiameterofCasingPipe)
            ThermalResistanceOftheSoil = thermalresistanceofsoilcal(ThermalConductivityofSoil, LayingDepthToCenterlineOfPipe, ExternalDiameterofCasingPipe)
    
            Dim HeatTransmissionCoefficientforASinglePipe As Double = (1 / (ThermalResistanceofTheInsulation + ThermalResistanceOfTheCarrierPipe + ThermalResistanceofTheJacketPipe + ThermalResistanceOftheSoil))
    
            Return HeatTransmissionCoefficientforASinglePipe
    
        End Function
    
        ' Velocity Calculation
    
        Public Function VelocityCalculation(ByVal txtFlowRate As Double, ByVal txtExternaldiameterofcarrierpipe As Double, ByVal txtCarrierPipeWallThickness As Double) As Double
    
            Dim InternalDiameterofCarrierPipe As Double = (Externaldiameterofcarrierpipe - 2) * (CarrierPipeWallThickness / 1000)
            Dim Velocity As Double = FlowRate * 0.001 / (3.1416 * InternalDiameterofCarrierPipe * (InternalDiameterofCarrierPipe / 4))
            Dim RoundedVelocity = Math.Round(Velocity, 3)
            lblVelocity.Text = " Velocity : " & RoundedVelocity & " m / sec "
    
    
    
            Return Velocity
    
            Exit Function
        End Function

  8. #8
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Using Returned value of a function

    I have another tip to organize your code, and that is storing your functions as shared in classes:
    Code:
        Public Class PipeCasing
            Public Shared Function InternalDiam(ByVal ExternalDiam As Double, ByVal InsulationThickness As Double) As Double
                Return ExternalDiam + 2 * (InsulationThickness / 1000)
            End Function
            Public Shared Function ExternalDiam(ByVal InternalDiam As Double, ByVal JacketThickness As Double) As Double
                Return InternalDiam + 2 * (JacketThickness / 1000)
            End Function
        End Class
        Public Class PipeCarrier
            Public Shared Function InternalDiam(ByVal ExternalDiam As Double, ByVal WallThickness As Double) As Double
                Return ExternalDiam - 2 * (WallThickness / 1000)
            End Function
        End Class
    For some other calculations this is a bit harder. Try at best to get your calculations inside a class, or even better, have all "pipe objects" as a separate class. Like this:
    Code:
        Public Class Pipe
            Public MyCarrier As Carrier
            Public MyCasing As Casing
            Public MyInsulation As Insulation
            Public MyCrossSection As CrossSection
    
            Public Class Carrier
                Public InternalDiam As Double
                Public ExternalDiam As Double
            End Class
            Public Class Casing
                Public InternalDiam As Double
                Public ExternalDiam As Double
            End Class
            Public Class Insulation
                Public ThermalResistance As Double
            End Class
            Public Class CrossSection
                Public Area As Double
            End Class
        End Class
    You can also add functions to these sub classes, or do the calculation in the new sub. (in the constructor)
    Code:
            Public Class Casing
                Sub New(ByVal InternalDiameter As Double, ByVal JacketThickness As Double)
                    Me.InternalDiam = InternalDiam
                    Me.ExternalDiam = InternalDiam + 2 * (JacketThickness / 1000)
                End Sub
                Public InternalDiam As Double
                Public ExternalDiam As Double
            End Class
    So you can organize your code and make it look very clean:
    Code:
            Dim p As New Pipe()
            p.MyCasing = New Pipe.Casing(100, 200)
    So instead of:
    ThermalResistanceofTheJacketPipe
    You get:
    Pipe.Jacket.ThermalResistance

  9. #9
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Using Returned value of a function

    Quote Originally Posted by bergerkiller View Post
    You are not specifying a value type to store it in, replace:
    Code:
    Dim InternalDiameterofCasingPipe = Externaldiameterofcarrierpipe + 2 * (insulationthickness / 1000)
    With:
    Code:
    Dim InternalDiameterofCasingPipe As Double = Externaldiameterofcarrierpipe + 2 * (insulationthickness / 1000)
    Not sure what happens when not specifying a type, so I recommend avoiding this at all.
    A little off-topic, but as long as Option Infer is on (which it should be by default) then not specifying the type is fine, and VB will infer ('guess') the type from the expression you assign to it.

    For example
    Code:
    Dim x = 3   'x is of type Integer
    Dim y = "Hi"  'y is of type String
    Dim z = New Button()  'z is of type Button
    In your case you are assigning a calculation so you'd have to know the specifics of how VB handles calculations, but it will likely be a Double (I'm too lazy to figure it out at the moment, but since you are dividing it should be Double).

    (If you're familiar with C#, this is the equivalent of the 'var' keyword in C#)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width