|
| Author |
Message |
jonigr
Joined: 04 Oct 2007 Posts: 3
|
Posted: Sat Feb 16, 2008 1:12 am Post subject: Fill an array vector-style ? |
|
|
Instead of
Dim MyArray(2) As Integer
MyArray(0) = 19
MyArray(1) = 36
MyArray(2) = 82
it seems so much more intuitive and efficient to declare
Dim MyArray(2) As Integer = (19,36,82)
In fact I found several examples on the web doing it this way, but whatever
I try (braces instead of brackets etc.) I keep getting a Compile error
“Expected: end of statement” message highlighting the “=”-sign.
What am I doing wrong? Thank you! (VB 6.3 / Excel 2002)
-Joni
Archived from group: microsoft>public>vb>syntax |
|
| Back to top |
|
 |
jonigr
Joined: 04 Oct 2007 Posts: 3
|
Posted: Sat Feb 16, 2008 1:54 am Post subject: Re: Fill an array vector-style ? |
|
|
Seems to work. Excellent! Thank you.
-Joni
"Ed" wrote:
> On Fri, 15 Feb 2008 20:12:02 -0800, jonigr
> wrote:
>
> >Instead of
> >
> >Dim MyArray(2) As Integer
> >MyArray(0) = 19
> >MyArray(1) = 36
> >MyArray(2) = 82
> >
> >it seems so much more intuitive and efficient to declare
> >
> >Dim MyArray(2) As Integer = (19,36,82)
> >
> >In fact I found several examples on the web doing it this way, but whatever
> >I try (braces instead of brackets etc.) I keep getting a Compile error
> >“Expected: end of statement” message highlighting the “=”-sign.
> >
> >What am I doing wrong? Thank you! (VB 6.3 / Excel 2002)
> >
> >-Joni
>
> I don't think that's possible in VB6.
>
> You could use the Array and Variant commands to do something similar
> tho,
>
> Dim MyArray() As Variant
> MyArray = Array(19, 36, 82)
>
>
> |
|
| Back to top |
|
 |
Jeff Johnson
Joined: 04 Oct 2007 Posts: 1327
|
Posted: Mon Feb 18, 2008 2:34 pm Post subject: Re: Fill an array vector-style ? |
|
|
"jonigr" wrote in message @microsoft.com...
> it seems so much more intuitive and efficient to declare
>
> Dim MyArray(2) As Integer = (19,36,82)
> What am I doing wrong?
You're assuming that VB supports this syntax, which it doesn't. Other than
the Array() function, which is not doing EXACTLY what you're looking for,
there is simply no way to fill an array in one shot in VB.
(And before someone says "You can use CopyMemory()," I maintain that you
have to copy from SOMETHING and that INITIAL something will have to be
filled element-by-element, so you can't avoid doing it at least ONCE.) |
|
| Back to top |
|
 |
Ralph
Joined: 04 Oct 2007 Posts: 4148
|
Posted: Mon Feb 18, 2008 2:58 pm Post subject: Re: Fill an array vector-style ? |
|
|
"Jeff Johnson" wrote in message@corp.supernews.com...
> "jonigr" wrote in message
> @microsoft.com...
>
> > it seems so much more intuitive and efficient to declare
> >
> > Dim MyArray(2) As Integer = (19,36,82)
>
> > What am I doing wrong?
>
> You're assuming that VB supports this syntax, which it doesn't. Other than
> the Array() function, which is not doing EXACTLY what you're looking for,
> there is simply no way to fill an array in one shot in VB.
>
> (And before someone says "You can use CopyMemory()," I maintain that you
> have to copy from SOMETHING and that INITIAL something will have to be
> filled element-by-element, so you can't avoid doing it at least ONCE.)
>
I'm surprised that someone hasn't brought up the exotica.
1) You can do this by reading a file with a Get that you have previous
filled with a Put statement.
[Warning Air Code!]
' Create a simple utility app to create and fill a file.
Dim MyArray(2) As Integer
MyArray(0) = 19
MyArray(1) = 36
MyArray(2) = 82
Dim hFile As Integer
hFile = FreeFile()
Open "\MyArray.xxx" _
For Binary As #hFile
Put #hFile, , MyArray
Close #hFile
----
' later in your app
Dim MyArray() As Integer
hFile = FreeFile()
Open "\MyArray.xxx" _
For Binary As #hFile
Get #hFile, , MyArray
Close #hFile
2) Karl's Resource method
http://vb.mvps.org/articles/ap199912.asp
3) For the truly ugly, place all the values in a single comma separated
string, then Split() it. Of course you will then have to use conversion
functions or trust VB's ETC or inherent Variant handling to insure they are
always treated as "Integers".
4) or - I'm sure there are many others I can't think of at the moment.
hth and not confuse
-ralph |
|
| Back to top |
|
 |
Larry Serflaten
Joined: 04 Oct 2007 Posts: 2644
|
Posted: Tue Feb 19, 2008 11:55 pm Post subject: Re: Fill an array vector-style ? |
|
|
"Jeff Johnson" wrote
> (And before someone says "You can use CopyMemory()," I maintain that you
> have to copy from SOMETHING and that INITIAL something will have to be
> filled element-by-element, so you can't avoid doing it at least ONCE.)
The idea behind the technique is to do the work at design time, or compile time,
rather than looping in the executing code, eg:
Dim MyArray() As Integer = {19, 36, 82}
In the above line of code the values of the array had to be declared and typed in.
That work was done in the IDE, whereas in the executable code they would all fall
into place.
CopyMemory can be used to move them into place after some initial work has
been done in the IDE _without_ having to fill a structure element-by-element.
Consider:
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _
(ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Private Sub Command1_Click()
Const Data = "$R"
Dim MyArray(0 To 2) As Integer
Dim i As Long
' Fill array
CopyMemory VarPtr(MyArray(0)), StrPtr(Data), 6
' View data
For i = 0 To 2
Debug.Print MyArray(i)
Next
End Sub
While the example here only works with Byte values, a small bit of tweaking will
allow that same concept to work with values in the range of Integers and Longs
and others. (Left for the experimentation of the reader
HTH
LFS |
|
| Back to top |
|
 |
Jim Mack
Joined: 04 Oct 2007 Posts: 735
|
Posted: Wed Feb 20, 2008 1:14 am Post subject: Re: Fill an array vector-style ? |
|
|
Larry Serflaten wrote:
>
> Consider:
>
> Private Declare Sub CopyMemory Lib "kernel32.dll" Alias
> "RtlMoveMemory" _
> (ByVal Destination As Long, ByVal Source As Long, ByVal Length
> As Long)
>
> Private Sub Command1_Click()
> Const Data = "$R"
> Dim MyArray(0 To 2) As Integer
> Dim i As Long
>
> ' Fill array
> CopyMemory VarPtr(MyArray(0)), StrPtr(Data), 6
>
> ' View data
> For i = 0 To 2
> Debug.Print MyArray(i)
> Next
>
> End Sub
How's that work out for values in the range of, say, 130 to 140?
Repeat after me: strings are not suitable for binary data... (-:
--
Jim |
|
| Back to top |
|
 |
Larry Serflaten
Joined: 04 Oct 2007 Posts: 2644
|
Posted: Wed Feb 20, 2008 1:16 am Post subject: Re: Fill an array vector-style ? |
|
|
"Jim Mack" wrote
> > ' Fill array
> > CopyMemory VarPtr(MyArray(0)), StrPtr(Data), 6
>
> How's that work out for values in the range of, say, 130 to 140?
You're quite right. Due to the example data, I completely over looked
the unimess problems.
Suffice it to say it will have limited use, under specific conditions.....
LFS
|
|
| Back to top |
|
 |
|
|