Hi again
i have a strange issue today. Sample code is at the below of the post. The code that i have pointed out is too strange. According to my knowledge using return statement the functions return values as byval and copies the value to the stack. Also how can my class can reach outscope elements and change them. Take a look at to the code below. How this could be?
code Code:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim b As New ByteClass Dim sumArray As Long = 0 Dim ByteArray As Byte() = {} Dim idx As Integer = 0 ByteArray = b.GetArray sumArray = SumByteArray(ByteArray) Do idx += 1 If sumArray <> SumByteArray(ByteArray) Then Exit Do Debug.Print("Not changed yet" + "" + Str(idx)) Loop MsgBox("The ByteArray has changed???") End Sub Public Function SumByteArray(ByVal pByteArray As Byte()) As Long 'This function return the sum value of the ByteArray Dim retval As Long = 0 For i As Integer = 0 To pByteArray.Length - 1 retval = retval + pByteArray(i) Next Return retval End Function End Class Class ByteClass Dim ByteArray As Byte() = {} Dim Foo As System.Threading.Thread Sub New() ByteArray = New Byte(4096) {} Foo = New System.Threading.Thread(AddressOf ThreadWork) Foo.Start() End Sub Public Function GetArray() As Byte() Return ByteArray End Function Sub ThreadWork() 'Generate a random mutation on bytestream Dim r1 As New Random Dim r2 As New Random Dim idx As Integer Dim v As Integer = 0 Do v = r1.Next(0, 255 + 1) idx = r2.Next(0, 4096 + 1) ByteArray(idx) = v Application.DoEvents() Loop End Sub End Class




Reply With Quote