R
ryguy7272
I am teaching myself VB.NET. I got this code from a book:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dblReturns As Double() = New Double(9) {0.0203, -0.0136, 0.0012,
0.0266, -0.0063, -0.0601, 0.0307, 0.0123, 0.0055, 0.0441}
Label1.Text = Format(Average(dblReturns), "#.#####")
'Label2.Text = Format(Var(dblReturns), "#.#####")
End Sub
Public Function Average(ByRef InArray As Double()) As Double
Dim dblTotalReturn As Double
Dim x As Integer
Dim dblLength# = UBound(InArray, 1)
For x = 0 To dblLength
dblTotalReturn += InArray(x)
Next x
Return dblTotalReturn / (dblLength + 1)
End Function
The Average function works just fine, but I am wondering if there is an
easier way to do this. Also, I am trying to figure out how to calculate
Variance. Are there built-in functions for these types of calculations (and
others)? I’ve been using both Excel and Access for quite some time and these
functions are simply built into the apps. I find it hard to believe that
VB.NET would not offer the same type of courtesy to users, but after
searching online for a bit, I have come up with nothing.
Please help.
Thanks,
Ryan---
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dblReturns As Double() = New Double(9) {0.0203, -0.0136, 0.0012,
0.0266, -0.0063, -0.0601, 0.0307, 0.0123, 0.0055, 0.0441}
Label1.Text = Format(Average(dblReturns), "#.#####")
'Label2.Text = Format(Var(dblReturns), "#.#####")
End Sub
Public Function Average(ByRef InArray As Double()) As Double
Dim dblTotalReturn As Double
Dim x As Integer
Dim dblLength# = UBound(InArray, 1)
For x = 0 To dblLength
dblTotalReturn += InArray(x)
Next x
Return dblTotalReturn / (dblLength + 1)
End Function
The Average function works just fine, but I am wondering if there is an
easier way to do this. Also, I am trying to figure out how to calculate
Variance. Are there built-in functions for these types of calculations (and
others)? I’ve been using both Excel and Access for quite some time and these
functions are simply built into the apps. I find it hard to believe that
VB.NET would not offer the same type of courtesy to users, but after
searching online for a bit, I have come up with nothing.
Please help.
Thanks,
Ryan---