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

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:

Dim theArray() As Variant
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:

dim strArray as string
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.

Function ArrayInt(ParamArray values() As Variant) As Integer()
    Dim i As Long
    ReDim res(0 To UBound(values)) As Integer
    For i = 0 To UBound(values)
        res(i) = values(i)
    Next
    ArrayInt = res()
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.

thanks for the info

changed the function to

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

and this works fine

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.