Results 1 to 10 of 10

Thread: Populating an array with initial values

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Sydney, Australia
    Posts
    74

    Question

    Does anybody know how I can assign a lot of values to an array at once?

    in C I would do some thing like
    int MyData[] = {1,1,2,3,7}

    is there any way I can do something similar in VB?


  2. #2
    Lively Member
    Join Date
    Mar 2000
    Posts
    81
    You can't initiate variables in VB...yet - that comes with version 7.

    Toot
    Some cause happiness wherever they go; others, whenever they go.

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    you can do something close though ..

    dim myString as string
    dim myArray

    mystring="1,1,2,3,4,5,6,7,8"

    myarray=split (mystring,",")

    this will populate the array with the string component (devided by the ",").

    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    For variant arrays you can do this:
    Code:
    Dim a as variant
    a=array(1,2,3,4,5)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Sydney, Australia
    Posts
    74
    hmmm

    I knew about the array function, but I must admit the split function was new to me. The problem is that in both cases I end up with a variant array. This would not be a problem as such if the initial data was all that i wanted in the array, but unfortunately I need to dynamically resize the array.

    Can I still do this with variant arrays?
    What if I wanted a multi dimensional array, can I still do it this way?

    thanks for any help


  6. #6
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    you can use

    redim preserve array (size)

    this will resize the array and keep the information

    or ...

    you can use dictionary objects and than you don't need to define a size, it is also more efficient with memory, where do you take the information from ? (a db ?)
    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2000
    Location
    Sydney, Australia
    Posts
    74
    will redim preserve work with variant arrays as well as with arrays of variants?

    The info doesn't come from a db, it depends on waht the user has entered while running the app

    I think the Dictionary object is exactly what I want though, thanks

  8. #8
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    I am not sure about the redim with variants,

    But the dictionary objects are great, are you o.k with those ?
    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Àrrays in variant are just the same as regular arrays except that you can use For each to enumerate them, overwrite them with other arrays and anything else, and they are slightly slower. I'm not sure, i don't have vb here to test but i think you can pass a regular array as a variant into a function and then set the array inside the function. You could pass all the items in a paramarray variant.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    New Member
    Join Date
    Oct 2000
    Posts
    10
    Just do this:
    Code:
    Dim MyArray() as long
    ReDim MyArray(1) as Long 'do this else will not work
    
    ReDim Preserve MyArray(NewSize)
    That will work


    theres a dictionary type in VB? goes to look that up

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width