我正在调用一个返回字符串的函数,但它实际上只返回它应该返回的字符串的第一个字符。
这是一个示例代码,用于重新创建我遇到的问题:
Public Function GetSomeStringValue(Value as Integer) As String
... Code Goes here
Return Some_Multicharacter_string
End Function
函数调用如下所示:
SomeStringValue = GetSomeStringValue(Value)
为什么这不返回整个字符串?
最佳答案
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.
在尝试了一百种不同的东西,重构了我的代码,在调试器中多次单步执行代码,甚至让一个同事调查了问题之后,我终于灵机一动,找到了答案。
在重构代码的某个时候,我更改了函数以摆脱 Value 参数,将其保留如下:
Public Function GetSomeStringValue() As String
... Code Goes here
Return Some_Multicharacter_String
End Function
但是,我忽略了删除调用函数时传入的参数:
SomeStringValue = GetSomeStringValue(Value)
编译器没有提示,因为它将我所做的解释为调用不带括号的函数,这是 VB6 时代的遗留功能。然后,Value 参数转换为从函数返回的字符串(又名字符数组)的数组索引。
所以我删除了参数,一切正常:
SomeStringValue = GetSomeStringValue()
我发布这个是为了让其他人在遇到问题时/如果他们遇到问题时能够识别出来,并且能够比我更快地解决它。我花了相当长的时间解决,我希望我可以节省其他人一些时间。
关于vb.net - 为什么返回字符串的 VB.Net 函数实际上只返回一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/142697/