我有一个子程序可以计算某些值。这方面的主要输入是通过表单中的数字下拉。在表单中输入数字后,代码会使用它来计算其他值,然后使用这些值创建图表。 我希望第一个子程序的值可以被绘制图形的其他子程序使用。 这是代码:
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
如何从GCalc sub获取dI
的值到INumericUpDown_ValueChanged?
我试图通过其他帖子,但不理解他们。有人可以帮帮我吗?
感谢您的帮助。
三木
答案 0 :(得分:2)
你应该从中返回一些东西 - 所以你必须改为Function
。
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
然后使用它
Dim result As Result = GCalc()
Chart1.Series("Series1").Points.Clear()
Chart1.Series("Series1").Points.AddXY(0, result.dI)
答案 1 :(得分:1)
1 更改您的
Public Sub GCalc()
到函数
Public Function GCalc() as Double
并像这样返回你的价值:
Public Function GCalc()
'your existing code...
return di
End Sub
比你可以调用你的功能
Dim di = Gcalc()
2 或使用byref值:
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
而不是打电话
'Declare di & de..
Dim dI as double
Dim dE as double
'Now fill the variables...
GCalc(byRef dI, byRef dE)
答案 2 :(得分:0)
我认为最好的解决方案是将值存储在全局变量中,因为您需要2个值而不仅仅是1,因此它比使用函数更好。
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