有没有办法给多个变量赋值(在这种情况下是整数),而不是一次全部?
例如,我有Dim aceVal, twoVal, threeVal, fourVal, fiveVal, sixVal, sevenVal, eightVal, nineVal, tenVal As Integer
,并且在待列表框选择之前,我想将三个值分别设置为四个值和六个值。
我该怎么做?感谢。
答案 0 :(得分:5)
我真的不是要成为一个混蛋,但是你有什么理由不能这样做:
threeVal = -1
fourVal = -1
sixVal = -1
答案 1 :(得分:5)
在vb.net中无法将它们全部分配到一行,如threeVal = fourVal = SixVal。
如果你需要所有这些变量,那么一次只有一行。我建议如果-1将被大量使用,那么为它创建一个常量并将var赋值给常量。
如果可能的话,我还会考虑使用数组或集合存储所有值,并使用它们来设置/检索它们的值。然后,您的代码中不必包含60个变量。数组和/或集合会更清晰。
答案 2 :(得分:4)
您可以像这样使用数组/字典:
Dictionary myValues = new Dictionary(); myValues.Add("FirstVal", 1); myValues.Add("SecondVal", -1); myValues.Add("ThirdVal", 1);
然后你可以编写一个简单的函数:
public updateMyValues(string[] myKeys, int myValue) { foreach (string s in myKeys) { myValues[s] = myValue; } }
最后当你的列表框改变时,你可以调用函数来更新你想要的变量:
upDateMyValues({"FirstVal", "ThirdVal"}, -1);
希望这有帮助。
*编辑:我知道它在C#中,但它很容易移植到VB。
答案 3 :(得分:4)
如果你想把它保持在一行,你就可以这样做
Dim m1 As String = "false", m2 As String = "false", m3 As String = "false"
'etc..
答案 4 :(得分:2)
您可以使用构造函数声明和赋值:
Dim str1, str2, str3, str4 As New String("asdf")
Dim int1, int2, int3, int4 As New Integer
答案 5 :(得分:0)
我知道这是一个老线程,但我自己也遇到了类似的问题 - 这就是我做的方式(我只处理4个值
Private Sub SetFalse(ByRef first As Boolean, Optional ByRef second As Boolean = False, Optional ByRef third As Boolean = False, Optional ByRef fourth As Boolean = False)
first = False
second = False
third = False
fourth = False
End Sub
这可以通过使第一个变量成为所需的值来轻松调整(这段代码也看起来有点愚蠢,因为我必须提供一个只能是真或假的默认值,但很明显有整数或者看起来更有意义的东西。)
答案 6 :(得分:0)
您可以尝试这样:
Dim aceVal, twoVal,fourVal,sevenVal, eightVal, nineVal, tenVal As Integer
Dim threeVal As Integer =-1,fiveVal=-1,sixVal=-1
或者点击这篇文章:http://www.informit.com/library/content.aspx?b=Net_2003_21days&seqNum=95
答案 7 :(得分:0)
您可以将其放入List并使用For Each方法 这是代码
Dim aceVal, twoVal, threeVal, fourVal, fiveVal, sixVal, sevenVal, eightVal, nineVal, tenVal As Integer
Dim var As New List(Of Integer) From {threeVal, fiveVal, sixVal} 'you can add more variable here
For Each n As Integer In var
n = -1
Next
答案 8 :(得分:0)
我认为多重赋值功能 所以 很酷 C#我写了 VB 扩展方法( Assign
)做同样的事情。语义很容易理解;您只需在任何值上调用Assign
即可将其分配给多个其他变量
即。
Call True.Assign(b1, b2, b3)
Call 4.1.Assign(d1, d2, d3)
等...
以下是代码:
Imports System.Runtime.CompilerServices
Namespace Utility
Public Module MultiAssignExtensionMethod
' Multiply assign the same value to 1 (required) or more variables
<Extension()> _
Public Function Assign(Of T)(this As T, ByRef first As T, Optional ByRef second As T = Nothing, Optional ByRef third As T = Nothing,
Optional ByRef forth As T = Nothing, Optional ByRef fifth As T = Nothing, Optional ByRef sixth As T = Nothing,
Optional ByRef seventh As T = Nothing, Optional ByRef eighth As T = Nothing, Optional ByRef nineth As T = Nothing,
Optional ByRef tenth As T = Nothing) As T
' I would LIKE to do this as a ParamArray, but it doesn't allow references for basic types....grrr
first = this
second = this
third = this
forth = this
fifth = this
sixth = this
seventh = this
eighth = this
nineth = this
tenth = this
Return this ' For use as assignment and evaluation as Function parameter
End Function
End Module
End Namespace