Why does a VB.Net function that returns string only actually return a single character?

StackOverflow https://stackoverflow.com/questions/142697

  •  02-07-2019
  •  | 
  •  

Question

I'm calling a function that returns a string, but it's only actually returning the first character of the string it's supposed to be returning.

Here's a sample piece of code to recreate the issue I'm experiencing:

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

The function call looks like:

SomeStringValue = GetSomeStringValue(Value)

Why is this not returning the entire string?

Was it helpful?

Solution

Note: this answer was originally written by the OP, Kibbee, as a self-answer. However, it was written in the body of the question, not as an actual separate answer. Since the OP has refused repeated requests by other users, including a moderator, to repost in accordance with site rules, I'm reposting it myself.

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.

At some point when I was refactoring the code, I 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:

SomeStringValue = GetSomeStringValue(Value)

The compiler didn't complain because it interpreted what I was doing as calling the function without brackets, which is a legacy feature from the VB6 days. Then, 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:

SomeStringValue = GetSomeStringValue()

I'm posting this so that other people will recognize the problem when/if they ever encounter it, and are able to solve it much more quickly than I did. It took quite a while for me to solve, and I hope I can save others some time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top