473,597 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can an empty array be iteratively extended?

I want to build an array on the fly in code with a parameter passed for the
number of elements in the array. I am trying to do this by looping, but
get an array with just one element in the end.
Can this be done my way or some other way?

Dim idx As Integer

Dim cnt As Integer = 5 'parm for # of elements

Dim picArray As PictureBox()

For idx = 0 To cnt - 1

picArray = New PictureBox() {New PictureBox} 'one element

picArray(idx) = New PictureBox() {New PictureBox} 'bad syntax

Next

idx = picArray.GetUpp erBound(0)

'when above works, then do this:

For idx = 0 To cnt - 1

picArray(idx).B orderStyle = BorderStyle.Fix edSingle

picArray(idx).B ackColor = SystemColors.Co ntrol

picArray(idx).H eight = size

picArray(idx).W idth = size

Next

Thanks,

Dean Slindee
Nov 20 '05 #1
3 2517
* "Dean Slindee" <sl*****@mindsp ring.com> scripsit:
I want to build an array on the fly in code with a parameter passed for the
number of elements in the array. I am trying to do this by looping, but
get an array with just one element in the end.
Can this be done my way or some other way?


Instead of using an array (can be changed in size without loosing its
elements with 'ReDim Preserve'), use an 'ArrayList'. You can add
elements to the arraylist by passing them to the arraylist's 'Add'
method.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
Dean,
I want to build an array on the fly in code with a parameter passed for the number of elements in the array. Is the number of elements fixed based on a parameter? If so, then you can
use something like the following, however if the number of elements are
unknown and you want an "array" that can change size I would do as Herfried
suggests & use Redim Preserve or an ArrayList (I favor an ArrayList over
Redim Preserve as its easier).

Have you tried something like:

Public Sub MakeArray(ByVal cnt As Integer)
Dim picArray(cnt - 1) As PictureBox
Dim idx As Integer

For idx = 0 To cnt - 1
picArray(idx) = New PictureBox()

picArray(idx).B orderStyle = BorderStyle.Fix edSingle
picArray(idx).B ackColor = SystemColors.Co ntrol
picArray(idx).H eight = size
picArray(idx).W idth = size
Next

End Sub

Note that arrays in VB.NET are "off by one", you give the high bound on the
Dim, not the number of elements, hence the "cnt - 1" on the Dim picArray.

Hope this helps
Jay
"Dean Slindee" <sl*****@mindsp ring.com> wrote in message
news:ee******** ******@tk2msftn gp13.phx.gbl... I want to build an array on the fly in code with a parameter passed for the number of elements in the array. I am trying to do this by looping, but
get an array with just one element in the end.
Can this be done my way or some other way?

Dim idx As Integer

Dim cnt As Integer = 5 'parm for # of elements

Dim picArray As PictureBox()

For idx = 0 To cnt - 1

picArray = New PictureBox() {New PictureBox} 'one element

picArray(idx) = New PictureBox() {New PictureBox} 'bad syntax

Next

idx = picArray.GetUpp erBound(0)

'when above works, then do this:

For idx = 0 To cnt - 1

picArray(idx).B orderStyle = BorderStyle.Fix edSingle

picArray(idx).B ackColor = SystemColors.Co ntrol

picArray(idx).H eight = size

picArray(idx).W idth = size

Next

Thanks,

Dean Slindee

Nov 20 '05 #3
Jay,
Yes, that does the trick! I had (erroneously) boxed myself in with the
notion that the picArray()had to be Dim'd (scoped) at the form-level in
order to be able show the picArray in a panel on the form. Ah, scope freedom
is a wonderful thing.
Thanks,
Dean

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:#C******** ******@TK2MSFTN GP10.phx.gbl...
Dean,
I want to build an array on the fly in code with a parameter passed for the
number of elements in the array.

Is the number of elements fixed based on a parameter? If so, then you can
use something like the following, however if the number of elements are
unknown and you want an "array" that can change size I would do as

Herfried suggests & use Redim Preserve or an ArrayList (I favor an ArrayList over
Redim Preserve as its easier).

Have you tried something like:

Public Sub MakeArray(ByVal cnt As Integer)
Dim picArray(cnt - 1) As PictureBox
Dim idx As Integer

For idx = 0 To cnt - 1
picArray(idx) = New PictureBox()

picArray(idx).B orderStyle = BorderStyle.Fix edSingle
picArray(idx).B ackColor = SystemColors.Co ntrol
picArray(idx).H eight = size
picArray(idx).W idth = size
Next

End Sub

Note that arrays in VB.NET are "off by one", you give the high bound on the Dim, not the number of elements, hence the "cnt - 1" on the Dim picArray.

Hope this helps
Jay
"Dean Slindee" <sl*****@mindsp ring.com> wrote in message
news:ee******** ******@tk2msftn gp13.phx.gbl...
I want to build an array on the fly in code with a parameter passed for

the
number of elements in the array. I am trying to do this by looping, but get an array with just one element in the end.
Can this be done my way or some other way?

Dim idx As Integer

Dim cnt As Integer = 5 'parm for # of elements

Dim picArray As PictureBox()

For idx = 0 To cnt - 1

picArray = New PictureBox() {New PictureBox} 'one element

picArray(idx) = New PictureBox() {New PictureBox} 'bad syntax

Next

idx = picArray.GetUpp erBound(0)

'when above works, then do this:

For idx = 0 To cnt - 1

picArray(idx).B orderStyle = BorderStyle.Fix edSingle

picArray(idx).B ackColor = SystemColors.Co ntrol

picArray(idx).H eight = size

picArray(idx).W idth = size

Next

Thanks,

Dean Slindee


Nov 20 '05 #4

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

Similar topics

3
1439
by: Jyrki Keisala | last post by:
I have an XML file which looks like this: <command name="Options" phrase="Options"> <key value="z" extended="NONE" qual="L-CTRL" pause="100" repeat="1" duration="200"/> </command> <command name="Command Room" phrase="Command Room"> <key extended="F2" pause="100" repeat="1" duration="200"/> </command>
7
6470
by: Derek Basch | last post by:
I can remove objects from an array by doing this: for (i in oCache){ if (i == "test"){ delete oCache; }; }; However, the array's length property is unaffected.
3
276674
by: pkumar | last post by:
How to convert this byte array to string byte b=new byte; Is there any function or I need read one by one and build the string thanks
2
2012
by: Bob Stearns | last post by:
I thought that the given expression was always TRUE if "not_there" wasn't among the keys (or subscripts if you will) of $_SESSION. Below find a dump of $_SESSION, a small snippet of code and the results. I really don't understand. A new pair of eyes may be able to spot what should be an obvious bug. Thanks for looking. _SESSION=array 16 { userid=>bob; setname=>Junk_Cows; setuser=>jhough;
1
6544
by: dave | last post by:
I am having a problem i bleieve a number of other individuals have had before. I have tried the solutions found on the net however I still have the problem. I am hosting a web site on an external server (i do not have access to registry). I am using the following connection string to read values from rows from an excel sheet into a dataset/datatable. One of the columns contains alpha-numeric values ie. 400c
13
1702
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;
4
2324
by: noddy | last post by:
I have a mysql table called Users The userID field values start at 100 and increment up from there I am trying to transfer values from this table into a javascript array Here is what the code looks like: <?php $queryUser = mysql_query("SELECT * FROM Users...etc $row_queryUser = mysql_fetch_assoc($queryUser); $lengthUsers = mysql_num_rows($queryUser) ; ?>
7
1955
by: Giakko | last post by:
hello, is there a way to create an array in this way?: new Array('key1'='value1','key2'='value2','key3'='value3') ?????? thanks, simone, http://www.notte.com
36
9151
by: laredotornado | last post by:
Hi, I'm using PHP 5. I have an array of strings. What is the simplest way to remove the elements that are empty, i.e. where the expression "empty($elt)" returns true? Thanks, - Dave
0
7969
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
7886
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
8272
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
8381
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
8035
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
8258
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
6688
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
5847
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
3886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.