1

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
4

1 回答 1

3

在 VB 中,您可以制作自定义对象的列表。

dim oSubscriberList as new List(of Subscriber)

然后您可以实例化新订阅者并将它们添加到列表中

oSubscriberList.add('add object here')

这可能是处理它的最简单、快速和肮脏的方法。您还可以创建一个单独的类来创建对象的集合。“最佳”实践,如果您想遵循 SOLID 编程原则并使用测试驱动开发,将指向您创建一个单独的集合类来处理它,但这不是必需的。

编辑:根据下面的评论

您不需要创建订阅者列表类。只需创建一个常规的订阅者列表并将它们添加到列表中即可。在您想要创建此列表的地方执行此操作(表单加载、某些事件等)

Dim oSubscriberList as NEW List(of Subscriber)

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()

while objReader.Read()
    oSubscriberList.Add(New Subscriber(objReader("SubscriberID"))
end while

'additional cleanup steps here

然后你可以像这样迭代你的列表:

For each sub as Subscriber in oSubscriberList
    'do something
Next
于 2013-08-23T21:01:28.250 回答