vb.net - 为什么返回字符串的 VB.Net 函数实际上只返回一个字符?

标签 vb.net syntax

我正在调用一个返回字符串的函数,但它实际上只返回它应该返回的字符串的第一个字符。

这是一个示例代码,用于重新创建我遇到的问题:

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/

相关文章:

c# - 英文日期差异

vb.net - 使用 Is 运算符比较循环中的字符串

换行符后带有三个点的 python 字符串...这是什么东西?

html - 如果输入是焦点,那么如何更改 Sass 中不同类的属性?

c - 为什么这段代码总是给出这个错误?语法错误: Bad for loop variable

vb.net - 使用 .Split 删除空条目

asp.net - 异常消息: The process cannot access the file because it is being used by another process

c - 问题理解 C 中的 block

c - 为什么在函数定义之外声明的函数变量不会引发错误?

c# - 我只想让我的客户向我的服务器打个招呼,但不能