-
Aug 13th, 2014, 07:07 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Function '' doesn't return a value on all code paths.
Code:
Private Function RequiredEntry() As Boolean
If txtCakeID.Text = "" Or txtName.Text = "" Or txtCake_Description.Text = "" Or txtWeight.Text = "" Or txtPrice.Text = "" = True Then
MsgBox("Please enter required * information....", MsgBoxStyle.Critical, "Attention...")
Return True
Exit Function
End If
End Function
Can someone please tell me why do I get this warning.
"Function 'RequiredEntry' doesn't return a value on all code paths."
And also in the above code "End Function" is underlined.
The program works fine without any errors but I want to make it perfect by fixing this warning error.
Thank You!
-
Aug 13th, 2014, 07:32 AM
#2
Re: Function '' doesn't return a value on all code paths.
You get the warning because the function doesn't return a value on all code paths just as it says. When your if statement result in true, the code encounters a Return statement, However when the if statement results in false, there is no Return statement encountered.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Aug 13th, 2014, 07:53 AM
#3
Re: Function '' doesn't return a value on all code paths.
That's why I'm not a fan of returns in the middle of functions. I'm one of those types that subscribes to the Single Entry Single Exit methodology... But, there are sometimes when it's appropriate to shuck that and do what's needed.
At any rate... there's two ways to solve it... one is to provide a return on the alternate path.
Code:
Private Function RequiredEntry() As Boolean
If txtCakeID.Text = "" Or txtName.Text = "" Or txtCake_Description.Text = "" Or txtWeight.Text = "" Or txtPrice.Text = "" = True Then
MsgBox("Please enter required * information....", MsgBoxStyle.Critical, "Attention...")
Return True
Exit Function
End If
Return False ' Presumably since your If returns true, the alternate returns false
End Function
I call this the pepper method, your function gets peppered with returns.
The other is to use the single exit. I usually start all my functions off the same way:
Code:
'(Stub)
Private Function SomeFunction() as Boolean
Dim retValue as Boolean = False ' Default value
Return retValue
End function
Then I flesh it out.
so yours would look like this:
Code:
Private Function RequiredEntry() As Boolean
Dim retValue as Boolean = False
If txtCakeID.Text = "" Or txtName.Text = "" Or txtCake_Description.Text = "" Or txtWeight.Text = "" Or txtPrice.Text = "" = True Then
MsgBox("Please enter required * information....", MsgBoxStyle.Critical, "Attention...")
retValue = True
End If
Return retValue
End Function
Now I'm guaranteed to ensure a value gets returned along all code paths.
Again, this is really just my design preference. Some people swear by the former while others will swear by the latter. I've actually seen problems with both. Main key I think is to be consistent.
-tg
-
Aug 13th, 2014, 07:56 AM
#4
Re: Function '' doesn't return a value on all code paths.
The other is to use the single exit. I usually start all my functions off the same way:
That's how I do it as well.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Aug 13th, 2014, 08:08 AM
#5
Thread Starter
Addicted Member
Re: Function '' doesn't return a value on all code paths.
thanks a lot Kebo and Techgnome. It worked.
-
Aug 13th, 2014, 09:42 AM
#6
Re: [RESOLVED] Function '' doesn't return a value on all code paths.
Another thing is that you should never need an "Exit Function". If you've used the "Return" statement, that's all that's needed.
The only time "Exit Function" ever makes sense is if you're using the old "function name as a temp variable" scheme, which is absolutely a bad idea now that "Return" is available (since VB.NET version 1).
-
Aug 13th, 2014, 12:39 PM
#7
Re: [RESOLVED] Function '' doesn't return a value on all code paths.
I am going to nit pick at every Ones examples here. For One no messagebox should really be used here. It's a validation function. Thats it's task. Next there is comparing booleans to true or false. RETURN the orelse statement.
Code:
Dim isvalid = txtCakeID.Text = "" Or txtName.Text = "" Or txtCake_Description.Text = "" Or txtWeight.Text = "" Or txtPrice.Text = ""
Even add some LINQ
vb Code:
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If RequiredEntry() Then ' passed Else MessageBox.Show("Invalid!!!") End If End Sub Private Function RequiredEntry() As Boolean Return Not {Me.TextBox1, Me.TextBox2, Me.TextBox3}.Any(Function(tb) tb.Text.Trim = String.Empty) End Function End Class
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|