0

Why this code doesn't work? it returns an unexpected value actually total = 0.

total = Cscore(TextBox1.Text) * CDbl(TextBox2.Text)

when trying this

total = TextBox1.Text * TextBox2.Text

it returns the expected value but I need to pass textbox1.text to the function to get the desirable value

the function is this

Public Function Cscore(ByVal score As Integer) As Double

    Select Case score
        Case score = 100
            Return 5.0
        Case score >= 95
            Return 5.0
        Case score >= 90
            Return 4.75
        Case score >= 85
            Return 4.5
        Case score >= 80
            Return 4.0
        Case score >= 75
            Return 3.5
        Case score >= 70
            Return 3.0
        Case score >= 65
            Return 2.5
        Case score >= 60
            Return 2.0
        Case score < 60
            Return 1.0
    End Select
End Function

thanks in advance

1
  • 3
    It would be a lot easier to answer this question if you told us in what way the code doesn't work. Does it throw an error? Return an unexpected value, what? Off the top of my head, you're causing an implicit conversion from string to int by passing the Text property of a Textbox into that function... Commented Sep 5, 2010 at 1:58

4 Answers 4

4

Using Option Strict On would have helped you quickly find the problem. Your syntax for the Case statements is wrong, those expressions evaluate to Boolean. score will rarely be equal to True or False, you only get a value if score equals 0 or 1. You need to write it like this:

    Select Case score
        Case Is = 100
            Return 5.0
        Case Is >= 95
            Return 5.0
        Case Is >= 90
            Return 4.75
        '' etc...
    End Select

Use Option Strict On while you learn to program in VB.NET. You can turn it off again when you've become a master.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for good advice in general. Also consider not turning Option Strict off, because why not let the compiler catch such mistakes?
Also if the same result is returned for >=95 and =100 why have both cases? Unless that is an oversimplification for the purposes of the example code.
@djacobson Option Strict is OFF by default so saying "consider not turning Option Strict off" doesn't make sense. Hans Passant has already said consider turning it ON.
2

For starters, if the value is greater than 100, then this function doesn't have a return value (it will return the default value for the type - 0 in this instance).

You also should use Integer.TryParse to verify all input is an integer before calling a function expecting an Integer.

In the end, I would suggest always having a case else on the end of any select statement.

Comments

0

Use the Val() function to convert a String to an Integer.

http://msdn.microsoft.com/en-us/library/k7beh1x9(VS.80).aspx

Comments

0

what if you try something along the lines of

Dim tb1 as Integer = Integer.Parse(TextBox1.Text)
Dim tb2 as Integer = Integer.Parse(TextBox2.Text)

total = Cscore(tb1) * tb2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.