VB中变量的地址

时间:2012-10-09 17:46:58

标签: vb.net string-concatenation

这在C中很容易,我怎么能用VB做呢? 这就是我现在正在尝试的。

Dim a As String = "a"
Dim b As String = "b"
Dim c As String = "c"
Dim d As String = "d"

For Each i in {a, b, c, d}
    i = "blah" & i
End For

这不起作用,因为这只是修改i而不是基础变量。

我真正需要的是指针!?

2 个答案:

答案 0 :(得分:3)

VB的For Each循环不支持这样的构造。这很遗憾,但无论如何还有更好的方法。尽量避免循环:

Dim items = {a, b, c, d}.Select(Function (s) "blah" & s)

如果这不是有效的VB(将集合初始化程序与方法调用相结合......),以下方法确实有效:

Dim items = (New List(Of String)() From {a, b, c, d}).Select(Function (s) "blah" & s)

答案 1 :(得分:1)

这里,使用数组中变量的地址代替内存中变量的地址:

Dim a As String = "a"
Dim b As String = "b"
Dim c As String = "c"
Dim d As String = "d"
Dim items = {a,b,c,d}

For i As Integer = 0 To items.Length - 1
   items(i) = "blah" & items(i)