Not able to access Enum from another class - asp.net

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

Related

Invalid procedure call or argument error vb 6

I have the following code:
I am getting the error in counts.Item(value) = 1.
Set xmlNodeList = xmlEncounter.documentElement.selectNodes("//BillingLIne/Receipts")
If xmlNodeList.length > 1 Then
For i_Loop1 = 1 To xmlNodeList.length
strserviceChecked_GL(i_Loop1) = xmlNodeList.Item(i_Loop1 - 1).Attributes.getNamedItem("ServiceID").nodeValue
Id1tobechecked_GL(i_Loop1) = xmlNodeList.Item(i_Loop1 - 1).Attributes.getNamedItem("Receptid1").nodeValue
Id2tobechecked_GL(i_Loop1) = xmlNodeList.Item(i_Loop1 - 1).Attributes.getNamedItem("Receptid2").nodeValue
Id3tobechecked_GL(i_Loop1) = xmlNodeList.Item(i_Loop1 - 1).Attributes.getNamedItem("Receptid3").nodeValue
Id4tobechecked_GL(i_Loop1) = xmlNodeList.Item(i_Loop1 - 1).Attributes.getNamedItem("Receptid4").nodeValue
Next i_Loop1
End If
Dim value As Variant
Dim counts As Collection
Set counts = New Collection
For Each value In strserviceChecked_GL
If Not Exists(counts,value) Then
counts.Item(value) = 1
Else
counts.Item(value) = counts.Item(value) + 1
End If
Next
Public Function Exists(ByVal oCol As Collection, ByVal vKey As Variant) As Boolean
On Error Resume Next
oCol.Item vKey
Exists = (Err.Number = 0)
Err.Clear
End Function
When a collection has no item for a key, you need to add one, and give it a value as follows.
For Each value In strserviceChecked_GL
If Not Exists(counts,value) Then
' Key 'value' is not in 'counts' collection.
' Add key 'value', and give it the value 1.
counts.Add 1, CStr(value)
'counts.Item(value) = 1
Else
counts.Item(value) = counts.Item(value) + 1
End If
Next

What's the best way to parse an SQL fragment string into a List(of string) for a Listbox control?

I'm trying to take this string:
(("DISPLAY_NAME" like N'sadf%') And ("ID" = 2) And ("IsCRITERION" = null))
and parse it into a List(of string) so that it can be displayed like:
(
(
"DISPLAY_NAME" like N'sadf%'
)
And
(
"ID" = 2
)
Or
(
"IsCRITERION" = null
)
)
I'm close but don't quite have it. My code currently looks like:
Dim filterlist As New List(Of String)
Dim temp As String = String.Empty
Dim lvl As Integer = 0
Dim pad As String = String.Empty
For Each chr As Char In originalString '--- filter is the string i posted above
Select Case chr.ToString.ToLower()
Case "("
filterlist.Add(pad.PadLeft(lvl * 5) & chr)
lvl += 1
Case ")"
filterlist.Add(pad.PadLeft(lvl * 5) & temp)
If lvl > 0 Then lvl -= 1
filterlist.Add(pad.PadLeft(lvl * 5) & chr)
'If lvl > 0 Then lvl -= 1
temp = String.Empty
Case Else
temp &= chr
End Select
Next
'--- Removes the empty line produced by generating the List(of String)
filterlist = filterlist.Where(Function(s) Not String.IsNullOrWhiteSpace(s)).ToList()
listSelectedCriteria.DataSource = filterlist
listSelectedCriteria.DataBind()
Unfortunately, the above code produces something close to what I desire but the "And"s and "Or"s are not in the right places:
(
(
"DISPLAY_NAME" like N'sadf%'
)
(
And "ID" = 2
)
(
Or "IsCRITERION" = null
)
)
Would using regular expressions be better? Thanks for the help
Probably the "best" way (although that's getting into "primarily opinion-based" territory) would be to use a parser, but assuming that your input is limited to similar looking strings, here's what I came up with:
Dim originalString = "((""DISPLAY_NAME"" like N'sadf%') And (""ID"" = 2) And (""IsCRITERION"" = null))"
Dim filterlist = New List(Of String)()
Dim temp = New StringBuilder()
Dim lvl = 0
Dim addLine =
Sub(x As String)
filterlist.Add(New String(" ", lvl * 4) & x.Trim())
End Sub
For Each c In originalString
Select Case c
Case "("
If temp.Length > 0 Then
addLine(temp.ToString())
temp.Clear()
End If
addLine("(")
lvl += 1
Case ")"
If temp.Length > 0 Then
addLine(temp.ToString())
temp.Clear()
End If
lvl -= 1
addLine(")")
Case Else
temp.Append(c)
End Select
Next
If temp.Length > 0 Then
addLine(temp.ToString())
temp.Clear()
End If
filterlist.Dump() ' LINQPad ONLY
This results in:
(
(
"DISPLAY_NAME" like N'sadf%'
)
And
(
"ID" = 2
)
And
(
"IsCRITERION" = null
)
)
However, you will probably end up having to add code as you find different inputs that don't quite work how you want.
Instead of looking at each characters, I would start be doing a split. And then add/remove padding depending on what character is at the start.
Dim tempString As String = "((""DISPLAY_NAME"" like N'sadf%') And (""ID"" = 2) And (""IsCRITERION"" = null))"
Dim curPadding As String = ""
Const padding As String = " "
Dim result As New List(Of String)
For Each s As String In Text.RegularExpressions.Regex.Split(tempString, "(?=[\(\)])")
If s <> "" Then
If s.StartsWith("(") Then
result.Add(curPadding & "(")
curPadding &= padding
result.Add(curPadding & s.Substring(1).Trim())
ElseIf s.StartsWith(")") Then
curPadding = curPadding.Substring(padding.Length)
result.Add(curPadding & ")")
result.Add(curPadding & s.Substring(1).Trim())
Else
result.Add(curPadding & s)
End If
End If
Next

Reset pagesize for each record iText

I am trying to reset the pagesize of each record in pdf, which is the total page
(1 of pagesize
2 of pagesize.......)
The 1st blockcode work for 1 single record but then when it come to multiple record it showed:
1 of 0 //1st record
2 of 0
1 of 0 //2nd record
.......
I think there is something to do with document.setPageSize() but it is boolean and belong to Rectangle.
Please help me solve this problem.
Thank.
Public Overrides Sub onEndPage(ByVal writer As PdfWriter, ByVal document As Document)
Dim page As Rectangle = document.getPageSize()
Dim cb As PdfContentByte = writer.getDirectContent()
Dim arialbasefont As BaseFont = arial.getBaseFont
Dim pg As Rectangle = document.getPageSize()
Dim pageNumberText As String = "Page " & writer.getPageNumber() & " of "
Dim timeStampText As String = Now.ToString
Dim pageNumberTextLength As Double = arialbasefont.getWidthPoint(pageNumberText, footerFontSize)
Dim timeStampTextLength As Double = arialbasefont.getWidthPoint(timeStampText, footerFontSize)
Dim pageNumberTextLeft As Double = 20
Dim templateLeft As Double = pageNumberTextLeft + pageNumberTextLength
Dim pageNumberTextBottom As Double = 5 + footerFontSize
cb.beginText()
cb.setFontAndSize(arialbasefont, footerFontSize)
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, pageNumberText, pageNumberTextLeft, pageNumberTextBottom, 0)
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, Now, pg.urx - (timeStampTextLength + 20), pageNumberTextBottom, 0)
cb.endText()
cb.addTemplate(tpl, templateLeft, pageNumberTextBottom)
End Sub
For Each ProjectID In array
Dim rptRequestReportObj As New rptRequestReport2
rptRequestReportObj.Report(document, ProjectID)
document.newPage()
document.setPageCount(1)
Next ProjectID

Averaging columns in a table - ignoring certain columns

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

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