Does VB have something that can return just a column of a multi-column array?
Author |
Message |
George Heste #1 / 25
|
Does VB have something that can return just a column of a multi-column array?
I would like to access just one dimension of a multi-column and rows array. Something like if A(i,j) is an array with values for i and j how about A(I,j) being just a one-dimensional array? I could be say the number of elements i. Thanks. -- George Hester _________________________________
|
Thu, 16 Dec 2010 17:06:42 GMT |
|
|
RB Smissaer #2 / 25
|
Does VB have something that can return just a column of a multi-column array?
I don't think there is any simple way to do that other than making a new array and copying from the old to the new array, so something like this: Dim i As Long Dim A(0 To 10, 0 To 2) As Long Dim A1() As Long ReDim A1(LBound(A) To UBound(A)) As Long For i = LBound(A1) To UBound(A1) A1(i) = A(i, 1) Next i RBS
Quote: >I would like to access just one dimension of a multi-column and rows array. > Something like if A(i,j) is an array with values for i and j how about > A(I,j) being just a one-dimensional array? I could be say the number of > elements i. Thanks. > -- > George Hester > _________________________________
|
Thu, 16 Dec 2010 17:18:05 GMT |
|
|
Larry Serflate #3 / 25
|
Does VB have something that can return just a column of a multi-column array?
Quote: > I would like to access just one dimension of a multi-column and rows array. > Something like if A(i,j) is an array with values for i and j how about > A(I,j) being just a one-dimensional array? I could be say the number of > elements i. Thanks.
Not gonna happen, unless you build it as an array of arrays. Even then it may not be worth the hassle since you can just pass a reference to the entire mutli-dim. array around and iterate through whatever you need. LFS Private Sub Form_Load() Dim row(0 To 3), col, i For i = 0 To 3 ' Populating an array of arrays.... ReDim col(0 To 2) As Long col(0) = i col(1) = i + i col(2) = i + i + i row(i) = col Next i col = row(2) ' Grab that one row... For i = 0 To 2 Debug.Print i, col(i) Next End Sub
|
Thu, 16 Dec 2010 20:46:42 GMT |
|
|
argus #4 / 25
|
Does VB have something that can return just a column of a multi-column array?
Quote:
> I would like to access just one dimension of a multi-column and rows array. > Something like if A(i,j) is an array with values for i and j how about > A(I,j) being just a one-dimensional array? I could be say the number of > elements i. Thanks. > -- > George Hester > _________________________________
I think you could do that in dBASE II, but not in VB (Define a two dim array, and then pick an array item with a single number. Gad!! That's a looong time ago!!)
|
Thu, 16 Dec 2010 21:56:34 GMT |
|
|
dpb #5 / 25
|
Does VB have something that can return just a column of a multi-column array?
Quote:
> I would like to access just one dimension of a multi-column and rows array. > Something like if A(i,j) is an array with values for i and j how about > A(I,j) being just a one-dimensional array? I could be say the number of > elements i. Thanks.
As others have noted, a sad lack in VB is the implementation of array slices and other array operations as part of the syntax... :( --
|
Thu, 16 Dec 2010 22:31:27 GMT |
|
|
Jim Mac #6 / 25
|
Does VB have something that can return just a column of a multi-column array?
Quote:
> I would like to access just one dimension of a multi-column and > rows array. Something like if A(i,j) is an array with values for i > and j how about A(I,j) being just a one-dimensional array? I could > be say the number of elements i. Thanks.
VB doesn't natively allow that, but it's possible to fool VB into thinking it does, depending on how the array is ordered. But it's a hack, and has to be approached carefully. Basically, the idea is to create a blank single-dim array, and manipulate its SafeArray structure to have the correct bounds, and a pointer to the data of interest. It's not for the inexperienced or the careless, though. You can get things bollixed up pretty well if you miss a step, or do something you shouldn't. -- Jim Mack MicroDexterity Inc www.microdexterity.com
|
Thu, 16 Dec 2010 23:39:44 GMT |
|
|
Bob O`Bo #7 / 25
|
Does VB have something that can return just a column of a multi-column array?
Quote:
>> I would like to access just one dimension of a multi-column and >> rows array. Something like if A(i,j) is an array with values for i >> and j how about A(I,j) being just a one-dimensional array? I could >> be say the number of elements i. Thanks. > VB doesn't natively allow that, but it's possible to fool VB into > thinking it does, depending on how the array is ordered. But it's a > hack, and has to be approached carefully. > Basically, the idea is to create a blank single-dim array, and > manipulate its SafeArray structure to have the correct bounds, and a > pointer to the data of interest. > It's not for the inexperienced or the careless, though. You can get > things bollixed up pretty well if you miss a step, or do something you > shouldn't.
A somewhat safer alternative (but using more resources) is LSet. When the definitions of the members of a UDT are arranged appropriately, one can sometimes use LSet to move a block of data, intact, to an instance of a different type, which then gives different access to the same byte data. Option Explicit Public Type A twoD(0 To 9, 0 To 9) As Byte End Type Public Type C x(0 To 9) As Byte End Type Public Type B oneD(0 To 9) As C End Type Dim FirstOne As A Dim SecondOne As B Public Sub main() Dim i As Long, j As Long 'build some test data For i = 0 To 9 For j = 0 To 9 FirstOne.twoD(i, j) = Val(CStr(i) & CStr(j)) Next Next '"translate" LSet SecondOne = FirstOne 'read one For i = 0 To 9 Debug.Print SecondOne.oneD(5).x(i) Next End Sub Bob --
|
Fri, 17 Dec 2010 02:01:22 GMT |
|
|
George Heste #8 / 25
|
Does VB have something that can return just a column of a multi-column array?
That sounds neat. A 1-dim array of pointers to the second column of the multi-dim array A Interesting. Thanks for the tip. -- George Hester _________________________________
Quote:
> > I would like to access just one dimension of a multi-column and > > rows array. Something like if A(i,j) is an array with values for i > > and j how about A(I,j) being just a one-dimensional array? I could > > be say the number of elements i. Thanks. > VB doesn't natively allow that, but it's possible to fool VB into > thinking it does, depending on how the array is ordered. But it's a > hack, and has to be approached carefully. > Basically, the idea is to create a blank single-dim array, and > manipulate its SafeArray structure to have the correct bounds, and a > pointer to the data of interest. > It's not for the inexperienced or the careless, though. You can get > things bollixed up pretty well if you miss a step, or do something you > shouldn't. > -- > Jim Mack > MicroDexterity Inc > www.microdexterity.com
|
Fri, 17 Dec 2010 05:27:35 GMT |
|
|
Larry Serflate #9 / 25
|
Does VB have something that can return just a column of a multi-column array?
Quote: > > Basically, the idea is to create a blank single-dim array, and > > manipulate its SafeArray structure to have the correct bounds, and a > > pointer to the data of interest. > That sounds neat. A 1-dim array of pointers to the second column of the > multi-dim array A Interesting. Thanks for the tip.
I could be mistaken, but I don't think Jim meant an a array of pointers. The way I understood it was that you would create the 2D array which is laid out in memory one row after the other: (Using letters for illustration) Dim data(1 to 3, A to C) Would lay out in memory like: 1A 2A 3A 1B 2B 3B 1C 2C 3C So that with a second 1D array of 3 elements, altering the descriptor of the variable (its SafeArray structure) you could make VB think that the data for that 1D array starts at 1A to access the first row, or change it to 'point to' 1B for the second row, or 1C for the third. But, consider what might happen if you somehow let that 1D array go out of scope while its pointing to the middle of your 2D array. VB is going to want to reclaim the memory for that 1D array, right? LFS
|
Fri, 17 Dec 2010 08:08:12 GMT |
|
|
Jim Mac #10 / 25
|
Does VB have something that can return just a column of a multi-column array?
Quote:
> That sounds neat. A 1-dim array of pointers to the second column of > the multi-dim array A Interesting. Thanks for the tip.
What Larry said. I didn't want to expand on the technique or its pitfalls unless you expressed an interest, but the main gotcha is indeed that you have to maintain both arrays in scope, and unwind your changes to the 1-D proxy array before killing it. But if you are careful, and you do understand what you're doing, it can be a very fast and powerful addition to your bag of tricks. -- Jim Mack MicroDexterity Inc www.microdexterity.com Quote: > _________________________________ > "Jim Mack" wrote...
>>> I would like to access just one dimension of a multi-column and >>> rows array. Something like if A(i,j) is an array with values for i >>> and j how about A(I,j) being just a one-dimensional array? I could >>> be say the number of elements i. Thanks. >> VB doesn't natively allow that, but it's possible to fool VB into >> thinking it does, depending on how the array is ordered. But it's a >> hack, and has to be approached carefully. >> Basically, the idea is to create a blank single-dim array, and >> manipulate its SafeArray structure to have the correct bounds, and >> a pointer to the data of interest. >> It's not for the inexperienced or the careless, though. You can get >> things bollixed up pretty well if you miss a step, or do something >> you shouldn't. >> -- >> Jim Mack >> MicroDexterity Inc >> www.microdexterity.com
|
Fri, 17 Dec 2010 08:18:19 GMT |
|
|
Bill McCarth #11 / 25
|
Does VB have something that can return just a column of a multi-column array?
Hi George,
Quote: > That sounds neat. A 1-dim array of pointers to the second column of the > multi-dim array A Interesting. Thanks for the tip.
You might want to have a look at : http://vb.mvps.org/articles/bb200007.asp Then you can grab the SafeArray wrapper class from Karl's MapFile sample.
|
Fri, 17 Dec 2010 11:12:53 GMT |
|
|
senn #12 / 25
|
Does VB have something that can return just a column of a multi-column array?
http://vb.mvps.org/articles/bb200007.asp If you read from this site-page, be aware of a typo, Below one, and possibly more in this page. Typos in learning material is a frustrating acquaintance.: Following the pvData member is the series of SafeArrayBounds, which specify the lower bound (lLBound) and count of elements (cElements) for each dimension. In VB, the UBound function returns cElements minus lLBound minus one for the given dimension. That's why a variant array, or a param array with no elements, returns minus one for its Ubound.
|
Fri, 17 Dec 2010 19:01:45 GMT |
|
|
Bill McCarth #13 / 25
|
Does VB have something that can return just a column of a multi-column array?
Quote: > http://vb.mvps.org/articles/bb200007.asp > If you read from this site-page, be aware of a typo, > Below one, and possibly more in this page. Typos > in learning material is a frustrating acquaintance.:
Are you referring to the erroneous comma ? Quote: > Following the pvData member is the series of SafeArrayBounds, > which specify the lower bound (lLBound) and count of elements > (cElements) for each dimension. In VB, the UBound function > returns cElements minus lLBound minus one for the given > dimension. That's why a variant array, or a param array with > no elements, returns minus one for its Ubound.
|
Fri, 17 Dec 2010 19:15:00 GMT |
|
|
senn #14 / 25
|
Does VB have something that can return just a column of a multi-column array?
Quote: > http://vb.mvps.org/articles/bb200007.asp > If you read from this site-page, be aware of a typo, > Below one, and possibly more in this page. Typos > in learning material is a frustrating acquaintance.: > Following the pvData member is the series of SafeArrayBounds, > which specify the lower bound (lLBound) and count of elements > (cElements) for each dimension. In VB, the UBound function > returns cElements minus lLBound minus one for the given > dimension. That's why a variant array, or a param array with > no elements, returns minus one for its Ubound.
For example: if the cElement was 10 and the lLBound was 5, the above would return an UBound of 4 (cElements minus lLBound minus 1). A UBound of 4 cannot contain 10 elements. A correction of cElements + ILBound - 1 would return a UBound of 14
|
Fri, 17 Dec 2010 21:00:39 GMT |
|
|
Bill McCarth #15 / 25
|
Does VB have something that can return just a column of a multi-column array?
Quote: > A correction of > cElements + ILBound - 1
Yep, thanks.
|
Fri, 17 Dec 2010 21:19:42 GMT |
|
|
Page 1 of 2
|
[ 25 post ] |
|
Go to page:
[1]
[2] |
|