tags:

views:

63

answers:

4

Hi,

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

+2  A: 

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.

davisoa
A: 

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

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

James
A: 

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
rockinthesixstring
same result0 is returned
Fahad
+4  A: 

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.

Hans Passant
thank you this works
Fahad
+1 for good advice in general. Also consider *not* turning Option Strict off, because why not let the compiler catch such mistakes?
djacobson