The strange array assignment of VB.NET

xiaoxiao2021-03-06  113

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 is not 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 (TEST i) (0) & "|" & test (i)) NEXTEND SUB runs above code You can find that the results of five display are the same ... If you think the same value when it is Random, then You can use a step-by-step debugger. In the Auto window, you will find every time the cycle, the value of TEST (i-1) is always changed with Test (i) .... This also shows me The problem mentioned above, that is, the array assignment to the array is "address" instead "to save .... But if it is not an array, there will be no such situation! Solution is to put randomtext (1) AS String defines to the inside of the first loop, that is, generate a new array every loop once

Let's 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) (0) & "|" & Test (i) (1) & "
" Next%> You will find the 5 lines of data after running !!! This is "transmitted value" in the ASP number of groups. Not "inlet" save !!!!

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

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

New Post(0)