PDA

View Full Version : ارسال آرایه ها با مقدار و با مراجعه



mohandess_anita
چهارشنبه 27 مهر 1384, 12:16 عصر
سلام
من یک مثال از تو کتاب نوشتم ولی مفهوم Sub seconddouble رو نفهمیدم .میشه برام توضیح بدین.ممنون می شم.
برنامشو به این شکله:
Module Module1

Sub main()
Dim i As Integer
Dim firstarray As Integer()
Dim firstarraycopy As Integer()
firstarray = New Integer() {1, 2, 3}
firstarraycopy = firstarray
Console.WriteLine("test passing array refrence using byval.")
Console.Write("contents of firstarray before calling first double:")
For i = 0 To firstarray.GetUpperBound(0)
Console.Write(firstarray(i) & " ")
Next
firstdouble(firstarray)
Console.Write(vbCrLf & "contents of firstarray after calling firstdouble:")
For i = 0 To firstarray.GetUpperBound(0)
Console.Write(firstarray(i) & " ")
Next
Console.Write(vbCrLf)
For i = 0 To firstarraycopy.GetUpperBound(0)
Console.Write(firstarraycopy(i) & " ")
Next
If firstarray Is firstarraycopy Then
Console.WriteLine(vbCrLf & "the refrences are equal.")
Else
Console.WriteLine(vbCrLf & "the refrences are not equal.")
End If
Dim secondarray As Integer()
Dim secondarraycopy As Integer()
secondarray = New Integer() {1, 2, 3}
secondarraycopy = secondarray
Console.WriteLine(vbCrLf & "test passing array refrence using byref.")
Console.Write("contents of secondarray before calling seconddouble:")
For i = 0 To secondarray.GetUpperBound(0)
Console.Write(secondarray(i) & " ")
Next
seconddouble(secondarray)
Console.Write(vbCrLf & "contents of secondarray after calling seconddouble:")
For i = 0 To secondarray.GetUpperBound(0)
Console.Write(secondarray(i) & " ")
Next
Console.Write(vbCrLf)
For i = 0 To secondarraycopy.GetUpperBound(0)
Console.Write(secondarraycopy(i) & " ")
Next
If secondarray Is secondarraycopy Then
Console.WriteLine(vbCrLf & "the refrences are equal.")
Else
Console.WriteLine(vbCrLf & "the refrences are not equal.")
End If
Console.ReadLine()
End Sub
Sub firstdouble(ByVal array As Integer())
Dim i As Integer
For i = 0 To array.GetUpperBound(0)
array(i) *= 2
Next
array = New Integer() {11, 12, 13}
End Sub

Sub seconddouble(ByRef array As Integer())
Dim i As Integer
For i = 0 To array.GetUpperBound(0)
array(i) *= 2
Next
array = New Integer() {11, 12, 13}
End Sub
End Module
نمی دونم چرا در پروسیجر دوم چرا secondarray مقدار 11و12و13 رو می گیره ولی secondarraycopy همان مقدار 2و4و6 رو مگیره.
خروجی برنامه:

test passing array refrence using byval.
contents of firstarray before calling first double:1,2,3
contents of firstarray after calling firstdouble:2,4,6
the refrences are equal.
test passing array refrence using byref.
contents of secondarray before calling seconddouble:1,2,3
contents of secondarray after calling seconddouble:11,12,13
the refrences are not equal.

mohandess_anita
پنج شنبه 28 مهر 1384, 09:40 صبح
سلام
لطفا جواب سؤال منو زودتر بدین.
ممنون میشم.