Chidi Okeh
Chidi Okeh

Reputation: 1557

Do you know how I can get this function to produce the correct result?

The function below is supposed to calculate user's answers to questions.

If the user hasn't take the test, show a message that the user hasn't taken the test.

If the user's answer is between 0% and 75%, let the user know s/he doesn't meet minimum requirement.

If the user's score is greater than 75%, then user has passed.

Problem is that if the user's score is 0, then user gets a message that s/he hasn't taken the test and this wrong.

Any idea how I can correct this?

Thanks a lot in advance

Public Function getStatus(ByVal value As Object) As String
    Dim myRow As FormViewRow = scoreGridView.Row
    Dim Message As String = ""
    Dim lbscore As Label = DirectCast(myRow.FindControl("PercentLabel"), Label)
    If Decimal.Parse(value) >= 0 AndAlso Decimal.Parse(value) < 75 Then
        Message = "<span style='color:red;font-weight:bold'>Your score does not meet minimum requirement</span>"
    ElseIf Decimal.Parse(value) >= 75 Then
        Message = "<span style='color:green;font-weight:bold'>Congratulations you have passed the test!</span>"
    Else
        Message = "You have not yet taken the test for this survey"
    End If
    Return Message
End Function

Upvotes: 3

Views: 83

Answers (2)

Karl Anderson
Karl Anderson

Reputation: 34844

Use the d literal suffix for your values to ensure they are compared as Decimal types, like this:

Public Function getStatus(ByVal value As Object) As String
    Dim myRow As FormViewRow = scoreGridView.Row
    Dim Message As String = ""
    Dim lbscore As Label = DirectCast(myRow.FindControl("PercentLabel"), Label)
    If Decimal.Parse(value) >= 0d AndAlso Decimal.Parse(value) < 75d Then
        Message = "<span style='color:red;font-weight:bold'>Your score does not meet minimum requirement</span>"
    ElseIf Decimal.Parse(value) >= 75d Then
        Message = "<span style='color:green;font-weight:bold'>Congratulations you have passed the test!</span>"
    Else
        Message = "You have not yet taken the test for this survey"
    End If
    Return Message
End Function

Altenatively, for checking against zero (decimal), you can use the Decimal.Zero, like this:

If Decimal.Parse(value) >= Decimal.Zero AndAlso Decimal.Parse(value) < 75d Then

Upvotes: 1

Roldan Tenido
Roldan Tenido

Reputation: 23

what are you trying to achieve?

Base in your code it will return "Your score does not meet minimum requirement" when the value is 0.

and returning "You have not yet taken the test for this survey" if the value is < 0.

Upvotes: 2

Related Questions