1

I've the following code which successfully makes an average for all the columns from a table. What I need to do though is ignore certain columns in this equation.

Dim totalNumber as Double = 0
Dim count as Integer = 0

For x = 0 To xyz123.Tables(0).Columns.Count - 1
  Dim current as Double = 0
  If Double.TryParse(xyz123.Tables(0).Rows(0)(x).ToString(), current) AndAlso current <> 0 Then
    count += 1
    totalNumber += current
  End If
Next

Dim averageRating as Double = totalNumber / count
Tom
  • 12,776
  • 48
  • 145
  • 240
  • which columns? are you going to identify them by index/name/their content? – hawbsl Feb 21 '11 at 11:03
  • I already ignore columns with a value of 0 but I also need to ignore columns by name (for example, "weather") – Tom Feb 21 '11 at 11:05

2 Answers2

1

Using your source code you can try this

Dim totalNumber as Double = 0
Dim count as Integer = 0

For x = 0 To xyz123.Tables(0).Columns.Count - 1

Dim current as Double = 0

If Double.TryParse(xyz123.Tables(0).Rows(0)(x).ToString(), current) AndAlso current <> 0 AndAlso x <> 13 AndAlso x <> 1 Then
    count += 1
    totalNumber += current
End If
Next

Dim averageRating as Double = totalNumber / count

In the above example this will ignore columns 14 and 2 in your average

Hope this helps

Jamie Taylor
  • 3,500
  • 21
  • 65
  • 99
0

If you want to exclude certain columns by name e.g. columns named "weather" then test for Columns(x).ColumnName in your code:

Dim totalNumber as Double = 0
Dim count as Integer = 0

For x = 0 To xyz123.Tables(0).Columns.Count - 1

    If Not xyz123.Tables(0).Columns(x).ColumnName="weather" Then
          Dim current as Double = 0
          'etc
hawbsl
  • 15,313
  • 25
  • 73
  • 114