I hate to admit this but I am new to object oriented programming in VB.NET. I have a class object called Subscriber.vb which works OK but I'd like to create a "set" or list of these objects. Could someone please help me leverage the following code to create a list of the subscribers so a "consumer" could loop through this list of subscribers? Here is what I have so far:
Public Class Subscriber
Public Sub New(ByVal theSubscriberID As Int32)
Dim sConnDatabase As String = ConfigurationManager.ConnectionStrings("DatabaseConnString").ConnectionString
Dim connection As New SqlConnection(sConnDatabase)
Dim cmd As SqlCommand
Try
cmd = New SqlCommand("GetSubscriberInfo_v", connection)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@SubscriberID", theSubscriberID)
connection.Open()
Dim objReader As SqlDataReader = cmd.ExecuteReader()
Do While objReader.Read()
SetObjectData(objReader)
Loop
objReader.Close()
connection.Close()
Catch ex As Exception
Throw
End Try
End Sub
Private Sub SetObjectData(ByVal theObjReader As SqlDataReader)
Try
Me._ID = Convert.ToInt32(theObjReader("SubscriberID"))
Me._NameForLogon = theObjReader("SubscriberName").ToString()
Me._NameInFull = theObjReader("SubscriberNameFull").ToString()
Me._DaysUntilExpired = Convert.ToInt32(theObjReader("DaysUntilExpired"))
Me._SignupDate = theObjReader("SignupDate")
Me._ExpirationDate = theObjReader("ExpirationDate")
Me._SubscriberPhone = theObjReader("SubscriberPhone").ToString()
Me._MostRecentRenewal = theObjReader("MostRecentRenewal")
Me._CumulativeRevenue = Convert.ToDecimal(theObjReader("CumulativeRevenue"))
Me._NumberOfRenewals = theObjReader("NumberOfRenewals")
Me._SubscriptionStatusCode = theObjReader("SubscriptionStatusCode")
Me._SubscriptionStatus = theObjReader("SubscriptionStatus").ToString()
Me._NotificationStatusCode = theObjReader("NotificationStatusCode")
Me._NotificationStatus = theObjReader("NotificationStatus")
Catch ex As Exception
Throw
End Try
End Sub
End Class
I did not show the getters and setters. This has to be restricted to Visual Studio 2008 unfortunately. For a few reasons, we cannot upgrade this environment.
What would be the best practice here? Add a Public Class SubscriberList to the Subscriber.vb file or should it be a separate file? More importantly, I am stuck on how to take what I have an create a proper list. Then the caller would create an instance of the SubscriberList object. Please help me get started. Thanks.
EDIT: Here is what I came up with thanks to your idea (I'm thinking of adding some overloaded constructors which might filter the data some various ways...would that be a good practice?):
Public Class SubscriberList
Public Sub New()
Dim sConnDatabase As String = ConfigurationManager.ConnectionStrings("DatabaseConnString").ConnectionString
Dim connection As New SqlConnection(sConnDatabase)
Dim cmd As SqlCommand
Dim oSubscriberList As New List(Of Subscriber)
cmd = New SqlCommand("GetSubscriberInfo_v", connection)
cmd.CommandType = CommandType.StoredProcedure
connection.Open()
Dim objReader As SqlDataReader = cmd.ExecuteReader()
Do While objReader.Read()
Dim id As Integer = objReader("SubscriberID")
Dim s As Subscriber = New Subscriber(id)
oSubscriberList.Add(s)
Loop
objReader.Close()
connection.Close()
End Sub
End Class
New error trying to use:
Dim allSubscribers As New SubscriberList
For Each Subscriber In allSubscribers
' allSubscribers is not declared
Next
Why not declared ? Confused rookie mistake I am sure...
EDIT (Number 2): Changed name from SubscriberList to Subscribers plural & got this working (see below) - but I am very puzzled by the advice to remove the database connection and query from the constructor(s) and place in separate class(es). I was picturing adding overloaded constructors to Subscriber (and Subscribers). I cannot imagine how the constructors of each would get their respective data.
Public Class Subscribers
Implements IEnumerable(Of Subscriber)
#Region "properties"
Public List As New List(Of Subscriber)
#End Region
Public Function GetEnumerator() As IEnumerator(Of Subscriber) _
Implements IEnumerable(Of Subscriber).GetEnumerator
Return List.GetEnumerator()
End Function
Private Function GetEnumerator1() As IEnumerator _
Implements IEnumerable.GetEnumerator
Return List.GetEnumerator()
End Function
Public Sub New()
Dim sConnDatabase As String = ConfigurationManager.ConnectionStrings("DatabaseConnString").ConnectionString
Dim connection As New SqlConnection(sConnDatabase)
Dim cmd As SqlCommand
cmd = New SqlCommand("SELECT * FROM dbo.Subscriber_v", connection)
cmd.CommandType = CommandType.Text
connection.Open()
Dim objReader As SqlDataReader = cmd.ExecuteReader()
Do While objReader.Read()
Dim id As Integer = objReader("SubscriberID")
Dim s As Subscriber = New Subscriber(id)
List.Add(s)
Loop
objReader.Close()
connection.Close()
End Sub
End Class