为什么返回字符串的VB.Net函数只返回一个字符?

前端之家收集整理的这篇文章主要介绍了为什么返回字符串的VB.Net函数只返回一个字符?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
调用一个函数返回一个字符串,但它实际上只返回字符串的第一个字符,它应该返回。

以下是重新创建我遇到的问题的示例代码段:

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参数转换为从函数返回的字符串(aka字符数组)的数组索引。

所以我删除了参数,一切工作正常:

SomeStringValue = GetSomeStringValue()

我发布这个,以便其他人会认识到问题,当他/她/他们遇到它,并能够比我更快地解决它。我花了很长时间来解决,我希望我可以节省一些时间。

原文链接:https://www.f2er.com/vb/256341.html

猜你在找的VB相关文章