Results 1 to 6 of 6

Thread: Accessing Array Elements

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    22

    Question Accessing Array Elements

    I don't know why, but I can't seem to figure this out, it doesn't make any sense to me.

    I create an array like this...

    Code:
        Dim strArray() as String
        Dim strContents as String
    
        strContents = joe,john,jimmy
    
        strArray = Split(strContents, ",")
    How do I access each element in the array I try this...

    txtName.Text = strArray(0)

    and I get script out of range or something. I tried strArray(1) and I get the same thing. Can vb6 not directly access the elements? Every tutorial I see online only teaches how to display the elements with a for loop. I can't find a single tutorial that teaches how to just access one element when needed.

    What am I doing wrong here?

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Accessing Array Elements

    You have to use "s to assign the data to the string
    Code:
      Dim strArray() as String
        Dim strContents as String
    
        strContents = "joe,john,jimmy"
    
        strArray = Split(strContents, ",")
    
       Text1.Text=strArray(1)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    22

    Re: Accessing Array Elements

    I figured it out, sorry for the thread. I first was using the wrong array I had another array with almost the same name. Stupid me for not making my arrays way different in name. The second thing I was doing wrong was using commas since when reading from a text file it will only read till the first comma. I am using : instead and it works great even reading the contents from a file.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Accessing Array Elements

    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Accessing Array Elements

    You can read commas from a text file just fine, you just need to do it differently.

    Input # will read up to a comma or a line break assuming of course that the comma is not contained within "s in which case it is considered part of the data and will be read.
    Line Input # reads a full line up to a line break, commas included.

    There are other methods that can read any number of bytes up to the entire file at once.

    You just need to use the right method for the job at hand.

  6. #6
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Accessing Array Elements

    How do I access each element in the array I try this...

    txtName.Text = strArray(0)
    following way you can access each of the element
    Code:
    Private Sub Form_Load()
      Dim strArray() As String
        Dim strContents As String
    
        strContents = "joe,john,jimmy" 'it is string so it needs to be in ""
    
        strArray = Split(strContents, ",")
        Text1 = strArray(0)
        Text2 = strArray(1)
        Text3 = strArray(2)
    End Sub

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