简体   繁体   English

从另一个子调用字符串

[英]Calling string from another sub

I want to use a string that I declared previously into a function (there's a reason why I want it to be a function). 我想使用之前在函数中声明的字符串(这是我希望将其作为函数的原因)。 But the function isn't recognising the string, my question is how I could modify the code below to make it work? 但是函数无法识别字符串,我的问题是如何修改下面的代码以使其起作用? Any help is appreciated. 任何帮助表示赞赏。 Thank you in advance. 先感谢您。

Sub encryption

Dim text As String = wraper.DecryptData(ciphertext)

End sub

and... 和...

Public Function CallResults() As String

Dim instructions As String = text '//'text' isn't being recognised
Return instructions

End function

Declare the variable text at class-level: 在类级别声明变量text

Dim text As String

Sub encryption

   text = wraper.DecryptData(ciphertext)

End sub

Public Function CallResults() As String

   Dim instructions As String = text
   Return instructions

End function

You can also add a Module to the project that has the text variable declared in it (variable is global to the project. 您也可以向其中添加了声明的文本变量的模块添加到项目中(变量是项目的全局变量。

However it would be better design to have the function accept a variable that it can act upon: 但是,让函数接受可以作用的变量会是更好的设计:

Public Function CallResults(Dim text As String)

   Dim instructions As String = text
   Return instructions

End function
Public Function CallResults() As String

Dim instructions As String = encryption '//'text' isn't being recognised
Return instructions

End function

would do it. 会做到的。 @Ahmed Salman Tahir's answer would be more efficient if you are getting it more than once as it would cache the result of encryption. 如果您不止一次获得@Ahmed Salman Tahir的答案,它将更有效率,因为它将缓存加密结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM