Given a Ulongint I wanted to do a Knuth shuffle on the eight bytes. My first attempt was to use a Sub with a ByRef parameter, but that did not work - the shuffling failed. I tried a variety of themes but to no avail. I gave up and used a Function with a ByVal parameter and, obviously, a return value. That worked, but it didn't sit right with me having a value coming in via a parameter and going out via a return.
I went back to the Sub approach and eventually come up with this:
Code: Select all
Sub ShuffleUlongint( ByRef x As Ulongint )
Dim As Ulongint y = x
Dim As Byte Ptr z = Cast(Byte Ptr,@y)
For i As Long = 0 to 6
Swap z[i], z[xRange(i,7)]
Next
x = y
End Sub
What got the ByRef method to work was the two statements 'Dim As Ulongint y = x' and 'x = y'.
Effectively then we have switched from a ByRef environment to a ByVal environment, done some work and switched back to a ByRef environment.
At the moment, I not able to describe with any conviction why we have to do that.
Can anyone come up a clear explanation?