views:

3598

answers:

2

The other day I was working on project, and ran into a weird bug. I called a function, and it was only returning the first character from the string which it should have been returning. After trying a hundred different things, refactoring my code, stepping through the code in the debugger many times, and even having a co-worker look into the problem, I finally, in a flash of genius, discovered the answer. Here's a sample piece of code to recreate the issue I was experiencing.

Public Function GetSomeStringValue(Value as Integer) As String
    ... Code Goes here
    Return Some_Multicharacter_string
End Function

And the function call looked like

SomeStringValue = GetSomeStringValue(Value)

At some point when I was refacting the code, and changed the function to get rid of the Value parameter, leaving it as follows

Public Function GetSomeStringValue() As String
    ... Code Goes here
    Return Some_Multicharacter_String
End Function

However, I neglected to remove the parameter that I was passing in when calling the function. The compiler didn't complain because it interpreted what I was actually doing was calling the function without brackets, as is a legacy feature from the VB6 days. And the Value parameter transformed into the array index of the string (aka character array) that was returned from the function.

So I removed the parameter, and everything worked fine. I'm posting this in hopes people will read it, and recognize the problem when if they ever encounter it, and are able to solve it much more quickly than I am. It took quite a while for me to solve, and I hope I can save others some time.

+1  A: 

Just to get the question from the 'unanswered' list:

The compiler didn't complain because it interpreted what the code was actually calling the function without brackets, as is a legacy feature from the VB6 days. And the Value parameter transformed into the array index of the string (aka character array) that was returned from the function.

Ilya Kochetov
+1  A: 

I have to say this is pretty interesting. I've also run into the problem of being able to compile functions that should return something but don't. This annoys me no end

lomaxx