如何在vb.net中创建强类型列表类

时间:2013-08-23 20:48:31

标签: vb.net constructor foreach instance generic-list

我讨厌承认这一点,但我是VB.NET中面向对象编程的新手。我有一个名为Subscriber.vb的类对象,它工作正常,但我想创建一个“集合”或这些对象的列表。有人可以请我帮助我利用以下代码创建订阅者列表,以便“消费者”可以遍历此订阅者列表吗?以下是我到目前为止的情况:

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

我没有展示吸气剂和二传手。不幸的是,这必须限于Visual Studio 2008。由于一些原因,我们无法升级此环境。

这里最好的做法是什么?将公共类SubscriberList添加到Subscriber.vb文件中,还是应该是单独的文件?更重要的是,我被困在如何采取我创建一个正确的列表。然后调用者将创建SubscriberList对象的实例。请帮我开始吧感谢。

编辑:这是我想出的结果,这要归功于你的想法(我正在考虑添加一些可能以某种方式过滤数据的重载构造函数......这会是一个好习惯吗?):

    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

尝试使用的新错误:

Dim allSubscribers As New SubscriberList
For Each Subscriber In allSubscribers
                       ' allSubscribers is not declared
Next

为什么不宣布?困惑的菜鸟错误我肯定...

编辑(第2号): 将名称从SubscriberList更改为Subscribers plural&得到了这个工作(见下文) - 但我很困惑的建议是从构造函数中删除数据库连接和查询并放在单独的类中。我正在想象将重载的构造函数添加到Subscriber(和Subscribers)。我无法想象每个构造函数如何获得各自的数据。

    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

1 个答案:

答案 0 :(得分: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