The strange array assignment of VB.NET

xiaoxiao2021-03-06  117

Today, I found a strange phenomenon in .NET in the process of writing, it is to give arrays to array to arouse the argument. The address is "address", that is, the array does not generate a new value when it is assigned to array, and saves the array. Instead, it is transmitted to the address to the array. Take a bit: there are array A and array B. Now assign array b to array A. Time array A is not a value of array B but saves array B's memory address. This creates a problem: if the value of the array B changes, the value of the array A is also changed !!! .... and the ASP is not the case, VB may not this, my machine doesn't have VB. So test Can't ...

This may not be very clear (my expression ability is very poor), give a sample code:

VB.NET code:

SUB TestCode ()

Dim test (4) as array, randomtext (1) AS STRING

DIM RAN As New Random, I, J AS Integer

For i = 0 TO 4

For j = 0 to 1

Randomtext (j) = ran.next (100)

NEXT

Test (i) = randomtext

NEXT

For i = 0 TO 4

MSGBOX (TEST (I) (0) & "| & test (i) (1))

NEXT

End Sub

Run the above code You can find that the results of five display are the same ... If you have the same value as you want Random, you can use a step-by-step debugger. In the Auto window, you will find whenever a loop The value inside TEST (I-1) is always following the value of Test (i) .... This also shows that I have said that the problem mentioned above, that is, the array assignment to the array is "inventory" instead of " Path value "Save .... But if it is not an array, there will be no such situation! Solution is to put the definition of randomtext (1) AS String in the first loop, that is, one cycle New array

Let us see if it will be the same in the ASP.

ASP code:

<% @ Language = "VBScript"%>

<%

Randomize

DIM TEST (4), Randomtext (1)

Dim Ran, i, j

For i = 0 TO 4

For j = 0 to 1

Randomtext (j) = int (RND (TIME) * 100 1)

NEXT

Test (i) = randomtext

NEXT

For i = 0 TO 4

Response.Write Test (i) & "|" & test (i) (1) & "
"

NEXT

%>

After running, 5 lines of data are different !!! That is, in the ASP count group assignment to array is "passing value" instead of "to address" save !!!!

How many things have changed ?? Many things have changed ... become accustomed to it or not used to it?

转载请注明原文地址:https://www.9cbs.com/read-98255.html

New Post(0)