Results 1 to 9 of 9

Thread: looping to assign an array [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    looping to assign an array [RESOLVED]

    I have an array array(2) that will store 3 values. - 0,1, and 2 (that IS right, i think)

    I need to assign array(0) a value that is returned by an indexof() function and repeat this two more times.

    how can I make the index number increase by one when assigning the array?

    array(0) = first occurance
    array(1) = 2nd
    array(2) - 3rd

    Any suggestions?
    Last edited by Andy; Feb 11th, 2004 at 09:54 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Note : VB.NET , C#.NET arrays are zero-based . Meaning the first element is 0 , next is 1 . You can iterate through the array by assigning the index property of the array to get the specific element .


    Example :
    All elements in the array are initialized with zero . Note how it adds 1 to each element .
    VB Code:
    1. Private Sub anotherTest()
    2.         Dim myar(2) As String
    3.  
    4.         For i As Integer = 0 To 2
    5.             'Here you can assign whatever to your array elemnts
    6.             'Like this
    7.             myar(i) += i
    8.             MessageBox.Show(myar(i))
    9.         Next
    10.     End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    what I tried was close. I had the the syntax a bit wrong! I am starting to get this@!!@

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Do you mean adding elements to the array after its declared? In other words changing it from 3 elements to 4 and so on. If that is what you mean than you can't declare it a fixed size at the begining but its easier to just use an ArrayList object instead. Otherwise if you are just talking about assigning values to existing elements then Pirates code should work fine.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    right. I know all I need is 3 so I am going to assign 0,1 and 2 unknown values. Pirate's been a BIG help lately.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Pirate's the bomb!

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    .................. ...................

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    great. I got my array to get the needed info. What I'm thinking now is to make this code into a function. More code will take the integers that the array is assigned and extract strings out of another string. Can I return more than one thing from a function?

    ie:

    return strArtist, strAlbum, strTrackNum
    (is that even how to do it?)

    Here is what I have so far if anyone cares to peruse it:
    Code:
    Public Function FindDashes(ByVal str) As Integer
    
            'declare the variables first
            Dim start, at, [end], count, myar(3), i As Integer
    
            'initialize the variables
            [end] = str.Length
            start = 3
            count = 0
            at = 0
            i = 0
    
            'go through the string and find and mark all the dashes
            While start <= [end] AndAlso at > -1 ' start+count must be a position within -str-.
                count = [end] - start
                at = str.IndexOf("-", start, count)
                myar(i) = at
    
                If at = -1 Then
                    Exit While
                End If
    
                'erase this, it's just for debugging
                Console.WriteLine("{0}", at)
                Console.WriteLine("myar(" & i & ") = " & myar(i))
    
                i = i + 1
                start = at + 1
            End While
            Return strArtist, strAlbum, strTrackNum
    
        End Function
    keeping in mind that the code to get the string info hasn't been added yet.

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    No , you can't return more than one value at a time . This can be done in C# , but alternatively , use ArrayList or string array to return the extracted strings that you mentioned . For the rest , I'm not sure what you mean exactly .

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