jsam
jsam

Reputation: 11

Is there a way to call & use a classes type from methods within the class? Vb.Net

Hello I will preface this by saying Im previously an asp developer and am learning oop programming/Vb.Net

Im working on a 3 tier architecture and trying to abstract my code as much as possible since I have a very large intranet to convert. In my business layer I am defining my classes with management methods. Below is an example of one of my classes.

My question: Is there a way for me to genericaly refer to the class type and object type so that I dont have to continualy refer to the class name/type "ServiceRequest" throughout the class. For example something like:

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

Any info if this is possible and what topic i should research would be appreciated. Feel free to comment on the code in general if im doing something glaringly wrong. Thanks

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

Upvotes: 1

Views: 159

Answers (1)

Rob P.
Rob P.

Reputation: 15071

If I'm understanding correctly; you might be able to use Generics.

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 is whatever type you want it to be.

Upvotes: 2

Related Questions