assigning arrays in VB

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2005
Posts: 41
Reputation: mrmike is an unknown quantity at this point 
Solved Threads: 0
mrmike mrmike is offline Offline
Light Poster

assigning arrays in VB

 
0
  #1
May 26th, 2005
Just got a quick question

I know how to assign arrays using the DIM command, but I want to give the array a set of constant figures that will never change

Under Delphi I would use the following when declaring the array at startup

var
myarray :array[0..8] of integer = (value1, value2, value3, etc, etc)

how can I do this under VB6, I have looked at the MSDN help but it only shows on how to create the DIM array, and nothing about assigning a particular list.

I have set the line as

Dim myarray(8) as integer

but if I add '= (value1, value2, value3, etc, etc) it reports an error

does this mean I have to set each item individually on each line as follows

myarray(0)=value1
myarray(1)=value2
myarray(2)=value3

or is there an easier solution

thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: assigning arrays in VB

 
0
  #2
May 26th, 2005
The unfortunate answer is that VB ultimately only allows you to assign arrays as such. However, There is a minor solution that you can take into play. Depending on the data type, you can use different predefined functions in VB to return yourself arrays, but it's kind of crappy. There is a function, in VB4-6, Called "Array", but it will only assign variant data types, and to be perfectly honest with you, variant data types suck. They are huge, bulky, and make your program slower than needs be. They are really ugly variable, and should be avoided. You can use the function like this, however:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim theArray() As Variant
  2. theArray() = Array("Spring", "Summer", "Fall", "Winter")

If The Data Type Were Strings, You could get fancy, and use the split function, but it's not really the same concept as you were wanting:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. dim strArray as string
  2. strArray = split("summer;winter;spring;fall", ";")

Or You can create a function to handle this, which is probably the best means of doing so. It's a bit of a pain, compared to a language like Perl, or Delphi, where assigning values is fairly easy.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Function ArrayInt(ParamArray values() As Variant) As Integer()
  2. Dim i As Long
  3. ReDim res(0 To UBound(values)) As Integer
  4. For i = 0 To UBound(values)
  5. res(i) = values(i)
  6. Next
  7. ArrayInt = res()
  8. End Function

Let me know how this code works for you, and if you need any further assistance. I'll be glad to help.

The information referenced here, was taken from: devx.com, and the credit belongs to : Francesco Balena.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 41
Reputation: mrmike is an unknown quantity at this point 
Solved Threads: 0
mrmike mrmike is offline Offline
Light Poster

Re: assigning arrays in VB

 
0
  #3
May 26th, 2005
thanks for the info

changed the function to

Function mydata(ByVal item as integer, ParamArray valuelist() as variant)

and this works fine
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC