SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    retriving array values

    Code:
     While m_reader.Read
                If Not m_reader("cname") = "" Then
                    arryname.Add(m_reader("cname"))
     arrymail.Add(m_reader("mail"))
                End If
            End While
    
    
     For Each item As String In arryname
    
            Next
    i hve the above code in which i am adding the fields 2 array
    for retriving the values of each field i will need to execute the for loop twice
    how can i retive values of both array items using a single loop

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,827
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Since you seem to be using a very basic array for both items, you could do this.

    Code:
    Dim item As Integer
    For item As Integer = 0 To arryname.Length - 1
      'arryname(item)
      'arrymail(item)
    Next
    And to cover any rare situations where the array lengths may end up different
    Code:
    Dim item As Integer
    For item As Integer = 0 To arryname.Length - 1
      If item < arryname.Length Then
        'arryname(item)
      End If
    
      If item < arrymail.Length Then
        'arrymail(item)
      End If
    Next
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by svibuk View Post
    Code:
     While m_reader.Read
                If Not m_reader("cname") = "" Then
                    arryname.Add(m_reader("cname"))
     arrymail.Add(m_reader("mail"))
                End If
            End While
    
    
     For Each item As String In arryname
    
            Next
    i hve the above code in which i am adding the fields 2 array
    for retriving the values of each field i will need to execute the for loop twice
    how can i retive values of both array items using a single loop
    refercing elow but i get error in .add , error at from

    http://stackoverflow.com/questions/4...255327#4255327

  4. #4
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tried using
    Dim dictionary As New Dictionary(Of String, String)

    Dim m_readersc As SqlDataReader = cmdsc1.ExecuteReader()
    While m_readersc.Read
    If Not m_readersc("cname") = "" Then
    dictionary.Add(m_readersc("cid"), m_readersc("cname"))
    End If
    End While


    errror index out of range

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,827
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    On which line.... I assume it is referring to your m_readsc("cid") or m_readsc("cname") as your current code isn't trying to read from the Dictionary at this point.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  6. #6
    SitePoint Guru
    Join Date
    Dec 2008
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the abovr errr was due to a blank field. now i get thee belowerror

    Value of type 'String' cannot be converted to 'System.Collections.Generic.Dictionary(Of String, String)'.

    For Each value As Dictionary(Of String, String) In dictionary
    Dim id As String = value("cid")
    Dim description As String = value("cname")
    Next

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,827
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Use your debugger, it should make it obvious which of those arrays have an index out of range and it won't be the dictionary.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


Bookmarks

Posting Permissions

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