Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all,
Could someone tell me why I am getting this warning and how to solve it.
Code works fine but I would like to know how to avoid warnings like this in the future.

VB
Public Function GetPropertyValue(objBlockRef As AcadBlockReference, propName As String) As Object
        Dim iProp As Long
        Dim dybprop As Object

        If objBlockRef.IsDynamicBlock Then '<--| if the passed block reference is a dynamic block one
            dybprop = objBlockRef.GetDynamicBlockProperties '<--| get its dynamic properties
            For iProp = LBound(dybprop) To UBound(dybprop) '<--| iterate through them properties
                With dybprop(iProp)
                    If .PropertyName = propName Then '<--| if the current property matches the passed name
                        GetPropertyValue = .Value '<--| return its value
                        Exit Function
                    End If
                End With
            Next
        End If

    End Function


What I have tried:

I don't have any idea. I have tried to use "return" on the name of the function.
Posted
Updated 25-Nov-17 15:23pm
v2

1 solution

You don't have a single Return statement anywhere. This isn't VB6 or VBScript.

An actual Return statement has to be in there, and in an appropriate place. If you replace the GetPropertyValue = .Value with the Return statement, you'll get another error, "Not all code paths return a value."

So, rewrite the function
VB.NET
Public Function GetPropertyValue(objBlockRef As AcadBlockReference, propName As String) As Object
    Dim iProp As Long
    Dim dybprop As Object
    Dim returnValue As Object = Nothing

    If objBlockRef.IsDynamicBlock Then '<--| if the passed block reference is a dynamic block one
        dybprop = objBlockRef.GetDynamicBlockProperties '<--| get its dynamic properties
        For iProp = LBound(dybprop) To UBound(dybprop) '<--| iterate through them 
properties
            ' The With was completely unnecessary.
            If dybprop(iProp).PropertyName = propName Then '<--| if the current property matches the passed name
                returnValue = dybprop(iProp).Value '<--| return its value
                Exit For
            End If
        Next
    End If

    Return returnValue
End Function
 
Share this answer
 
v2
Comments
blueye89 26-Nov-17 16:30pm    
Warning BC42104 Variable 'returnValue' is used before it has been assigned a value. A null reference exception could result at runtime.
Dave Kreskowiak 26-Nov-17 17:09pm    
That Dim returnValue As Object should have read Dim returnValue As Object = Nothing.
blueye89 26-Nov-17 17:13pm    
Solved! Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900