Reputation: 27609
I have some code I inherited which has a lot of warnings that I would like to get removed. Many are of the form Property '<propertyname>' doesn't return a value on all code paths
. I know why it is saying this, I'm just trying to determine what the correct 0 impact way of fixing this is.
The original code is this:
Public ReadOnly Property LevelName() As String Implements IMember.LevelName
Get
End Get
End Property
My first thought was that I could just put a Return Nothing
statement in there but I notice if I decompile it in Reflector it gives me this:
Public ReadOnly Property LevelName As String
Get
Dim LevelName As String
Return LevelName
End Get
End Property
Unfortuantely if I put this in my code (having changed the local variable name to avoid collisions) it then complains that it is being used before it has been assigned a value.
Try to analyze IL in LinqPad (not sure if this is a good test or not) reveals that the empty method is the same as the above that Reflector gave me but it is different from return Nothing
and return ""
.
What can I use to get rid of this warning while guaranteeing the code runs 100% the same?
Edit: I thought I'd include the IL that LinqPad is giving me for the various situations:
Empty get:
IL_0000: ldloc.0
IL_0001: ret
Explicit empty string returned:
IL_0000: ldstr ""
IL_0005: ret
Explicit Nothing Returned:
IL_0000: ldnull
IL_0001: ret
I'm not sure what ldloc.0
does here. I'm wondering if it is trying to get something from a register which will be null and if that is therefore the same as ldnull in this situation but I havent' ever really studied IL much before...
Upvotes: 0
Views: 180
Reputation: 6155
The ldloc.0
instruction is loading a local variable that is then returned in the next statement. The code, being empty, doesn't declare any variables, so you may wonder what this is. This phantom local is the variable that VB automatically creates with the same name as the function or property. If you have a branch without a return, it returns this variable for you. This is mostly a legacy/compatibility feature by now as most people will use the Return
statement and make their own variables.
Since this variable is just defined and returned without ever being assigned to, you can see that Oliver's comment to his answer that VB will return the default value for the type is correct. Return Nothing
is the best match for this case. The IL may be different, but the behavior will be the same. (The machine code may come out the same, but I don't try to guess at what the JITter does.)
Upvotes: 1
Reputation: 112372
You must have a return statement! (Even if VB allows you to leave it out, my feeling as programmer says to me that one must belong here.)
Public ReadOnly Property LevelName() As String Implements IMember.LevelName
Get
Return Nothing
End Get
End Property
Or
Public ReadOnly Property LevelName() As String Implements IMember.LevelName
Get
Return ""
End Get
End Property
The code decompiled from IL does not always work, because in IL you can do things that are not possible in VB. In general, high-level languages like VB or c# are much more restrictive than low-level languages like assembler or IL (which is a kind of assembler).
Upvotes: 1