473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Redim Multidimenional Arrays

I am having a problem using Multidim arrays. I want to create an array
which as I understand it is dimensioned as:

dim xyz (rows,columns) as String

I want to populate it with rows from a table in a database. I don't know
how many rows I am getting so of course I have to Redim inside the loop that
does this.

Unfortunately, the error message and documentation say: "In a
multidimensional array, you can change only the last dimension when you use
Preserve"

How can that be? How can you not be able to change the number of rows? Or
am I missing something? Is there a different variable type that I need to
use?

Thanx in advance,
--
Anil Gupte
www.keeninc.net
www.icinema.com
Oct 5 '06 #1
9 5338
Is there a different variable type that I need to use?

Use a dataset for working with database tables.

Also you could build a dynamic array by first counting the number of
rows and columns, then sizing the array, and then populating it. But
that will give you a difficult to manage, unflexible, memory hog of an
object, so you should stick with the datasets.

Thanks,

Seth Rowe

Anil Gupte wrote:
I am having a problem using Multidim arrays. I want to create an array
which as I understand it is dimensioned as:

dim xyz (rows,columns) as String

I want to populate it with rows from a table in a database. I don't know
how many rows I am getting so of course I have to Redim inside the loop that
does this.

Unfortunately, the error message and documentation say: "In a
multidimensional array, you can change only the last dimension when you use
Preserve"

How can that be? How can you not be able to change the number of rows? Or
am I missing something? Is there a different variable type that I need to
use?

Thanx in advance,
--
Anil Gupte
www.keeninc.net
www.icinema.com
Oct 5 '06 #2
Well, two things here. Not allowing the number of rows to change is the
dmbest implementation of Arrays I have ever seen. I'd like to thump the guy
in charge of that piece. :-)

But more pertinently, lests discuss your suggestion of using datasets. I
already am using a dataset. I am trying to put the data from the dataset
into an array so I can access it more easily. The underlying problem is
that I have a dropdown list where based on the selection I want to access
another table. So, for example, if I have:

One
Two
Three
Four

in a drop down list that comes from a table (call it Table1) containing
One, Kids
Two, Adults
Three, Couples
Four, Singles

I want to then select from a different table based on the drop down
selection. So if someone chooses Kids from the drop down, I want to execute
"Select * from Table2 where xyz='Kids'"

Any suggestions?
--
Anil Gupte
www.keeninc.net
www.icinema.com

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>Is there a different variable type that I need to use?

Use a dataset for working with database tables.

Also you could build a dynamic array by first counting the number of
rows and columns, then sizing the array, and then populating it. But
that will give you a difficult to manage, unflexible, memory hog of an
object, so you should stick with the datasets.

Thanks,

Seth Rowe

Anil Gupte wrote:
>I am having a problem using Multidim arrays. I want to create an array
which as I understand it is dimensioned as:

dim xyz (rows,columns) as String

I want to populate it with rows from a table in a database. I don't know
how many rows I am getting so of course I have to Redim inside the loop
that
does this.

Unfortunately, the error message and documentation say: "In a
multidimensional array, you can change only the last dimension when you
use
Preserve"

How can that be? How can you not be able to change the number of rows?
Or
am I missing something? Is there a different variable type that I need
to
use?

Thanx in advance,
--
Anil Gupte
www.keeninc.net
www.icinema.com

Oct 7 '06 #3
Anil Gupte wrote:
I am having a problem using Multidim arrays. I want to create an array
which as I understand it is dimensioned as:

dim xyz (rows,columns) as String

I want to populate it with rows from a table in a database. I don't know
how many rows I am getting so of course I have to Redim inside the loop that
does this.

Unfortunately, the error message and documentation say: "In a
multidimensional array, you can change only the last dimension when you use
Preserve"
<snip>

You must declare your array like this:

Dim xyz(Columns, Rows) As String

This way the Rows dimmension can be resized at will.

The Redim keyword comes from VB.Classic. It turns out that arrays, in
VB.Classic, used column-major order (maybe because Basic copied so many
Fortran features). This means that the array binaries were layed out in
memory in a way that turned impractical to redim any but the last
dimmension. For instance, the following array:

Dim A(0 To 1, 0 To 2) As Integer

Would have the following layout in memory in VB.Classic:

(0,0) (1, 0) (0, 1) (1, 1) (0, 2) (1, 2)

As you can see, resizing the first dimmension would require introducing
gaps all over the array, something doable but expensive. On the other
hand, resizing the last dimmension would only allocate more space at
the end of the array.

Notice that arrays in .Net have Row.major order. This means that the
previous array, in .Net, is layed out in memory like this:

(0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (1, 2)

which makes easier to resize the first dimmension, but not the others.

Nevertheless, the Redim rule from VB.Classic still applies, because of
compatibility issues. The irony is that VB.Net must now perform a
costly "copy by chunks" operation when you redim the array... :-P
HTH.

Regards,

Branco.

Oct 7 '06 #4
Well, two things here. Not allowing the number of rows to change is the
dmbest implementation of Arrays I have ever seen. I'd like to thump the guy
in charge of that piece. :-)
If you really wanted to, you could create your own methods to resize an
array. You would just have to create a new array with the correct
number or rows and columns. Then loop through the original array
plugging these values into the proper place in the new array. But then
you might as well flush the scalability and performance of your app
down the toilet :-)
I want to then select from a different table based on the drop down
selection. So if someone chooses Kids from the drop down, I want to execute
"Select * from Table2 where xyz='Kids'"
When the user changes the selection of the drop down list, why not just
hit one of the dataset tables with the SQL command? Also, you could
just use datareaders to populate the drop down and then execute your
SQL commands directly against the server. This may be better if you
have data that constantly changes, as you won't have to worry about
refreshing the dataset all the time. Also, as a small warning, building
SQL commands "on the fly" can be prone to SQL injection attacks, you
may look into using parameterized Stored procedures (or whatever
alternitive your database provides) in order to protect against these
attacks.

Hope that Helps,

Seth Rowe

Anil Gupte wrote:
Well, two things here. Not allowing the number of rows to change is the
dmbest implementation of Arrays I have ever seen. I'd like to thump the guy
in charge of that piece. :-)

But more pertinently, lests discuss your suggestion of using datasets. I
already am using a dataset. I am trying to put the data from the dataset
into an array so I can access it more easily. The underlying problem is
that I have a dropdown list where based on the selection I want to access
another table. So, for example, if I have:

One
Two
Three
Four

in a drop down list that comes from a table (call it Table1) containing
One, Kids
Two, Adults
Three, Couples
Four, Singles

I want to then select from a different table based on the drop down
selection. So if someone chooses Kids from the drop down, I want to execute
"Select * from Table2 where xyz='Kids'"

Any suggestions?
--
Anil Gupte
www.keeninc.net
www.icinema.com

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Is there a different variable type that I need to use?
Use a dataset for working with database tables.

Also you could build a dynamic array by first counting the number of
rows and columns, then sizing the array, and then populating it. But
that will give you a difficult to manage, unflexible, memory hog of an
object, so you should stick with the datasets.

Thanks,

Seth Rowe

Anil Gupte wrote:
I am having a problem using Multidim arrays. I want to create an array
which as I understand it is dimensioned as:

dim xyz (rows,columns) as String

I want to populate it with rows from a table in a database. I don't know
how many rows I am getting so of course I have to Redim inside the loop
that
does this.

Unfortunately, the error message and documentation say: "In a
multidimensional array, you can change only the last dimension when you
use
Preserve"

How can that be? How can you not be able to change the number of rows?
Or
am I missing something? Is there a different variable type that I need
to
use?

Thanx in advance,
--
Anil Gupte
www.keeninc.net
www.icinema.com
Oct 7 '06 #5
Yes, thanx - that helps undersand what is going on, but seems like the old
legacy dragon strikes again! I have programmed in Lisp for a long time and
Arrays are so easy to redimension in any manner it is not funny. But maybe
because of Lisp, I have always conceptually thought of arrays as being:
((0,0) (0,1)
(1,0) (1,1)
(2,0) (2,1)
and so on...
)

Anyway, I did not underatand your statement:

Dim xyz(Columns, Rows) As String

Do you actually use the words Columns and Rows? I am doing:
Dim xyz(,) As String

--
Anil Gupte
www.keeninc.net
www.icinema.com

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Anil Gupte wrote:
>I am having a problem using Multidim arrays. I want to create an array
which as I understand it is dimensioned as:

dim xyz (rows,columns) as String

I want to populate it with rows from a table in a database. I don't know
how many rows I am getting so of course I have to Redim inside the loop
that
does this.

Unfortunately, the error message and documentation say: "In a
multidimensional array, you can change only the last dimension when you
use
Preserve"
<snip>

You must declare your array like this:

Dim xyz(Columns, Rows) As String

This way the Rows dimmension can be resized at will.

The Redim keyword comes from VB.Classic. It turns out that arrays, in
VB.Classic, used column-major order (maybe because Basic copied so many
Fortran features). This means that the array binaries were layed out in
memory in a way that turned impractical to redim any but the last
dimmension. For instance, the following array:

Dim A(0 To 1, 0 To 2) As Integer

Would have the following layout in memory in VB.Classic:

(0,0) (1, 0) (0, 1) (1, 1) (0, 2) (1, 2)

As you can see, resizing the first dimmension would require introducing
gaps all over the array, something doable but expensive. On the other
hand, resizing the last dimmension would only allocate more space at
the end of the array.

Notice that arrays in .Net have Row.major order. This means that the
previous array, in .Net, is layed out in memory like this:

(0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (1, 2)

which makes easier to resize the first dimmension, but not the others.

Nevertheless, the Redim rule from VB.Classic still applies, because of
compatibility issues. The irony is that VB.Net must now perform a
costly "copy by chunks" operation when you redim the array... :-P
HTH.

Regards,

Branco.

Oct 7 '06 #6
why not just
hit one of the dataset tables with the SQL command?
Yes, that is exactly waht I want to do. But my problem is that I need some
way of separating what the dropdown list shows from the actual value that is
used in the SQL statement. Something like the DisplayMember and ValueMember
properties. See my other post "How to assign value to a dropdown box
items?"

Thanx,
--
Anil Gupte
www.keeninc.net
www.icinema.com

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>Well, two things here. Not allowing the number of rows to change is the
dmbest implementation of Arrays I have ever seen. I'd like to thump the
guy
in charge of that piece. :-)

If you really wanted to, you could create your own methods to resize an
array. You would just have to create a new array with the correct
number or rows and columns. Then loop through the original array
plugging these values into the proper place in the new array. But then
you might as well flush the scalability and performance of your app
down the toilet :-)
>I want to then select from a different table based on the drop down
selection. So if someone chooses Kids from the drop down, I want to
execute
"Select * from Table2 where xyz='Kids'"

When the user changes the selection of the drop down list, why not just
hit one of the dataset tables with the SQL command? Also, you could
just use datareaders to populate the drop down and then execute your
SQL commands directly against the server. This may be better if you
have data that constantly changes, as you won't have to worry about
refreshing the dataset all the time. Also, as a small warning, building
SQL commands "on the fly" can be prone to SQL injection attacks, you
may look into using parameterized Stored procedures (or whatever
alternitive your database provides) in order to protect against these
attacks.

Hope that Helps,

Seth Rowe

Anil Gupte wrote:
>Well, two things here. Not allowing the number of rows to change is the
dmbest implementation of Arrays I have ever seen. I'd like to thump the
guy
in charge of that piece. :-)

But more pertinently, lests discuss your suggestion of using datasets. I
already am using a dataset. I am trying to put the data from the dataset
into an array so I can access it more easily. The underlying problem is
that I have a dropdown list where based on the selection I want to access
another table. So, for example, if I have:

One
Two
Three
Four

in a drop down list that comes from a table (call it Table1) containing
One, Kids
Two, Adults
Three, Couples
Four, Singles

I want to then select from a different table based on the drop down
selection. So if someone chooses Kids from the drop down, I want to
execute
"Select * from Table2 where xyz='Kids'"

Any suggestions?
--
Anil Gupte
www.keeninc.net
www.icinema.com

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@m7g2000cwm.googlegr oups.com...
>Is there a different variable type that I need to use?

Use a dataset for working with database tables.

Also you could build a dynamic array by first counting the number of
rows and columns, then sizing the array, and then populating it. But
that will give you a difficult to manage, unflexible, memory hog of an
object, so you should stick with the datasets.

Thanks,

Seth Rowe

Anil Gupte wrote:
I am having a problem using Multidim arrays. I want to create an
array
which as I understand it is dimensioned as:

dim xyz (rows,columns) as String

I want to populate it with rows from a table in a database. I don't
know
how many rows I am getting so of course I have to Redim inside the
loop
that
does this.

Unfortunately, the error message and documentation say: "In a
multidimensional array, you can change only the last dimension when
you
use
Preserve"

How can that be? How can you not be able to change the number of
rows?
Or
am I missing something? Is there a different variable type that I
need
to
use?

Thanx in advance,
--
Anil Gupte
www.keeninc.net
www.icinema.com

Oct 7 '06 #7
Anil Gupte wrote:
<snip>
Anyway, I did not underatand your statement:

Dim xyz(Columns, Rows) As String

Do you actually use the words Columns and Rows? I am doing:
Dim xyz(,) As String
<snip>

Sorry, I didn't make it clear. What I meant was to treat the first
dimmension as the column selector, and the last dimmension as the row
selector. So, if you have a table with the columns "Name" and "Age",
you'd have:

Dim Items(0 To 1, 0 To -1) As String 'An empty table

'...
'Adds a row:
Dim NewIndex As Integer = Items.GetLength(1)
Redim Preserve Items(0 To 1, 0 To NewIndex)
Items(0, NewIndex) = TheName
Items(1, NewIndex) = TheAge

Of course, all this trouble is because you want to extract the data
from the original dataset.
Another approach could be to use the orginal table:

mData = New DataTable

mData.Columns.Add("ID", GetType(Integer), Nothing)
mData.Columns.Add("Name", GetType(String), Nothing)
mData.Columns.Add("DisplayText", _
GetType(String), "id + ' - ' + name")

mData.Rows.Add(1, "John")
mData.Rows.Add(2, "Mary")
mData.Rows.Add(3, "Lamb")
mData.Rows.Add(4, "Little Pig")

With Me.ComboBox1
.DataSource = mData
.DisplayMember = "DisplayText"
.ValueMember = "ID"

'Selects the item with ID = 4
.SelectedValue = 4
End With

HTH

Regards,

Branco.

Oct 7 '06 #8
Thanx for the help. I got it working with the dataset instead of using the
arrays. However, now I have different problem. The
DropDownProfiles.SelectedValue returns a "DataRowView" instead of the value
from the field (my program expects an integer). If you have some
suggestions, let me know.

Thanx for the help so far.
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Anil Gupte wrote:
<snip>
>Anyway, I did not underatand your statement:

Dim xyz(Columns, Rows) As String

Do you actually use the words Columns and Rows? I am doing:
Dim xyz(,) As String
<snip>

Sorry, I didn't make it clear. What I meant was to treat the first
dimmension as the column selector, and the last dimmension as the row
selector. So, if you have a table with the columns "Name" and "Age",
you'd have:

Dim Items(0 To 1, 0 To -1) As String 'An empty table

'...
'Adds a row:
Dim NewIndex As Integer = Items.GetLength(1)
Redim Preserve Items(0 To 1, 0 To NewIndex)
Items(0, NewIndex) = TheName
Items(1, NewIndex) = TheAge

Of course, all this trouble is because you want to extract the data
from the original dataset.
Another approach could be to use the orginal table:

mData = New DataTable

mData.Columns.Add("ID", GetType(Integer), Nothing)
mData.Columns.Add("Name", GetType(String), Nothing)
mData.Columns.Add("DisplayText", _
GetType(String), "id + ' - ' + name")

mData.Rows.Add(1, "John")
mData.Rows.Add(2, "Mary")
mData.Rows.Add(3, "Lamb")
mData.Rows.Add(4, "Little Pig")

With Me.ComboBox1
.DataSource = mData
.DisplayMember = "DisplayText"
.ValueMember = "ID"

'Selects the item with ID = 4
.SelectedValue = 4
End With

HTH

Regards,

Branco.

Oct 8 '06 #9
I solved that problem. I googled for it and found this very interesting
article:

http://www.codeproject.com/csharp/sc...rolbinding.asp

Apparently the only right way to do this is to set DatSource last!
Amazing! (I would call this a bug in MS' implementation)

--
Anil Gupte
www.keeninc.net
www.icinema.com

"Anil Gupte" <an*******@icinema.comwrote in message
news:eb****************@TK2MSFTNGP02.phx.gbl...
Thanx for the help. I got it working with the dataset instead of using
the arrays. However, now I have different problem. The
DropDownProfiles.SelectedValue returns a "DataRowView" instead of the
value from the field (my program expects an integer). If you have some
suggestions, let me know.

Thanx for the help so far.
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
>Anil Gupte wrote:
<snip>
>>Anyway, I did not underatand your statement:

Dim xyz(Columns, Rows) As String

Do you actually use the words Columns and Rows? I am doing:
Dim xyz(,) As String
<snip>

Sorry, I didn't make it clear. What I meant was to treat the first
dimmension as the column selector, and the last dimmension as the row
selector. So, if you have a table with the columns "Name" and "Age",
you'd have:

Dim Items(0 To 1, 0 To -1) As String 'An empty table

'...
'Adds a row:
Dim NewIndex As Integer = Items.GetLength(1)
Redim Preserve Items(0 To 1, 0 To NewIndex)
Items(0, NewIndex) = TheName
Items(1, NewIndex) = TheAge

Of course, all this trouble is because you want to extract the data
from the original dataset.
Another approach could be to use the orginal table:

mData = New DataTable

mData.Columns.Add("ID", GetType(Integer), Nothing)
mData.Columns.Add("Name", GetType(String), Nothing)
mData.Columns.Add("DisplayText", _
GetType(String), "id + ' - ' + name")

mData.Rows.Add(1, "John")
mData.Rows.Add(2, "Mary")
mData.Rows.Add(3, "Lamb")
mData.Rows.Add(4, "Little Pig")

With Me.ComboBox1
.DataSource = mData
.DisplayMember = "DisplayText"
.ValueMember = "ID"

'Selects the item with ID = 4
.SelectedValue = 4
End With

HTH

Regards,

Branco.


Oct 8 '06 #10

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

Similar topics

10
by: KN | last post by:
I know both are pretty much the same and it comes down to personal choice. But I have to make the choice for the team. Things so far that I am considering 1. XML documentation in C# -- thats...
0
by: Lee Holsenbeck | last post by:
hi, i'm reading recordsets into arrays and need to read them from other procedures and resize them based on the record size, but cannot do both of those without an error. anyone know how to make...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
4
by: mom_newbie | last post by:
Hello all, I want to perform the following task: 1. Create an array 2. Fill it up (number of elements unknown in advance) 3. Iterate through it using For Each loop (cannot do this in the in...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
6
by: Fred Flintstone | last post by:
I have an app that requires the use of a 2 dimensional array of checkboxes. I can't seem to assign any values to them. I tried this: Dim BonusChecks(0, 0) As CheckBox ...and then later when...
6
by: chad | last post by:
assuming I have 2GB memory. And 1.5GB available. dim c1(,) as byte redim c1(1000, 1024*1024) throws system.outofmemoryexception. I'm like... huh? Initially I thought a lot of space is taken...
12
by: divya | last post by:
1. If The size of the array is unknown during programming time then how is such array declared?? Suppose there is an Array named arrClashname(size unknown at start), Now when the clash occurs...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.