473,698 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An Array question

Hi everybody,
To me the following code shouldn't work but it does !
Imports system.String
Dim x As String="This,is ,a,test"
Dim y(1) As String
y=x.Split(",")
TextBox1.text=y (3)
why "Y" which is an array of lenght 2 accepts index 3 which is larger than
its lenght?
Any thoughts?
--
Best regards,
Edward
Jun 27 '08 #1
14 993

"Edward" <Ed****@discuss ions.microsoft. comwrote in message
news:ED******** *************** ***********@mic rosoft.com...
Hi everybody,
To me the following code shouldn't work but it does !
Imports system.String
Dim x As String="This,is ,a,test"
Dim y(1) As String
y=x.Split(",")
TextBox1.text=y (3)
why "Y" which is an array of lenght 2 accepts index 3 which is larger
than
its lenght?
Any thoughts?
--
Best regards,
Edward
The statement:
y=x.Split(",")

creates a new array and assigns it to y. The first assignment allows for
items (0) and (1). The reassignment (the split statement) just returns an
array the size of which depends on the source string and the split string.

LS

Jun 27 '08 #2
yes but I was wondering about the logic behind this kind of behavior , when
we define a fixed size for an array isn't it suppose to keep its fixed size ?
In fact we can assign a larger array to a smaller array and VB doesn't
complian and resizes the smaller array!
--
Best regards,
Edward
"Lloyd Sheen" wrote:
>
"Edward" <Ed****@discuss ions.microsoft. comwrote in message
news:ED******** *************** ***********@mic rosoft.com...
Hi everybody,
To me the following code shouldn't work but it does !
Imports system.String
Dim x As String="This,is ,a,test"
Dim y(1) As String
y=x.Split(",")
TextBox1.text=y (3)
why "Y" which is an array of lenght 2 accepts index 3 which is larger
than
its lenght?
Any thoughts?
--
Best regards,
Edward

The statement:
y=x.Split(",")

creates a new array and assigns it to y. The first assignment allows for
items (0) and (1). The reassignment (the split statement) just returns an
array the size of which depends on the source string and the split string.

LS

Jun 27 '08 #3
"Edward" <Ed****@discuss ions.microsoft. comschrieb:
To me the following code shouldn't work but it does !
Imports system.String
Dim x As String="This,is ,a,test"
Dim y(1) As String
='Dim y() As String'.
y=x.Split(",")
TextBox1.text=y (3)
why "Y" which is an array of lenght 2 accepts index 3 which is larger
than
its lenght?
'Y' references an array of length 4 with indices 0, ..., 3 after the
assignment of 'Split''s return value. 'Y(3)' contains "test".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jun 27 '08 #4

"Edward" <Ed****@discuss ions.microsoft. comwrote in message
news:F8******** *************** ***********@mic rosoft.com...
yes but I was wondering about the logic behind this kind of behavior ,
when
we define a fixed size for an array isn't it suppose to keep its fixed
size ?
In fact we can assign a larger array to a smaller array and VB doesn't
complian and resizes the smaller array!
--
Best regards,
Edward
"Lloyd Sheen" wrote:
>>
"Edward" <Ed****@discuss ions.microsoft. comwrote in message
news:ED******* *************** ************@mi crosoft.com...
Hi everybody,
To me the following code shouldn't work but it does !
Imports system.String
Dim x As String="This,is ,a,test"
Dim y(1) As String
y=x.Split(",")
TextBox1.text=y (3)
why "Y" which is an array of lenght 2 accepts index 3 which is larger
than
its lenght?
Any thoughts?
--
Best regards,
Edward

The statement:
y=x.Split(", ")

creates a new array and assigns it to y. The first assignment allows for
items (0) and (1). The reassignment (the split statement) just returns
an
array the size of which depends on the source string and the split
string.

LS

It really is no different than any other variable assignment. At one moment
you have an array with capacity of 2 (0) and (1). When you use the x.split
you assign a new array. The old one is put in for garbage collection unless
there is another reference to it.

Same as if I assign a new string to a string varaible. I think you are
taking the first assignment as a "set in concrete" statement where it is
not. Until another assignment happens (which is the split) it has one array
and then after the assignment (split) you now have a new one.

LS

Jun 27 '08 #5
Edward wrote:
yes but I was wondering about the logic behind this kind of behavior , when
we define a fixed size for an array isn't it suppose to keep its fixed size ?
In fact we can assign a larger array to a smaller array and VB doesn't
complian and resizes the smaller array!
You need to understand that "Split" basically does a REDIM on your
variable, however, if you really must limit the size of the Array, try
the following -

Dim x As String = "This,is,a,test "
Dim y() As String = Split(x, ",", 2)

You'll find that "y" is now limited to just two elements. (0 and 1)

I hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Jun 27 '08 #6
Edward wrote:
I was wondering about the logic behind this kind of behavior , when
we define a fixed size for an array isn't it suppose to keep its fixed size ?
Nope.
In fact we can assign a larger array to a smaller array and VB doesn't
complain and resizes the smaller array!
It /used/ to be that the equivalent, VB "Proper" code ...

Dim y(1) As String
y = Split(x, ",")

.... wouldn't even /compile/ precisely because of this discontinuity.

In our Brave New World.Net, however, Split() creates a whole /new/ array
and dumps a reference to it back into the variable "y", junking whatever
may or may not have been there before; there's no relationship at all
between the previous array and the one that you're now assigning to it.

HTH,
Phill W.
Jun 27 '08 #7
On Jun 2, 6:09 pm, ShaneO <spc...@optusne t.com.auwrote:
Edward wrote:
yes but I was wondering about the logic behind this kind of behavior , when
we define a fixed size for an array isn't it suppose to keep its fixed size ?
In fact we can assign a larger array to a smaller array and VB doesn't
complian and resizes the smaller array!

You need to understand that "Split" basically does a REDIM on your
variable, however, if you really must limit the size of the Array, try
the following -
This is not accurate. The Split function does not ReDim the existing
array. Arrays are reference types. As pointed out earlier, Split
creates a *new* array and then assigns a reference to it to variable
y, discarding the old reference. There is no ReDim involved, you can
use Reflector to verify that.

Chris
Jun 27 '08 #8
Chris,

I wished I could tell you in Dutch
But what is in a word, there is done again a Dimension of an Array. So to
use the sentence ReDim is in my idea not that wrong. That ReDim keyword
restores the values of an old array is in my idea not the right use of the
meaning of the word in English.

However who am I to state that.

:-)

Cor

"Chris Dunaway" <du******@gmail .comschreef in bericht
news:d0******** *************** ***********@b1g 2000hsg.googleg roups.com...
On Jun 2, 6:09 pm, ShaneO <spc...@optusne t.com.auwrote:
>Edward wrote:
yes but I was wondering about the logic behind this kind of behavior ,
when
we define a fixed size for an array isn't it suppose to keep its fixed
size ?
In fact we can assign a larger array to a smaller array and VB doesn't
complian and resizes the smaller array!

You need to understand that "Split" basically does a REDIM on your
variable, however, if you really must limit the size of the Array, try
the following -

This is not accurate. The Split function does not ReDim the existing
array. Arrays are reference types. As pointed out earlier, Split
creates a *new* array and then assigns a reference to it to variable
y, discarding the old reference. There is no ReDim involved, you can
use Reflector to verify that.

Chris
Jun 27 '08 #9
Chris Dunaway wrote:
On Jun 2, 6:09 pm, ShaneO <spc...@optusne t.com.auwrote:
>Edward wrote:
>>yes but I was wondering about the logic behind this kind of behavior , when
we define a fixed size for an array isn't it suppose to keep its fixed size ?
In fact we can assign a larger array to a smaller array and VB doesn't
complian and resizes the smaller array!
You need to understand that "Split" basically does a REDIM on your
variable, however, if you really must limit the size of the Array, try
the following -

This is not accurate. The Split function does not ReDim the existing
array. Arrays are reference types. As pointed out earlier, Split
creates a *new* array and then assigns a reference to it to variable
y, discarding the old reference. There is no ReDim involved, you can
use Reflector to verify that.

Chris
Thank-you Chris, you are technically correct, however as I wrote in my
post it "basically does a REDIM". At the end of the day the original
variables reference is replaced with a reference to a new array created
by the Split function, but so what, for the sake of simplicity in
replying to the OP question, it "basically does a REDIM". :-)
ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Jun 27 '08 #10

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

Similar topics

3
6406
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include <stdio.h> void main(void) { char lines; int count = 0, i;
9
2617
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ 6.19 (http://www.eskimo.com/~scs/C-faq/q6.19.html). Thanks.
3
2693
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array is initialized with one element it works fine) But I can not change the initial state of the array. It must be null. what must I change.
11
2264
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^ LHSquestions = gcnew array<String^> {"question 1","question 2"}; private: static array<String^>^ RHSquestions = gcnew array<String^> {"question 1",
28
2434
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions 1: Why cannot I do the following:
104
16964
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.
51
23702
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc 10018-clc.c: In function `main': 10018-clc.c:22: warning: array subscript has type `char' I don't like warnings ... or casts.
7
6434
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are now thown out of the array released properly by the CLI?
8
1484
by: T. Wintershoven | last post by:
Hello all, I have a form with some checkboxes. The names of these checkboxes come from an array. When i click the submit button the resultcode doesn't recognize the names when i want to check wether or not some checkboxes are ticked. Assume that i tick checkboxes 100, 150 and 200 Below is some code i've used.(between the ****** lines)
4
4556
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first element so append a comma
0
8671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
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
9152
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...
1
8887
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
7709
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...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5858
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();...
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
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.