描述:
先定义一个二维数组map(x,y)
然后把map传入一个子过程(子函数也一样) test(ByVal arr(,) As Integer)
在子过程中对传入参数arr 进行任何的赋值、修改都会直接影响子过程外的map变量。
谁有解决办法啊?
环境:vs2013
类型:windows 窗口程序
测试框架:framework2 framework4
界面控件:textbox1、textbox2、button1
代码:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t As New test
Dim map(,) As Integer = t.str2map(Me.TextBox1.Text)
For x As Integer = 0 To map.GetUpperBound(0)
Dim line As String = ""
For y As Integer = 0 To map.GetUpperBound(1)
line = line & New Point(x, y).ToString & ":" & map(x, y) & ","
Dim map_temp(,) As Integer = map
t.test(map_temp)
Next
Me.TextBox2.AppendText(line & vbCrLf)
Next
End Sub
End Class
Class test
Public Function test(ByVal arr(,) As Integer) As Integer
For x As Integer = 0 To arr.GetUpperBound(0)
For y As Integer = 0 To arr.GetUpperBound(1)
arr(x, y) = 0
Next
Next
Return 0
End Function
Public Function str2map(ByVal str As String) As Integer(,)
Dim map(,) As Integer
Dim lines() As String = str.Trim.Split(vbCrLf)
Dim x, y As Integer
x = 0
y = lines.GetUpperBound(0)
ReDim Preserve map(y, x)
For y_index As Integer = 0 To y
Dim line As String = lines(y_index).Trim
Dim values() As String = line.Split(",")
x = values.GetUpperBound(0)
If x > map.GetUpperBound(1) Then ReDim Preserve map(y, x)
For x_index As Integer = 0 To x
If IsNumeric(values(x_index)) Then _
map(y_index, x_index) = values(x_index)
Next
Next
Return map
End Function
End Class
------解决思路----------------------
test.SetNewValue(map) <=只要调用了这个test方法,map的值就会改变. 但SetNewValue 里arr 没有使用valref
--------------
因为数组是个引用类型,所以给每个元素赋值不需要ref关键字
搜一搜:值类型,引用类型,值传递,引用传递
引用类型不改变自身的引用的前提下,使用值传递和使用引用传递是没有区别的
------解决思路----------------------
byref byval 对值 “值类型“有效,像int,bool,char,对”引用类型“是没有效果的
你那个int数字组属于“引用类型”,byval 跟byref效果是一样的
http://msdn.microsoft.com/zh-cn/library/t63sy5hs(VS.80).aspx