Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the strange assignment phenomenon of VB.NET Array

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you the content of an example analysis of the strange assignment of VB.NET arrays. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In the process of writing the program, we found a strange phenomenon when assigning values to the VB.NET array, that is, when assigning values to the array, it unexpectedly "points to the address of the array", that is, when the array assigns values to the array, it does not generate a new value and pass it to the array for preservation, but to the address of the array to the array.

For example: there are array An and array B. Now assign array B to array A. Then array A saves the memory address of array B instead of the value of array B, which gives rise to a problem: if the value of array B changes, so does the value of array A. This is not the case in ASP, and this may not be the case in VB. My computer does not have VB. So I can't test it.

This may not be clear (my expressive ability is very poor), as shown in the sample code that assigns a value to a VB.NET array:

The code for VB.NET array assignment:

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

If you run the above code, you will find that the result shown five times is the same. If you think RANDOM produces the same value, you can use step-by-step debugging. In the automatic window, you will find that the value in Test (iMel 1) always changes with the value of Test (I) whenever you loop. This also illustrates the problem I mentioned above, that is, when an array is assigned to an array, it is saved by "passing address" rather than "passing value". But this wouldn't happen if it wasn't an array! The solution is to put the definition of RandomText (1) As String in * loops, that is, to generate a new array each time.

Let's see if it is the same as the VB.NET array assignment in 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) & "< br>

"

Next

% >

After running, you will find that 5 rows of data are different! That is, in ASP, the array assignment to the array is "passing value" rather than "passing address" save!

Thank you for reading! This is the end of the article on "example analysis of strange assignment of VB.NET array". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report