msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Fill an array vector-style ?

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax
Author Message
jonigr



Joined: 04 Oct 2007
Posts: 3

PostPosted: Sat Feb 16, 2008 1:12 am    Post subject: Fill an array vector-style ? Reply with quote

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
View user's profile Send private message
jonigr



Joined: 04 Oct 2007
Posts: 3

PostPosted: Sat Feb 16, 2008 1:54 am    Post subject: Re: Fill an array vector-style ? Reply with quote

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
View user's profile Send private message
Jeff Johnson



Joined: 04 Oct 2007
Posts: 1327

PostPosted: Mon Feb 18, 2008 2:34 pm    Post subject: Re: Fill an array vector-style ? Reply with quote

"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
View user's profile Send private message
Ralph



Joined: 04 Oct 2007
Posts: 4148

PostPosted: Mon Feb 18, 2008 2:58 pm    Post subject: Re: Fill an array vector-style ? Reply with quote

"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
View user's profile Send private message
Larry Serflaten



Joined: 04 Oct 2007
Posts: 2644

PostPosted: Tue Feb 19, 2008 11:55 pm    Post subject: Re: Fill an array vector-style ? Reply with quote

"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 Wink

HTH
LFS
Back to top
View user's profile Send private message
Jim Mack



Joined: 04 Oct 2007
Posts: 735

PostPosted: Wed Feb 20, 2008 1:14 am    Post subject: Re: Fill an array vector-style ? Reply with quote

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
View user's profile Send private message
Larry Serflaten



Joined: 04 Oct 2007
Posts: 2644

PostPosted: Wed Feb 20, 2008 1:16 am    Post subject: Re: Fill an array vector-style ? Reply with quote

"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.....

Sad
LFS

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group