Math
Math

Reputation: 389

how to detect if the function need to return a value

I would like to know if it is possible to detect if a function need to return a value. Example :

dim lLayer as layer

lLayer = CreateLayer("Test")
CreateLayer("Test2")

private function CreateLayer(LayerName as string) as layer
    [...]
    if functionNeedReturn then
        return selectLayer(LayerName)
    else
        return nothing
    end if
end function

The first call of CreateLayer should return a value (so the if is true) and the second one should return nothing.

Is it possible to do something like this? Maybe with the system.reflection namespace...

Upvotes: 2

Views: 89

Answers (2)

Forgive me if I've misunderstood your question. In vb.net there is a static keyword which "specifies that one or more declared local variables are to continue to exist and retain their latest values after termination of the procedure in which they are declared"

So if one is only allowed to create one instance of layer, through CreateLayer, one could do it as following:

Private Function CreateLayer(name As String, Optional ByRef returnValueIsNull As Boolean = False) As Layer

    Static isCreated As Boolean

    If (isCreated) Then
        returnValueIsNull = True
        Return Nothing
    Else
        '[...]
        isCreated = True
        returnValueIsNull = False
        Return SelectLayer(name)
    End If

End Function

'Or reversed:

Private Function TryCreateLayer(name As String, Optional ByRef result As Layer = Nothing) As Boolean

    Static isCreated As Boolean

    If (Not isCreated) Then
        '[...]
        isCreated = True
        result = SelectLayer(name)
        Return True
    End If

    Return False

End Function

Now, if one is allowed to create multiple layers constrained by name, one could do it as following:

Private Function CreateLayer(name As String, Optional ByRef created As Boolean = False) As Layer

    Static cache As Dictionary(Of String, Layer)

    If (cache Is Nothing) Then
        cache = New Dictionary(Of String, Layer)
    End If

    Dim item As Layer = Nothing

    If (cache.TryGetValue(name, item)) Then
        created = False
    Else
        '[...]
        created = True
        item = SelectLayer(name)
        cache.Add(name, item)
        Return SelectLayer(name)
    End If

    Return item

End Function

'Or reversed:

Private Function TryCreateLayer(name As String, Optional ByRef result As Layer = Nothing) As Boolean

    Static cache As Dictionary(Of String, Layer)

    If (cache Is Nothing) Then
        cache = New Dictionary(Of String, Layer)
    End If

    If (Not cache.TryGetValue(name, result)) Then
        '[...]
        result = SelectLayer(name)
        cache.Add(name, result)
        Return True
    End If

    Return False

End Function

Upvotes: 0

Crono
Crono

Reputation: 10478

You have to create another function / property that determines whether CreateLayer will return something. It will then be up to the caller to call that first, unless he don't care receiving a null value.

However since your method is private I'm having a hard time figuring out what you're trying to protect yourself from, here.

Upvotes: 1

Related Questions