|
-
Aug 26th, 2009, 01:15 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] No value on all code paths
Hi Guys,
In more and more of my functions (althought they still work) i'm getting a warning the function doesn't return a value on all code paths.
for example:
vb.net Code:
Function functionLoadHiddenFields(ByVal fileLocation As String, ByVal Y As Integer)
'// Check if the file exists
If File.Exists(fileLocation) Then
'// Load the xml document
Dim XMLDoc As New Xml.XmlDocument
'// Find and load the XML file
XMLDoc.Load(fileLocation)
'// Tell where to look for the data we need
Dim XMLItem As Xml.XmlNode = XMLDoc.SelectSingleNode("hiddenFields/hiddenValues")
'MessageBox.Show((XMLItem.Attributes("hiddenFieldsID" & (Y)).InnerText))
Return XMLItem.Attributes("hiddenFieldsID" & (Y)).InnerText
End If
End Function
if i put in the return false it wouldn't show the message, is it basically a way of saying:
if expression true do this, return true, else return false
i know in PHPO for example i can just return either true, or a value.
thanks for any info guys
Graham
-
Aug 26th, 2009, 01:26 PM
#2
Re: No value on all code paths
If the "IF" evaluates false then the sub does not return anything.
You're also missing the type of what you're returning;
Function functionLoadHiddenFields(ByVal fileLocation As String, ByVal Y As Integer) As String
So you might want to do something like;
If Whatever
Return TheValue
Else
Return String.Empty
End If
Although I'd be inclined to return the state of whether the file was found and pass the acquired value back as a parameter.
Last edited by Bulldog; Aug 26th, 2009 at 01:37 PM.
-
Aug 26th, 2009, 01:37 PM
#3
Re: No value on all code paths
I'm not sure what PHPO is, but in VB you cannot return True, OR a value. You can return a Boolean (which is True or False), or any other type (Integer, String, your custom class, etc), but you cannot really mix them up.
The error is because the function does not return anything when the If statement is false. You could simply add an Else statement, and return something appropriate there. For example, you could return an empty string. If that's appropriate in your situation depends on what your situation is. If the Innertext could return an empty string, and if that would be perfectly valid, then that gets dangerous, because you cannot distinguish between an empty string due to the If statement being false, and an empty string that is valid.
-
Aug 26th, 2009, 01:47 PM
#4
Thread Starter
Hyperactive Member
Re: No value on all code paths
Hi Guys,
Sorry that was a type, it was meant to be PHP lol
That was probably a bad example to show i am still deciding what if anything i'm doing with that function lol, i understand what i'm doing now 
thanks for the input guys
Graham
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
|