开发者

List.Count Returns 0

I am attempting to read the results from a SQL query into a List(Of) and I can see the List.count while adding items to the List increments, however in another part of my code when I am attempting to read the List the List.Count returns 0.

My List is as follows:

Public Class roomList
    Public Sub New()
    End Sub

    Public Property eName() As String
        Get
            Return _eName
        End Get
        Set(ByVal value As String)
            _eName = value
        End Set
    End Property
    Public Property eID() As Integer
        Get
            Return _eID
        End Get
        Set(ByVal value As Integer)
            _eID = value
        End Set
    End Property
    Public Property eEmail() As String
     开发者_如何学Go   Get
            Return _eEmail
        End Get
        Set(ByVal value As String)
            _eEmail = value
        End Set
    End Property
    Private _eID As Integer
    Private _eName As String
    Private _eEmail As String
End Class

I am reading the SQL results into the list as follows: EDIT: this snippet of code is in it's own Sub().

Dim rooms As New List(Of roomList)    
While dr.Read
      rooms.Add(New roomList() With {.eID = dr.Item("mId"), .eName = dr.Item("roomName"), .eEmail = dr.Item("roomEmail")})
      MsgBox(rooms.Count)
    End While

I am then attempting to read the count of the List as follows: EDIT: this snippet of code is in it's own Sub().

Dim rooms As New List(Of roomList)()
MsgBox(rooms.Count)

Can anyone help me to understand why the List is returning a count of 0, and help me to fix the code so that the list is being correctly called?

I appreciate any assistance anyone is able to offer and if there is any further information you require please dont hesitate in contacting me.

Matt

EDIT: The two snippets of code in which I am attempting to read/write the List are in their own Sub's, should I instead be declaring a global variable?

The first Sub is as follows:

Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Try
            getRoomList()
            Dim rooms As New List(Of roomList)()
            Dim r As roomList
            MsgBox(rooms.Count)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Which calls a getRoomList() sub:

Private Sub getRoomList()
        Dim rooms As New List(Of roomList)
        Try
        ' SQL Query stuff goes here
                dr = sqlCmd.ExecuteReader
                While dr.Read
                    rooms.Add(New roomList() With {.eID = dr.Item("mId"), .eName = dr.Item("roomName"), .eEmail = dr.Item("roomEmail")})
                    'MsgBox(rooms.Count)
                End While
            End If
        Catch ex As Exception

        End Try
    End Sub


This works:

Dim rooms As New List(Of roomList)    
While dr.Read      
rooms.Add(New roomList() With {.eID = dr.Item("mId"), .eName = dr.Item("roomName"), .eEmail = dr.Item("roomEmail")})      
MsgBox(rooms.Count)    
End While
MsgBox(rooms.Count)

Eliminate the creation of a new room:

Dim rooms As New List(Of roomList)()
MsgBox(rooms.Count)

That above creates a new list object, which is in fact initially empty.

Per your edit

You mentioned

EDIT: The two snippets of code in which I am attempting to read/write the List are in their own Sub's, should I instead be declaring a global variable?

Because they are in 2 different sub routines, one instance is lost hence the count is now 0 because you've created a new instance. What you want to do is take the initial list and pass it to the sub routine / function in question. If you plan to modify that list pass it ByRef otherwise pass it ByVal. Something to this effect:

'code...
 PrintListCount(rooms)
'other code...

protected Sub PrintListCount(ByVal rooms as List(Of roomList))
MsgBox(rooms.Count)
end sub

This example is kind of scary as you really don't need to pass an entire list by value to print the count of the list..but I am just using it to show you how to pass the list rooms.

Per your second edit

Your getRoomList() function doesn't return a list. How do you expect to gather the count without a proper object returned? Change the sub to a function:

Private Function getRoomList() As List(Of roomList)        
Dim rooms As New List(Of roomList)        
Try        ' SQL Query stuff goes here                
dr = sqlCmd.ExecuteReader                
While dr.Read                    
rooms.Add(New roomList() 
With {.eID = dr.Item("mId"), .eName = dr.Item("roomName"), .eEmail = dr.Item("roomEmail")})                    
'MsgBox(rooms.Count)                
End While            
End If        
Catch ex As Exception        
End Try   
return rooms 
End Function

And in your load event:

Dim rooms as List(Of roomList)
rooms = getRoomList();

Now that you have a proper rooms object you can now say:

MsgBox(rooms.Count)

You will need to read up on passing objects, how functions work, and variable scope. Without this knowledge you are making it much harder on yourself.


Well this snippet:

Dim rooms As New List(Of roomList)()
MsgBox(rooms.Count)

will indeed always show 0, because you're creating a new list and declaring a separate variable called rooms.

You haven't shown where you're calling the code which does read the data (i.e. the previous snippet).

To be honest it's unclear where all of this code is executing. If you could present a short but complete program demonstrating the problem, that would help a lot. When we can only see snippets, it's hard to get a good sense of the overall program flow.


From the second code snippet, what makes you think you're using the same list? You created a New List(Of roomList) instance on a newly Dim'd variable. To fix this code, we need to know how the two snippets are related.


Some possibilities:

If you are declaring rooms list twice in the code, the 2nd declaration would create a new list of rooms of count 0 and basically wipe away the previous list. Based on what you have posted, that could be what is happening.

The reader could be returning 0 rooms if there are no rooms in the data source.

The rooms variable has the wrong scope. nNcrease it's scope to module, class, or higher so that both subs access the same variable, not two different ones.

Also, I would move the count out of the loop. Just count it at the end.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜