Results 1 to 9 of 9

Thread: How do work array???

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    6

    Talking

    Friend's,

    Please help me work's array in VB, how declarations variables array???

    Tnk's


  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    For starters, here is how you declare arrays:

    Code:
    Dim strArray() as string
    dim numArray() as integer
    dim varArray() as variant
    The above statements create empty arrays of the specified types. You can later resize them. If you want to declare the size of the arrays, you can use:

    Code:
    dim strArray(3) as string  'Array to hold 3 strings
    dim numArray(4,5) as integer  'Array to hold 4x5 integers, 4 rows and 5 columns

    There are various topics associated with arrays like the Base index, resizing the arrays etc. Either check out MSDN, get hold of a good VB book or come back to the forum for further clarifications.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Guest
    Arrays speed up the process a bit.

    Code:
    Private Sub Command1_Click()
    
        Dim x(12) As String
        x(0) = "M"
        x(1) = "a"
        x(2) = "t"
        x(3) = "t"
        x(4) = "h"
        x(5) = "e"
        x(6) = "w"
        x(7) = " "
        x(8) = "G"
        x(9) = "a"
        x(10) = "t"
        x(11) = "e"
        x(12) = "s"
        
        For I = LBound(x) To UBound(x)
        Print x(I)
        Next I
    
    End Sub

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    If you don't know the size of an array on declaring it your declare it as so:
    In a bas module
    Code:
    Option Explicit
    Public MyArr()
    
    Then later in your code, lets say you are reading a file using line input and then you want to store the lines in an array your would do this:
    
    Option Explicit
    'some event on the form
    
    Dim i As Integer, myVar as String
    Open ("C:\myfile.txt") for Input as #1
    While Not EOF(#1)
      ReDim Preserve MyArr(i)
      Input #1, myVar
      myArr(i) = myVar
      i = i + 1
    Loop
    Close #1
    
    To read the array into a list box
      For i = lBound(myArr) to Ubound(myArr)
        list1.addditem myArr(i)
      next i
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    Guest
    hmm, does microsoft produce VB in different countries' languages? ie french or german, instead of using english words such as Select Case... or If-then-else

    just a thought.

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    wossname

    How in anyone's name does that tie into Arrays?



    bty

    Do you know why a chair is called a chair and not a table?
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    VbWorld.net Has an excellent article about arrays, read it here:
    http://www.vb-world.net/articles/arrays/
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  8. #8
    Hyperactive Member
    Join Date
    Nov 2000
    Location
    Mexico City
    Posts
    306
    If you want to make a cycle into the array try this:

    dim int_I as integer
    dim int_J as integer
    dim a() as integer

    ...
    ...
    ...
    int_J=6
    redim a(int_J)

    for int_I=lbound(a) to ubound(a)
    ...
    ...
    next int_I

    By the way, if you´re using the preserve keyword, it can be done on the last dimension of the array.

  9. #9
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Matthew,

    On reading your reply I thought there was a mistake in the code. You declared the array of string with a dimension of 12. I took it to be the total number of elements in the array. Actually it is not so.

    This is another point of the arrays which I had failed to notice. And this is specific to VB.

    In all programming languages, when you declare an array you define the size of the array, i.e. the total number of elements in the array. But in VB you declare the Upper Bound of the array, i.e. the index of the last element in the array.

    Another interesting point is in this case the Option Base statement decides how many elements your array can hold. For e.g. in Matthew's example, Option Base 0 will store 13 elements in the array (Index 0 to Index 12) while Option Base 1 will store only 12 elements (Index 1 to Index 12).

    Are my observations correct?


    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

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