contactmatt
contactmatt

Reputation: 18600

Function assigns a value to itself

The following function assigns a value to itself instead of using the Return keyword to return a value from the function.

Public Function GetComponentDescription(ByVal partNumber As Long, ByVal paintTypeId As String) As String
    Dim componentDescription As String = String.Empty

    ' ...

    GetComponentDescription = componentDescription
End Function

Apparently this is valid syntax (I'm guessing it may be VB6 related, since this code was ported from VB6?).

Question: What is this line of code doing? Is it behaving the same as the Return keyword?

Upvotes: 1

Views: 65

Answers (3)

King of kings
King of kings

Reputation: 695

Yes. But it will return an empty string

Upvotes: 0

Dave Doknjas
Dave Doknjas

Reputation: 6542

Is it behaving the same as the Return keyword?

No. It sets a value to be returned whenever the function exits via an Exit Function or End Function, while 'Return' exits immediately with the value specified in the Return statement. You can even combine the "function name assignment" approach and 'Return' to make code that's even harder to understand.

Upvotes: 3

Blorgbeard
Blorgbeard

Reputation: 103467

Is it behaving the same as the Return keyword?

Yes.

I see it a lot in VB.net code ported from VB6 as well.

Here is an MSDN reference that describes it.

Upvotes: 1

Related Questions