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
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:
Eliminate the creation of a new room:
That above creates a new list object, which is in fact initially empty.
Per your edit
You mentioned
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
ByRefotherwise pass itByVal. Something to this effect: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:And in your load event:
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.