tags:

views:

27

answers:

2

Hi, I have to put data into a dictinary, thus formed: Dictionary (Of Integer, List (Of RicercaNews))

but when I go to put the list in the dictionary, I always return the same record, as if it does not increase the index of the list.

This is the code:

ResultTable Dim As New DataTable () 
            Dictgestionalews Dim As New Dictionary (Of Integer, List (Of RicercaNews)) 
            Listacat Dim As New Dictionary (Of Integer, String) 
            Dim listaricerca As List (Of RicercaNews) 
            Dim As New ricnews RicercaNews () 
            Try 
                listacat Me.GetAllCategoryNews = () 
                Dim comm As DbCommand = GenericDataAccess.CreateCommand () 


                For Each ck As KeyValuePair (Of Integer, String) In listacat 
                    comm.CommandText = "SELECT tabNews.idNews, tabNews.titolo, tabNews.commenti, tabNews.DataInizio, tabNews.DataFine, tabNews.idcatnews" _ 
                    & "FROM WHERE tabNews tabNews.idcatnews =" & ck.Key 
                    resultTable GenericDataAccess.ExecuteSelectCommand = (comm) 

                    If Me.GetFigliCategory (ck.Key)> 0 Then 
                        listaricerca = New List (Of RicercaNews) 

                        For index As Integer = 0 To resultTable.Rows (). Count () - 1 

                            ricnews.DataFine resultTable.Rows = (). Item (index). Item ("EndDate") 
                            ricnews.DataInizio resultTable.Rows = (). Item (index). Item ("StartDate") 
                            ricnews.idnews resultTable.Rows = (). Item (index). Item ("idNews) 
                            ricnews.titolo resultTable.Rows = (). Item (index). Item ("title") 
                            ricnews.idcatnews resultTable.Rows = (). Item (index). Item ("idcatnews) 
                            listaricerca.Add (ricnews) 
                        Next 



                        dictgestionalews.Add (ck.Key, listaricerca) 
                    Else 
                        dictgestionalews.Add (ck.Key New List (Of RicercaNews) ()) 

                    End If 

                Next 


                Return dictgestionalews 

            Catch ex As Exception 
                Utilita.LogError (former) 

                Return Nothing 
            End Try 

Can you tell me what's wrong? Thanks

A: 

You need to reate a new instance of RicercaNews and store it in the variable ricnews on each iteration of your inner most loop. At present you're creating just one instance and changing it's properties on every iteration.

Also, I can't see what Me.GetFigliCategory(ck.Key) is doing but I suspect you don't want to skip the object construction if it returns 0. Instead shouldn't you create the new list if it returns 0 and then create the object and it to the list on every iteration, irresepective of whether Me.GetFigliCategory(ck.Key)` returned 0 or not?

Daniel Renshaw
A: 

hello and thanks for your answer, you say that I enter this statement

Dim As New ricnews RicercaNews ()

inside the loop like this:

For index As Integer = 0 To resultTable.Rows (). Count () - 1
                            **Dim As New ricnews RicercaNews ()**
                            ricnews.DataFine resultTable.Rows = (). Item (index). Item ("EndDate")
                            ricnews.DataInizio resultTable.Rows = (). Item (index). Item ("StartDate")
                            ricnews.idnews resultTable.Rows = (). Item (index). Item ("idNews)
                            ricnews.titolo resultTable.Rows = (). Item (index). Item ("title")
                            ricnews.idcatnews resultTable.Rows = (). Item (index). Item ("idcatnews)
                            listaricerca.Add (ricnews)
                        Next

function Me.GetFigliCategory (ck.Key) says if by chance the father of the children, because my db has the categories and subcategories, the category where there has children can not be eliminated

Please add any new information to your question by editing it. If you want to discuss an answer,please use the add comment feature associated with that answer. If an answer has been provided that solved your problem, please accept it by clicking the tick next to the answer.
Daniel Renshaw