有没有办法打电话给&从类中的方法使用类类型? Vb.Net

时间:2011-03-03 21:18:50

标签: vb.net

您好,我将通过说我以前是一名asp开发人员并且正在学习oop编程/ Vb.Net

我正在研究3层架构并尽可能地抽象我的代码,因为我有一个非常大的内部网要转换。在我的业务层中,我使用管理方法定义了我的类。以下是我的一个课程的示例。

我的问题:我有没有办法通用引用类类型和对象类型,这样我就不必在整个类中继续引用类名/类型“ServiceRequest”。例如:

"Public Function GetItem(ByVal ID As Integer) As Me.GetType"? 

任何信息,如果这是可能的,我应该研究什么主题将不胜感激。如果我做了一些明显错误的话,请随意评论一般代码。感谢

Public Class ServiceRequest
    Inherits DataUtility

#Region "Properties"
    Public Property ReqID As Integer
    ' Public Property PrjID As Integer
    Public Property EntBy As String
    Public Property EntDate As DateTime
    Public Property ServiceType As String
    Public Property ResponseTime As String
    Public Property ServiceReason As String
    Public Property Comments As String
#End Region

#Region "Public Methods"
    Public Function Save() As Integer
        Return SaveRecord(GetParamsList(Me))
    End Function

    Public Function Delete(ByVal ID As Integer) As Boolean
        Return DeleteRecord(ID)
    End Function

    Public Function GetItem(ByVal ID As Integer) As ServiceRequest
        Dim retVal As ServiceRequest = New ServiceRequest()
        Dim sdr As SqlDataReader = GetRecord(ID)
        While sdr.Read()
            retVal = ParseDataToObj(sdr)
        End While
        Return retVal
    End Function

    Public Function GetList(ByVal ID As Integer) As List(Of ServiceRequest)
        Dim retVal As List(Of ServiceRequest) = New List(Of ServiceRequest)()
        Dim sdr As SqlDataReader = GetRecords(ID)
        While sdr.Read()
            Dim listObj As ServiceRequest = ParseDataToObj(sdr)
            retVal.Add(listObj)
        End While
        Return retVal
    End Function
#End Region

#Region "Helper Methods "
    Private Function GetParamsList(ByVal paramVal As ServiceRequest) As List(Of SqlParameter)
        'Sending Data to SQL Server - Stored Procedures
        Dim retVal As List(Of SqlParameter) = New List(Of SqlParameter)

        With paramVal
            retVal.Add(New SqlParameter("@ReqID", .ReqID))
            'retVal.Add(New SqlParameter("@CusID", .CusID))
            retVal.Add(New SqlParameter("@EntBy", .EntBy))
            retVal.Add(New SqlParameter("@ServiceType", .ServiceType))
            retVal.Add(New SqlParameter("@ResponseTime", .ResponseTime))
            retVal.Add(New SqlParameter("@ServiceReason", .ServiceReason))
            retVal.Add(New SqlParameter("@Comments", .Comments))
        End With
        Return retVal
    End Function

    Private Function ParseDataToObj(ByVal dr As SqlDataReader) As ServiceRequest
        ' Reading data and converts to object
        ' Single vs list - if boolean then get case types list

        Dim returnObjData As New ServiceRequest()
        With returnObjData
            .ReqID = MyBase.GetDataValue(Of Integer)(dr, "ReqID")
            '.CusID = MyBase.GetDataValue(Of Integer)(dr, "CusID")
            .EntBy = MyBase.GetDataValue(Of String)(dr, "EntBy")
            .EntDate = MyBase.GetDataValue(Of DateTime)(dr, "EntDate")
            .ServiceType = MyBase.GetDataValue(Of String)(dr, "ServiceType")
            .ResponseTime = MyBase.GetDataValue(Of String)(dr, "ResponseTime")
            .ServiceReason = MyBase.GetDataValue(Of String)(dr, "ServiceReason")
            .Comments = MyBase.GetDataValue(Of String)(dr, "Comments")
        End With
        Return returnObjData
    End Function
#End Region

End Class

1 个答案:

答案 0 :(得分:2)

如果我理解正确;你也许可以使用泛型。

Public Class classHolder(Of T)
    Private someValue As T

    Public Function GetItem(ByVal ID As Integer) As T
        Return someValue
    End Function
End Class

T是你想要的任何类型。