Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
Hello everyone : )

I was searching online on how to do coding to calculate standard deviation and came across the idea of using Excel in Visual Studio to calculate StDev for a dataset.

I apply the coding as followed to try using Excel StDev to do the math.

VB
Private Function Compute(ByVal data As ArrayList) As Double

        Dim result As Double = 0.0
        Dim sum As Double = 0.0
        Dim i As Integer = 0
        Dim count As Integer = 0


        'CALCULATE AVERAGE
        If ComputationType.Equals("Average") Then

            For i = 0 To data.Count - 1
                If data(i) Is DBNull.Value Then 'IMPORTANT! SOME RECORD HAS NULL VALUE SO MUST ADD A ZERO
                    sum += 0
                Else
                    sum += data(i)
                End If

                count += 1
            Next i

            result = sum / data.Count

            'CALCULATE STANDARD DEVIATION
        ElseIf ComputationType.Equals("STDev") Then

            result = Application.WorksheetFunction.StDev(data(i))

           Return result

    End Function


However, Visual Studio says that Application.WorksheetFunction is not a member of System.Windows.Forms.Application.

I will greatly appreciate if some pros can explain to me what went wrong and how to solve this problem? Thank you very much :)
Posted

1 solution

You need to import Microsoft.Office.Interop.Excel namespace (and reference the corrensponding assembly).
Here[^] you may find sample code.

However, for such a relatively simple task I won't use <code>Excel, after all VB.NET is a fully-featured programming language: check out the function definition (for instance here[^]) and code it yourself.
 
Share this answer
 
Comments
Boon How 18-Jan-12 19:38pm    
Thanks CPallini for the advise. I have imported Microsoft.Office.Interop.Excel as you suggested but the word "Application" could not be understood by VB.

Previously, I did the coding for standard deviation in VB.net but my answer was different from the excel calculation. I was told to use excel application in VB to avoid mis-calculation by my superior.
CPallini 19-Jan-12 3:33am    
Did you reference the right assembly?
BTW If you post the code of your sd calculation we may try to fix it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900