vb.net - vb.net 从函数调用特定字符串
问题描述
我有这个代码:
Function f1(ByVal x As String, ByVal o As String)
x = "me"
o = "you"
Return {x.ToString, o.ToString}
End Function
Function f2()
Dim a As String = "i love" & f1(Nothing, ToString)
End Function
我正在尝试从函数 f1() 中获取字符串 (o) 以在 f2() 中使用它但不工作。
解决方案
您应该打开 Option Strict On,因为该示例存在很多错误。
Function f1(ByVal x As String, ByVal o As String) As String() ' x and o are useless parameters
x = "me"
o = "you"
Return {x.ToString, o.ToString} ' Returns an array of string
End Function
Function f2() ' Method not returning anything
Dim a As String = "i love" & f1(Nothing, Me.ToString()).ToString() ' Can't really concatenate an array to a string. Also, parameter Me.ToString() doesn't make much sense
End Function
我建议你从像这样更简单的东西开始。
Function f1() As String
Dim o As String = "you"
Return o
End Function
Function f2() As String
Dim a As String = "I love " & f1()
Return a
End Function
然后添加一个参数来选择不同的返回值
Function f1(ByVal mustReturnX As Boolean) As String
Dim x As String = "me"
Dim o As String = "you"
If mustReturnX Then
Return x
End If
Return o
End Function
Function f2() As String
Dim a As String = "i love " & f1(True)
Return a
End Function
没有明确的要求,我们无能为力。
推荐阅读
- php - 使用存储过程在 Datatable 中显示数据时出现问题
- flutter - Flutter - 在进程之间停止 FutureBuilder 操作
- node.js - 我可以在没有任何 GUI 的情况下在 Raspberry pi 上运行 Chromium 中的本地托管网站吗?(仅限 CLI)
- r - 如何将 data.frame 中的列从列表转换为向量?
- javascript - 为什么我的 Google 图表无法在 Safari 上运行?
- reactjs - 如何在功能组件中映射设置器?
- c# - 当涉及外键时,无法邮递员 POST 到 dot net API c#
- javascript - firebase-messaging-sw.js 文件中用于 firebase 应用程序初始化的动态凭据
- java - 如何使用 Spring Data Redis 2.x 通过 Spring Boot 从 redis 缓存中获取所有密钥?
- python - 使用 Python 和 Kivy 的小型纸牌游戏