Miki Gajjar
Miki Gajjar

Reputation: 61

Use result from one subroutine in another for vb.net

I have a subroutine that does a calculation for certain values. The primary input for this is via a numericupdown in a form. Once the numbers are entered in the form, the code uses it to calculate other values which are then used to create a graph. I want the values from the first subroutine to be used by other subroutine which is plotting the graph. Here is the code:

Public Sub GCalc()
    Dim iI As Integer      
    Dim iE As Integer      
    Dim iT As Integer       
    Dim dI, dE As Double   'For chart datapoints

    iI = INumericUpDown.Value
    iE = ENumericUpDown.Value

    iT = iI + iE

    dI = (iI / iT) * 100
    dE = (iE / iT) * 100

End Sub

Private Sub INumericUpDown_ValueChanged(sender As Object, e As EventArgs) Handles INumericUpDown.ValueChanged
    'Clear any existing datapoint for that series and add the new data point when the numericupdown value is changed.
    Chart1.Series("Series1").Points.Clear()
    Chart1.Series("Series1").Points.AddXY(0, dI)
End Sub

How do I get the value of dI from GCalc sub to INumericUpDown_ValueChanged? I tried to go through the other posts but did not understand them. Can someone please help me with this?

Thanks for the help.

Miki

Upvotes: 0

Views: 124

Answers (3)

Cadburry
Cadburry

Reputation: 1864

1 Change your

Public Sub GCalc()

to a Function

Public Function GCalc() as Double

and return your value like this:

Public Function GCalc()
    'your existing code...
  return di
End Sub

than you can call your function

Dim di = Gcalc()

2 or use a byref value:

Public Sub GCalc(ByRef dI As Double, ByRef dE As Double)
    Dim iI As Integer
    Dim iE As Integer
    Dim iT As Integer
    'Dim dI, dE As Double   'For chart datapoints

    iI = INumericUpDown.Value
    iE = ENumericUpDown.Value

    iT = iI + iE

    dI = (iI / iT) * 100
    dE = (iE / iT) * 100
End Sub

than call

'Declare di & de..
Dim dI as double
Dim dE as double

'Now fill the variables...
GCalc(byRef dI, byRef dE)

Upvotes: 1

bugs2919
bugs2919

Reputation: 358

I think that the best solution is store the value on global variables, because you need 2 values not only 1, so its better than using functions.

    Dim dI, dE As Double  = 0.00

    Public Sub GCalc()
        Dim iI As Integer      
        Dim iE As Integer      
        Dim iT As Integer     

        iI = INumericUpDown.Value
        iE = ENumericUpDown.Value

        iT = iI + iE

        dI = (iI / iT) * 100
        dE = (iE / iT) * 100

    End Sub



  Private Sub INumericUpDown_ValueChanged(sender As Object, e As EventArgs) Handles INumericUpDown.ValueChanged
 GCalc()
        'Clear any existing datapoint for that series and add the new data point when the numericupdown value is changed.
        Chart1.Series("Series1").Points.Clear()
        Chart1.Series("Series1").Points.AddXY(0, dI)
    End Sub

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190925

You should return something from it - so you have to make it a Function instead.

Structure Result
   Public dI As Double
   Public dE As Double
End Structure
Public Function GCalc() As Result
    Dim iI As Integer      
    Dim iE As Integer      
    Dim iT As Integer       
    Dim dI, dE As Double   'For chart datapoints

    iI = INumericUpDown.Value
    iE = ENumericUpDown.Value

    iT = iI + iE

    dI = (iI / iT) * 100
    dE = (iE / iT) * 100
    Dim result As New Result
    result.dI = dI
    result.dE = dE
    Return result
End Function

Then using it

Dim result As Result = GCalc()
Chart1.Series("Series1").Points.Clear()
Chart1.Series("Series1").Points.AddXY(0, result.dI)

Upvotes: 2

Related Questions