473,796 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An odd array feature in vb.net

I have two independent arrays and mysteriously they occupy the same
space. Is this an error or a feature?

Dim tokens$() '(input list of msn's and
constants)
Dim values() As Object '(tokens with msn's replaced by
values)

temp="A = 2 * B"

'---parse the temp expression into tokens
tokens = Split(temp, " ")

'---setup a similar sized values array
ReDim values(UBound(t okens))
values=tokens

and under vb.net changing tokens(4) will now change values(4)!!.

I expected that making values = tokens would give me an array "copy by
element". In fact it made the two occupy the same array space. Is
this a bug or a language feature??

Nov 18 '08 #1
5 2057

"barcrofter " <jd*****@gmail. comwrote in message
news:5d******** *************** ***********@a3g 2000prm.googleg roups.com...
>I have two independent arrays and mysteriously they occupy the same
space. Is this an error or a feature?

Dim tokens$() '(input list of msn's and
constants)
Dim values() As Object '(tokens with msn's replaced by
values)

temp="A = 2 * B"

'---parse the temp expression into tokens
tokens = Split(temp, " ")

'---setup a similar sized values array
ReDim values(UBound(t okens))
values=tokens

and under vb.net changing tokens(4) will now change values(4)!!.

I expected that making values = tokens would give me an array "copy by
element". In fact it made the two occupy the same array space. Is
this a bug or a language feature??
First a couple of comments:

Since you are using Dot.Net I would suggest using the new syntax so:
dim Tokens() as string

Ensure that you have options strict and explicit set to on. This will cause
you a bit of hassle at first but will pay big dividends when debugging your
app. You will then have to declare the temp variable.

As for you question you are setting a reference to an array to another array
so they will both point to the same array. What you might want to do is use
one of the Copy methods to bring the items from one array to the other.

Also if you are not looking to export this array to unmanaged code I would
suggest looking into another collection type for example a List(of T). This
will allow you to manipulate the List without having to redim all the time.

LS
Nov 18 '08 #2
On 2008-11-18, barcrofter <jd*****@gmail. comwrote:
I have two independent arrays and mysteriously they occupy the same
space. Is this an error or a feature?

Dim tokens$() '(input list of msn's and
constants)
Dim values() As Object '(tokens with msn's replaced by
values)

temp="A = 2 * B"

'---parse the temp expression into tokens
tokens = Split(temp, " ")

'---setup a similar sized values array
ReDim values(UBound(t okens))
values=tokens

and under vb.net changing tokens(4) will now change values(4)!!.

I expected that making values = tokens would give me an array "copy by
element". In fact it made the two occupy the same array space. Is
this a bug or a language feature??
Arrays are reference types - so nothing is copied, except the reference. If
you want a copy, then you need to do something like this:

redim values(ubound(t okens))
tokens.copyto(v alue, 0)

Though, be aware that if the array contains reference types, then all that is
copied in each element is the reference... so changing properties on an
object in one array will be reflected in the other.

--
Tom Shelton
Nov 18 '08 #3
"barcrofter " <jd*****@gmail. comschrieb
ReDim values(UBound(t okens))
values=tokens

and under vb.net changing tokens(4) will now change values(4)!!.

I expected that making values = tokens would give me an array "copy by
element". In fact it made the two occupy the same array space. Is
this a bug or a language feature??

Lookup "reference types" in the documentation (in opposite to value types).
An array is a reference type. You've just copied a reference. 'values'
references the same array as 'tokens'. You can ommit the "Redim" I quoted
because it's overwritten in the next line "values=tokens" .
Armin

Nov 18 '08 #4
This is by design. See :
http://msdn.microsoft.com/en-us/libr...hs(VS.80).aspx
(Value Types and Reference Types)

As "A reference type contains a pointer to another memory location that
holds the data" and arrays are reference type it means that values=tokens
make the "values" variable to point to the same memory location than
"tokens" (and this is true for all objects).

See :
http://msdn.microsoft.com/en-us/libr...rray.copy.aspx for how to
copy the array content...

--
Patrice

"barcrofter " <jd*****@gmail. coma écrit dans le message de groupe de
discussion :
5d************* *************** **...legroups .com...
I have two independent arrays and mysteriously they occupy the same
space. Is this an error or a feature?

Dim tokens$() '(input list of msn's and
constants)
Dim values() As Object '(tokens with msn's replaced by
values)

temp="A = 2 * B"

'---parse the temp expression into tokens
tokens = Split(temp, " ")

'---setup a similar sized values array
ReDim values(UBound(t okens))
values=tokens

and under vb.net changing tokens(4) will now change values(4)!!.

I expected that making values = tokens would give me an array "copy by
element". In fact it made the two occupy the same array space. Is
this a bug or a language feature??
Nov 18 '08 #5
Wow! Humbling.
Nov 19 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8375
by: Raptor | last post by:
Hi, I'm quite new to MySQL and quite impressed by its feature set. I've also been looking at Interbase and it has a feature that allows a multidimensional array to be stored in a single field. Does MySQL also have this feature? I was thinking about using MySQLs GIS data types but it only supports a two dimensional point, where as I need a three dimensional point to be stored. Each point represents a radar point. If anybody can...
13
21376
by: HappyHippy | last post by:
Hi, I'm wondering what you think about this piece of code: #include<iostream> int main() { int size; std::cin >> size;
4
5399
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string literal. But the second printf seems to give me garbage. Any advise on what I am doing wrong? Thanx
11
4473
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
4
9700
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person writing the JavaScript doesn't have to pass the index in the "onChange" event name. I thought that one might be able to use "this.value" or compare this as
104
17016
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from that array Could you show me a little example how to do this? Thanks.
3
1904
by: herrin | last post by:
Hello, I'm trying to assign strings to array elements in javascript. It works in Firefox but in IE it throws "Unknown runtime error". sContent = '<p>this is a features test post.</p><p><a title="test" href="http://url.com">link</a></p>'; g_ImageTable = new Array ('<?php echo $sFeatureImgUrl; ?>', '<?php echo $iId; ?>', '<?php echo $sPermalink; ?>', '<?php echo $sTitle; ?>', sContent, '<?php echo $sCategory; ?>', '<?php echo $sTime;...
13
1714
by: sathyashrayan | last post by:
Dear group, pls go through the following function definition: function at_show_aux(parent, child) { var p = document.getElementById(parent); var c = document.getElementById(child); var top = (c == "y") ? p.offsetHeight+2 : 0; var left = (c == "x") ? p.offsetWidth +2 : 0;
9
1852
by: IamIan | last post by:
I'm using an array to store map features (name, lat, lon, caption, etc), from which the user can then select an individual feature. The problem is that when thousands of features are stored in the array, looping over the entire array looking for a match is SLOW. So I'm running a hash in parallel, where every time a feature is pushed onto the array it's name is also added to the hash as an identical key value pair. I then check if the key...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.