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

后端 未结 1 927
南旧
南旧 2021-02-06 23:07

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

1条回答
  •  滥情空心
    2021-02-06 23:33

    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.

    0 讨论(0)
提交回复
热议问题