Averaging columns in a table - ignoring certain columns - asp.net

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

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

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

Related

Error return empty arraylist from gridview

I try to compare two arraylist and check the differences between the two. the first arraylist I get from Gridview1 that was created manually, and the second arraylist I get by retrieving some data from database.
Here is my code so far:
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim strCheck As String = txtQuery.Text
Dim arrayList1 As ArrayList = New ArrayList()
Dim arrayList2 As ArrayList = New ArrayList()
Dim arrayList3 As ArrayList = New ArrayList()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
If strCheck.IndexOf("SELECT", StringComparison.CurrentCultureIgnoreCase) >= 0 And
strCheck.IndexOf("Customer ID", StringComparison.CurrentCultureIgnoreCase) >= 0 And
strCheck.IndexOf("SUM", StringComparison.CurrentCultureIgnoreCase) >= 0 And
strCheck.IndexOf("Price ($)", StringComparison.CurrentCultureIgnoreCase) >= 0 And
strCheck.IndexOf("Total Prices", StringComparison.CurrentCultureIgnoreCase) >= 0 And
strCheck.IndexOf("tbl_product", StringComparison.CurrentCultureIgnoreCase) >= 0 And
strCheck.IndexOf("GROUP BY", StringComparison.CurrentCultureIgnoreCase) >= 0 Then
For a = 0 To Gridview2.Rows.Count - 1 Step 1
For b = 0 To Gridview2.Columns.Count - 1 Step 1
arrayList1.Add(Gridview2.Rows(a).Cells(b).Text)
'TarrayList1.Add(arrayList1.Sort())
arrayList1.Sort()
Next
Next
For c = 0 To GridView3.Rows.Count - 1 Step 1
For d = 0 To GridView3.Columns.Count - 1 Step 1
arrayList2.Add(GridView3.Rows(c).Cells(d).Text)
arrayList2.Sort()
Next
Next
For Each listElement As String In arrayList1
If Not arrayList2.Contains(listElement) Then
arrayList3.Add(listElement)
End If
Next
For Each listElement As String In arrayList2
If Not arrayList1.Contains(listElement) Then
arrayList3.Add(listElement)
End If
Next
Dim dtNew As DataTable = New DataTable()
dtNew.Columns.Add("Array")
Dim j As Integer
For j = 0 To arrayList3.Count - 1 Step 1
dtNew.Rows.Add()
dtNew.Rows(j)("Array") = arrayList3(j).ToString()
Next
GridView4.DataSource = dtNew
GridView4.DataBind()
If GridView4.Rows.Count = 0 Then
MsgBox("TRUE")
Else
MsgBox("FALSE")
End If
Else
MsgBox("False")
End If
End Sub
>
The code will compare arraylist1 and arraylist2, if there is any differences then it will be added to arraylist3.
The error result is in
arrayList2
I don't why when I check the arraylist2 it returns an empty arraylist.
Do you have any suggestion why the code give such error?
Thanks in advance.

Count specific datatable columns

How to count datatable columns (created dynamically) that has the same first two characters in the header? Here's my code so far but is not working.
For col As Integer = 3 To dt.Columns.Count - 1
Dim cntLE, cntUE As Integer
If dt.Columns(col).ColumnName.Substring(0, 2) = "LE" Then
cntLE = dt.Columns.Count
ElseIf dt.Columns(col).ColumnName.Substring(0, 2) = "UE" Then
cntUE = dt.Columns.Count
End If
Next
It's because you are assigning the entire column count (i.e. dt.Columns.Count ) to the counters, instead of increment them by 1 if found.
Try this.
For col As Integer = 3 To dt.Columns.Count - 1
Dim cntLE, cntUE As Integer
If dt.Columns(col).ColumnName.Substring(0, 2) = "LE" Then
cntLE = cntLE + 1
ElseIf dt.Columns(col).ColumnName.Substring(0, 2) = "UE" Then
cntUE = cntUE + 1
End If
Next

Not able to access Enum from another class

I'm implementing this in VB.NET by attempting to use the implementation methodology here. I'm getting an error on this line:
Dim score As Master.PasswordScore = Master.CheckStrength(txtPassVal)
The error is:
Type 'Master.PasswordScore' is not defined.
Here is the full code of relevance:
Master Class:
Public Enum PasswordScore
Blank = 0
TooShort = 1
RequirementsNotMet = 2
VeryWeak = 3
Weak = 4
Fair = 5
Medium = 6
Strong = 7
VeryStrong = 8
End Enum
Public Function CheckStrength(ByVal password As String) As PasswordScore
Dim score As Int32 = 0
If password.Length < 1 Then
Return PasswordScore.Blank
End If
If password.Length < 8 Then
Return PasswordScore.TooShort
End If
If password.Contains("password") Or password.Contains("12345678") Then
Return PasswordScore.RequirementsNotMet
End If
If password.Length >= 8 Then
score += 2
End If
If password.Length >= 12 Then
score += 1
End If
If Regex.IsMatch(password, "/\d+/", RegexOptions.ECMAScript) Then
'Contains a number
score += 1
End If
If Regex.IsMatch(password, "/[a-z]/", RegexOptions.ECMAScript) Then
'Contains a lowercase letter
score += 1
End If
If Regex.IsMatch(password, "/[A-Z]/", RegexOptions.ECMAScript) Then
'Contains an uppercase letter
score += 1
End If
If Regex.IsMatch(password, "/.[!,#,#,$,%,^,&,*,?,_,~,-,£,(,)]/", RegexOptions.ECMAScript) Then
'Contains special character
score += 2
End If
Return CType(score, PasswordScore)
End Function
Using the code in another class:
Dim score As Master.PasswordScore = Master.CheckStrength(txtPassVal)
Dim i As Int32 = CType(score, Int32)
If i < 4 Then
lblMsg.Text = "Password does not meet minimum security requirements."
lblPasswordStrength.ForeColor = Drawing.Color.Red
Exit Sub
ElseIf i > 3 Then
lblPasswordStrength.ForeColor = Drawing.Color.DarkGreen
End If
lblPasswordStrength.Text = score.ToString()
It says it is not defined while it is. Any reason why it wouldn't give me access to it?
You can't access the function like this
Dim score As Master.PasswordScore = Master.CheckStrength(txtPassVal)
Define the 'CheckStrength' function as 'Shared':
Public Shared Function CheckStrength(ByVal password As String) As PasswordScore
Edit: I would suggest you to move the Enum 'PasswordScore' outside the class Master and then access it like this:
Dim score As PasswordScore = Master.CheckStrength(txtPassVal)
Edit 2:
Public Class Master
Public Shared Function CheckStrength(ByVal password As String) As PasswordScore
Dim score As Int32 = 0
If password.Length < 1 Then
Return PasswordScore.Blank
End If
If password.Length < 8 Then
Return PasswordScore.TooShort
End If
If password.Contains("password") Or password.Contains("12345678") Then
Return PasswordScore.RequirementsNotMet
End If
If password.Length >= 8 Then
score += 2
End If
If password.Length >= 12 Then
score += 1
End If
If Regex.IsMatch(password, "/\d+/", RegexOptions.ECMAScript) Then
'Contains a number
score += 1
End If
If Regex.IsMatch(password, "/[a-z]/", RegexOptions.ECMAScript) Then
'Contains a lowercase letter
score += 1
End If
If Regex.IsMatch(password, "/[A-Z]/", RegexOptions.ECMAScript) Then
'Contains an uppercase letter
score += 1
End If
If Regex.IsMatch(password, "/.[!,#,#,$,%,^,&,*,?,_,~,-,£,(,)]/", RegexOptions.ECMAScript) Then
'Contains special character
score += 2
End If
Return CType(score, PasswordScore)
End Function
End Class
Public Enum PasswordScore
Blank = 0
TooShort = 1
RequirementsNotMet = 2
VeryWeak = 3
Weak = 4
Fair = 5
Medium = 6
Strong = 7
VeryStrong = 8
End Enum

VB.net For Loop to capture data from multiple controls (CheckBoxList Items)

I have two CheckBoxList controls (chkListVideoMedia and chkListAudioMedia) on my page that I want to capture information from and insert the records into the database. I have it working for one of the controls, I just need help modifying my code below to include the second CBL
Dim values As New ArrayList()
For counter As Integer = 0 To chkListVideoMedia.Items.Count - 1
If chkListVideoMedia.Items(counter).Selected Then
MyTextBox.Text = chkListVideoMedia.Items(counter).Value
values.Add(newId)
End If
Next
If values.Count > 0 Then
For item As Integer = 0 To values.Count - 1
If item = 0 Then
MyMedia1.Text = values(item).ToString
End If
If item = 1 Then
MyMedia2.Text = values(item).ToString
End If
If item = 2 Then
MyMedia3.Text = values(item).ToString
End If
If item = 3 Then
MyMedia4.Text = values(item).ToString
End If
Next
End If
Thanks,
James
You can find out which Collection has the most Items, then check to make sure that count is not greater than the maximum Items in each collection. Something like this.
Dim values As New ArrayList()
Dim counter As Integer
If chkListVideoMedia.Items.Count > chkListAudioMedia.Items.Count Then
counter = chkListVideoMedia.Items.Count - 1
Else
counter = chkListAudioMedia.Items.Count - 1
End If
For x = 0 To counter
If Not (counter > chkListVideoMedia.Items.Count - 1) Then
'Do your work here
End If
If Not (counter > chkListAudioMedia.Items.Count - 1) Then
'Do your work here
End If
Next

ASP.Net get columns with values, add them and then divide

I've got some code which is working as it should but it just seems like a bit of a round about way in doing it and wondered if anyone had any ideas of how to tidy it up
Here's my code
Dim TotalNumber As Double
Dim NumberFilled As String
NumberFilled = Nothing
For x = 0 To drCode2a.Tables(0).Columns.Count - 1
If Not drCode2a.Tables(0).Rows(0)(x).ToString() = "0" Then
NumberFilled += drCode2a.Tables(0).Rows(0)(x).ToString() & "-"
TotalNumber = TotalNumber + drCode2a.Tables(0).Rows(0)(x).ToString()
End If
Next
Dim delimiters As Char() = New Char() {"-"c}
Dim TotalNumberFilled As String() = NumberFilled.Split(delimiters, StringSplitOptions.RemoveEmptyEntries)
Dim AverageRating As Double = TotalNumber / TotalNumberFilled.Length
Response.Write(NumberFilled & "<br/>" & TotalNumber & "<br/>" & AverageRating)
Basically for my example NumberFilled = "1-2-" and TotalNumber = 3 and AverageRating = 1.5
It shows that 2 columns were filled out and the total they equal is 3 so the average = 1.5
As I said it works like it should but I'd like to tidy up if possible
Thanks
Dim totalNumber as Double = 0
Dim count as Integer = 0
For x = 0 To drCode2a.Tables(0).Columns.Count - 1
Dim current as Double = 0
If Double.TryParse(drCode2a.Tables(0).Rows(0)(x).ToString(), current) AndAlso current <> 0 Then
count += 1
totalNumber += current
End If
Next
Dim averageRating as Double = totalNumber / count
Here is an alternative. I don't know that it's necessarily better, but it does make use of the StringBuilder object which is a little faster than strings and it keeps the total values as doubles rather than performing double to string to double conversions.
Dim NumberFilled As New StringBuilder("")
Dim TotalNumber as Double
Dim ColumnsFilled as Integer = 0
For each column as DataColumn in drCode2a.Tables(0).Columns
Dim value = drCode2a.Tables(0).Rows(0)(column.ColumnName).ToString()
If Not "0".Equals(value, StringComparison.OrdinalIgnoreCase) Then
If NumberFilled.Length > 0 Then NumberFilled.Append("-")
NumberFilled.Append(value)
TotalNumber = Convert.ToDouble(value)
ColumnsFilled += 1
End If
Next

Resources