Question Standard Deviation using DataGridview?

rnestor3

Member
Joined
Aug 3, 2013
Messages
14
Programming Experience
Beginner
I'm in a class college and I'm a beginner in programming. I'm trying to make a replica windows statistical calculator and I hit a road bump with the standard deviation button here is my code so far

Private Sub btnsigman1_Click(sender As System.Object, e As System.EventArgs) Handles btnsigman1.Click

Dim totalvalue As Integer
For Each dgvRow As DataGridViewRow In dgstat.Rows
If Not dgvRow.IsNewRow Then
totalvalue += CInt(dgvRow.Cells(1).Value)

End If
Next
txtinput.Text = totalvalue
End Sub
End Class

I've tried a lot from here and can't seem to come out right anyway i go, also there is a button on there Called F-E that i cannot get figured out. I have to turn this in Monday so any help would be really appreciated.
 
man I feel like im bugging you to death with this. I tried implementing all of what you said on the previous post and I got this error message

Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Windows.Forms.DataGridViewRow,System.Object]' to type 'System.Collections.Generic.IEnumerable`1[System.Int32]'.
 
thanks a million, I'm really not trying to get on here and get quick answers on things....I just could not figure this button out. Every other button on the calculator I have done except this one and the F-E button, not real sure about it either. I'm not sure how to make it stay into one mode until its clicked again to go back into the other
 
Dim squareDifferences As IEnumerable(Of Integer) = colValues.Select(Function(x) Math.Abs(x - setAverage) ^ 2) now im getting that same message coming up for this line
 
I just put everything that you had:
Private
Sub btnsigman1_Click(sender As System.Object, e As System.EventArgs) Handles btnsigman1.Click

Dim colvalues As IEnumerable(Of Integer) = From dgvrow As DataGridViewRow In dgstat.Rows

Where Not dgvrow.IsNewRow

Select CInt(dgvrow.Cells(1).Value)

Dim setaverage As Integer = colvalues.Average()

Dim Squaredifferences As IEnumerable(Of Integer) = colvalues.Select(Function(x) Math.Abs(x - setaverage) ^ 2)

Dim Stddeviation As Integer = Math.Sqrt(Squaredifferences.Average())

End Sub


then I also added txtinput.text = stddeviation at the end
 
Hmm that is a bit strange, maybe someone else can shed light on this, it looks like visual studio is confused somehow, and insists on returning a double... Yet the Linq version works fine...

weird.png
 
Hi All,

Some comments for you:-

VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  'With Option Strict On, This LINQ query will cause an error due to the Fact that Option Strict
  'Disallows Implicit Conversion from Object to DataGridViewRow.
  'To solve this you need to cast the DGV.Rows to a DataGridViewRow
  Dim colvalues As IEnumerable(Of Integer) = (From dgvrow As DataGridViewRow In dgstat.Rows.Cast(Of DataGridViewRow)() Where (Not dgvrow.IsNewRow) Select CInt(dgvrow.Cells(1).Value))
 
  'When using Average type Double is always returned since a decimal value can easily be returned.
  'i.e the average of 2 and 3 is 2.5, so that makes sense
  Dim setaverage As Double = colvalues.Average()
 
  'Since you have got type Double in the Average then that decimal can persist throughout the
  'Calculation and therefore type Double must be carried through to the other variables
  Dim Squaredifferences As IEnumerable(Of Double) = colvalues.Select(Function(x) Math.Abs(x - setaverage) ^ 2)
 
  Dim Stddeviation As Double = Math.Sqrt(Squaredifferences.Average())
  MsgBox(Stddeviation.ToString)
 
  'In the case of your LINQ Query Herman, you were using Option Infer to automatically infer the type
  'needed in the end result since you had Deviation = Math( etc .....)
  'If you hover the mouse over the Derivation variable you will see that type Double is inferred
  'So, to expand your LINQ query, you could have said:-
  Dim valueset = colvalues.ToArray
  Dim Deviation As Double = Math.Sqrt(valueset.Select(Function(n) Math.Abs(n - valueset.Average()) ^ 2).Average())
  MsgBox(Deviation.ToString)
 
  'All in all guys, everything falls into place by using Casting and Double types
End Sub

Hope that helps.

Cheers,

Ian

BTW, rnestor3, when posting code please use the Advanced Button to add Code tags around the code for readability.
 
In any case, the one liner to me is clearer and less code...

Math.Sqrt(colValues.Select(Function(x) Math.Abs(x - colValues.Average()) ^ 2).Average())
 
The overload for integer is used by Sqrt, even if Double is used internally, so any undesirable decimal is removed from the final deviation:

But, you would not want to do that Herman since it would corrupt the calculation. What if you were calculating the Standard Deviation on this population:-

VB.NET:
Dim valueset As Double() = {2.2, 4.3, 4.4, 4.5, 5.5, 5.5, 7.6, 9.6}

The answer would be 2.11 etc.... and not 2.

Cheers,

Ian
 
Last edited:
Back
Top