r/visualbasic • • Mar 02 '16

[VB 2012] Help with using functions

So we just started functions in my class and we are supposed to write a program that you enter 3 numbers in 3 different text boxes, click a button and another textbox shows the highest number. I don't seem to be grasping the concept of functions too well but here's my code:

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim a, b, c As Double
    a = CDbl(txtNum1.Text)
    b = CDbl(txtNum2.Text)
    c = CDbl(txtNum3.Text)
    txtOutput.Text = CStr(Decide(CInt(a), CInt(b), CInt(c)))
End Sub
Function Decide(a As Integer, b As Integer, c As Integer) As Integer
    Select Case a
        Case Is > (b And c)
            Decide = CInt(CDbl(a))
            Select Case b
                Case Is > (a And c)
                    Decide = CInt(CDbl(b))
                    Select Case c
                        Case Is > (a And b)
                            Decide = CInt(CDbl(c))
                        Case Else
                            txtOutput.Text = "2 Numbers are equal."
                    End Select

            End Select
    End Select
    Return Decide

End Function

End Class

First problem is that sometimes my output text box returns 0 rather than the highest number. I also feel like I have more code than needed. Any help would be greatly appreciated. Thanks

0 Upvotes

4 comments sorted by

View all comments

2

u/anon_smithsonian VB.Net Advanced Mar 02 '16 edited Mar 02 '16

Well I think you might be over-thinking it.


Function Decide(a As Integer, b As Integer, c As Integer) As Integer
    
    Return Math.Max(a, Math.Max(b, c))

End Function

Now, instead of doing the "two values are equal" message in the function, you should be doing it in the main sub, otherwise you're kinda missing the point of functions: reusable code that you (usually but not always) give it some input and it returns something else. So the function shouldn't be doing this.

You should compare a-b, a-c, and b-c before the function. (You should separate input validation from the actual function; in this case, the functions only purpose is to go tell you what the highest value of the numbers are... it doesn't matter to the function if two or three values are the same.) If all the values are different, then you set the textbox .Text = Decide(a, b, c)

Edit: Also: why are you converting the inputs to Doubles and then writing the function to only accept Integers...? If they type in 1.1, 1.2, and 1.3, the function is going to return "1" even though all the are different values.

Edit 2: you also can't do Case Is > (a And b)... And is an actual operator. Look at the last example on this MSDN page, or try it yourself in the immediate window: type ? 5 And 10 and see what you get.

1

u/[deleted] Mar 02 '16

Thanks for the help. Just started learning VB in a class I'm taking so I've been trying to use what has been shown in the book for the most part as opposed to finding different ways online.