加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 22:35:31 所属栏目:大数据 来源:网络整理
导读:我调用一个函数返回一个字符串,但它实际上只返回字符串的第一个字符,它应该返回。 以下是重新创建我遇到的问题的示例代码段: Public Function GetSomeStringValue(Value as Integer) As String ... Code Goes here Return Some_Multicharacter_stringEnd
我调用一个函数返回一个字符串,但它实际上只返回字符串的第一个字符,它应该返回。

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

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()

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读