Joseph Nields
Joseph Nields

Reputation: 5661

Suppress Warning in VB.NET

So, I have some VB.NET code I wrote. Something along the lines of this:

Public Function foo() As Object
    Select(someInt)
        Case 1
        Case 2
        Case 3
            return doStuff()
        Case Else
            Throw New ArgumentException("Can't supply that argument to this method.")
    End Select
End Function

This is fine code. If someone passes an invalid argument to the method that uses this snippet, it will tell them exactly where and why something went wrong and they can fix it in no time. However… it "doesn't return a value on every code path." How can I suppress this warning?

I'd like to have this warning available on other methods, but not on this one.

Warning message:

Upvotes: 0

Views: 437

Answers (3)

siride
siride

Reputation: 209575

VB.NET doesn't work like C# with switch statements. You can't just stack the cases on top of each other and have fall through. There is no fall through. Instead, you must put a return in each case, or use the comma notation:

Public Function foo() As Object
    Dim someInt As Integer = 0
    Select Case (someInt)
        Case 1
            Return 0
        Case 2
            Return 0
        Case 3
            Return 0
        Case Else
            Throw New ArgumentException("Can't supply that argument to this method.")
    End Select
End Function

Or

Public Function foo() As Object
    Dim someInt As Integer = 0
    Select Case (someInt)
        Case 1, 2, 3
            Return 0
        Case Else
            Throw New ArgumentException("Can't supply that argument to this method.")
    End Select
End Function

Upvotes: 5

Mork
Mork

Reputation: 404

Your code are inside a function.

Public Function NameHere(ByVal someInt as Integer) as Object

You need to return some value:

NameHere = "SomeValue"

Maybe you forgot this. Other solution is change Function to Sub and isn't need return value.

EDIT

Maybe you want to send parameter by reference:

Public Sub foo(ByRef someStuff as String) As Object
    Select(someInt)
        Case 1
        Case 2
        Case 3
            someStuff = doStuff()
        Case Else
            Throw New ArgumentException("Can't supply that argument to this method.")
    End Select
End Sub

This avoid your Warning

Upvotes: -1

Scott Mitchell
Scott Mitchell

Reputation: 8759

This error message indicates that this is within a Function, correct?

You need to return a value after doStuff(), so something like the following (presuming your function returns an Integer):

Select(someInt)
    Case 1
    Case 2
    Case 3
        doStuff()

        Return 0
    Case Else
        Throw New ArgumentException("Can't supply that argument to this method.")
End Select

If this doesn't answer your question, it might help if you share with us your entire function body.

Upvotes: 0

Related Questions