Vb. Strange array assignment phenomenon in net

Source: Internet
Author: User
The array was discovered today in the process of writing a program. NET is a strange phenomenon, that is, to the array to assign value to the array is actually "point to the address of the array", that is, the array assignment to the array is not generated after a new value to the array to save, but instead to the array address to the array. For example: There are arrays A and B. Now assign array b to array a. While array A does not hold the value of array B but instead saves the memory address of array B, this creates a problem: if the value of array b changes, then the value of array A is also changed!!! .... And in the ASP is not the case, VB may not be so, my machine does not have VB. So the test is not ...

This may not be clear (my expressive ability is very poor), give a sample code to know:
Vb. NET's 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 five times the results of the display are the same ... If you think that the random produces the same value, then you can use one step at a time. In the Auto window you will find that the value in test (i-1) always changes with the value of test (i) whenever you loop. This also explains the problem I mentioned above that the array assignment to the array is "address" instead of "pass value" save .... But if it's not an array, it's not going to happen! The workaround is to place the definition of Randomtext (1) As String inside the first loop, which means that a new array is generated once per loop

Let's see if the ASP will be the same situation.
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) & "<br>"
Next
%>
After you run, you'll find that 5 rows of data are different!!! That is, in the ASP, the array is assigned to the array as "pass value" instead of "address" save!!!!


. NET how has changed?? A lot of things have changed ... Are you getting used to it or aren't you getting used to it?



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.