Question accessing array values

svibuk

Active member
Joined
Jul 15, 2008
Messages
31
Programming Experience
1-3
VB.NET:
 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
 
Hi,

You cannot do it the way you currently have your code structured.

The first thing to realise is that for each Read of a DataReader you are returning a single record of multiple fields from your data source. You therefore need to create a representation of that record in your project using either a structure or a custom type.

Each field of each record is then added to the defined fields in your defined custom type which can then be read back when you need. See this example in which I use a structure to represent your data source record:-

VB.NET:
Private Structure MyRec
  Public Property CName As String
  Public Property Email As String
End Structure

VB.NET:
Dim arryname As ArrayList
 
While m_reader.Read
  If Not m_reader("cname") = "" Then
    Dim CurrentRec As New MyRec
    With CurrentRec
      .CName = m_reader("cname")
      .Email = m_reader("mail")
    End With
    arryname.Add(CurrentRec)
  End If
End While
 
For Each item As MyRec In arryname
   MsgBox(item.CName & " - " & item.Email)
Next

Hope that helps.

Cheers,

Ian
 
thanks


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
 
Last edited:
According to the documentation, an IndexOutOfRangeException occurs when:
No column with the specified name was found.
So, your query is either not returning a column named "cid" or a column named "cname".
 
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
 
What is 'dictionary'? Is every item in it a Dictionary(Of String, String)? I very much doubt it and the error message backs that up. Each item is apparently a String.
 
Dim dictionary As New Dictionary(Of String, String)
While m_readersc.Read
If Not m_readersc("cname") = "" Then
'MsgBox(m_readersc("cid").ToString)
dictionary.Add(m_readersc("cid").ToString, m_readersc("cname").ToString)
End If
End While
 
Hi,

No, you have got it all mixed up!

Firstly, your values "cid" and "cname" are field names that are returned by your Reader when you interrogated your data source. What is stored in the Dictionary is not the Field Names of the Reader but the values that were returned by the Field Names.

Secondly, I think you need to learn more about containers in general and what can be held within a container. In the case of the Dictionary have a look here:-

Dictionary(TKey, TValue) Class (System.Collections.Generic)

In your case what you have done is save the value of CID in the Key Element of the Dictionary and the CNAME value in the Value Element of the Dictionary.

When you then create a for loop in the way you have you need to create an element of type KeyValuePair(Of Type, Type) to then be able to access the information in the Dictionary.

To demonstrate what you are doing try this:-

VB.NET:
Dim myDict As New Dictionary(Of String, String)
 
myDict.Add("MyKeyValue", "This is the string associated with the MyKeyValue")
 
For Each myDictElement As KeyValuePair(Of String, String) In myDict
  MsgBox(myDictElement.ToString)
  MsgBox(myDictElement.Key)
  MsgBox(myDictElement.Value)
Next

Hope that helps.

Cheers,

Ian
 
thanls a lot.
yes u r right as i need to knw a lot and indepthh abt containers

any link u culd provide me whih will give a detailed info abt dictionary , containers & how it can be used

whts the difference in

Dim values As New List(Of Dictionary(Of String, String))
Dim dictionary As New Dictionary(Of String, String)
 
Hi,

Firstly, Just use the MSDN help library's and search for information on Lists, Dictionary's, Hash Tables, Stacks, Queues, ArrayLists etc. This will give you all the information you need.

With regards to your comparison question. consider the SECOND declaration first, being:-

"Dim dictionary As New Dictionary(Of String, String)" - In the real world you can consider this to resemble a real life Dictionary i.e. every word in a Dictionary is an entry in the dictionary variable with the word being the key and the explanation being the value.

If you then consider your first declaration being, " Dim values As New List(Of Dictionary(Of String, String))", you can think of this as getting multiple Dictionary's explained above and piling them one of top of each other. I would try and steer away from this sort of thing until you have a lot more experience with the different collections available to you in the .NET Framework.

Hope that helps.

Cheers,

Ian
 
Back
Top