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.GetUpperBound(0)
'when above works, then do this:
For idx = 0 To cnt - 1
picArray(idx).BorderStyle = BorderStyle.FixedSingle
picArray(idx).BackColor = SystemColors.Control
picArray(idx).Height = size
picArray(idx).Width = size
Next
Thanks,
Dean Slindee 3 2503
* "Dean Slindee" <sl*****@mindspring.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>
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).BorderStyle = BorderStyle.FixedSingle
picArray(idx).BackColor = SystemColors.Control
picArray(idx).Height = size
picArray(idx).Width = 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*****@mindspring.com> wrote in message
news:ee**************@tk2msftngp13.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.GetUpperBound(0)
'when above works, then do this:
For idx = 0 To cnt - 1
picArray(idx).BorderStyle = BorderStyle.FixedSingle
picArray(idx).BackColor = SystemColors.Control
picArray(idx).Height = size
picArray(idx).Width = size
Next Thanks,
Dean Slindee
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**************@TK2MSFTNGP10.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).BorderStyle = BorderStyle.FixedSingle picArray(idx).BackColor = SystemColors.Control picArray(idx).Height = size picArray(idx).Width = 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*****@mindspring.com> wrote in message news:ee**************@tk2msftngp13.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.GetUpperBound(0)
'when above works, then do this:
For idx = 0 To cnt - 1
picArray(idx).BorderStyle = BorderStyle.FixedSingle
picArray(idx).BackColor = SystemColors.Control
picArray(idx).Height = size
picArray(idx).Width = size
Next Thanks,
Dean Slindee
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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.
|
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
|
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...
|
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...
| |
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 ...
|
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...
|
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
|
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
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |