Rick Flow
Rick Flow

Reputation: 27

VB.NET CType: How do I use CType to change an object variable "obj" to my custom class that I reference using a string variable like obj.GetType.Name?

The code below works for the class that I hard coded "XCCustomers" in my RetrieveIDandName method where I use CType. However, I would like to be able to pass in various classes and property names to get the integer and string LIST returned. For example, in my code below, I would like to also pass in "XCEmployees" to my RetrieveIDandName method. I feel so close... I was hoping someone knew how to use CType where I can pass in the class name as a string variable.

Note, all the other examples I have seen and tried fail because we are using Option Strict On which disallows late binding. That is why I need to use CType. I also studied the "Activator.CreateInstance" code examples to try to get the class reference instance by string name but I was unable to get CType to work with that.

When I use obj.GetType.Name or obj.GetType.FullName in place of the "XCCustomers" in CType(obj, XCCustomers)(i) I get the error "Type 'obj.GetType.Name' is not defined" or "Type 'obj.GetType.FullName' is not defined"

Thanks for your help.

Rick

'+++++++++++++++++++++++++++++++

Imports DataLaasXC.Business

Imports DataLaasXC.Utilities

Public Class ucCustomerList


'Here is the calling method:

Public Sub CallingSub()

      Dim customerList As New XCCustomers()
      Dim customerIdAndName As New List(Of XCCustomer) = RetrieveIDandName(customerList, "CustomerId", " CustomerName")

     'This code below fails because I had to hard code “XCCustomer” in the “Dim item...” section of my RetrieveEmployeesIDandName method.
      Dim employeeList As New XCEmployees()
      Dim employeeIdAndName As New List(Of XCEmployee) = RetrieveIDandName(employeeList, "EmployeeId", " EmployeeName")

     'doing stuff here...

End Sub

'Here is the method where I would like to use the class name string when I use CType:
Private Function RetrieveIDandName(ByVal obj As Object, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair) 
    Dim selectedItems As List(Of IntStringPair) = New List(Of IntStringPair)

    Dim fullyQualifiedClassName As String = obj.GetType.FullName
    Dim count As Integer = CInt(obj.GetType().GetProperty("Count").GetValue(obj, Nothing))

    If (count > 0) Then
        For i As Integer = 0 To count - 1
            'Rather than hard coding “XCCustomer” below, I want to use something like “obj.GetType.Name”???
            Dim Item As IntStringPair = New IntStringPair(CInt(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerId").GetValue(CType(obj, XCCustomers)(i), Nothing)), _
     CStr(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerName").GetValue(CType(obj, XCCustomers)(i), Nothing)))

            selectedItems.Add(Item)
        Next
    End If

    Return selectedItems
End Function

End Class

'+++++++++++++++++++++++++++++++

' Below are the supporting classes if you need to see what else is happening:

Namespace DataLaasXC.Utilities

    Public Class IntStringPair
        Public Sub New(ByVal _Key As Integer, ByVal _Value As String)
            Value = _Value
            Key = _Key
        End Sub

        Public Property Value As String
        Public Property Key As Integer
    End Class
End Namespace

'+++++++++++++++++++++++++++++++

Namespace DataLaasXC.Business

    Public Class XCCustomer
        Public Property CustomerId As Integer
        Public Property CustomerName As String
    End Class
End Namespace

'+++++++++++++++++++++++++++++++

Namespace DataLaasXC.Business

    Public Class XCCustomers
        Inherits List(Of XCCustomer)

       Public Sub New()
           PopulateCustomersFromDatabase()
       End Sub

       Public Sub New(ByVal GetEmpty As Boolean)
       End Sub
    End Class
End Namespace

'+++++++++++++++++++++++++++++++

Namespace DataLaasXC.Business

    Public Class XCEmployee
        Public Property EmployeeId As Integer
        Public Property EmployeeName As String
    End Class
End Namespace

'+++++++++++++++++++++++++++++++

Namespace DataLaasXC.Business

    Public Class XCEmployees
        Inherits List(Of XCEmployee)

       Public Sub New()
           PopulateEmployeesFromDatabase()
       End Sub

       Public Sub New(ByVal GetEmpty As Boolean)
       End Sub
    End Class
End Namespace

Upvotes: 0

Views: 13785

Answers (3)

Binary Worrier
Binary Worrier

Reputation: 51711

From MSDN

CType(expression, typename)
. . .
typename : Any expression that is legal within an As clause in a Dim statement, that is, the name of any data type, object, structure, class, or interface.

This is basically saying you can't use CType dynamically, just statically. i.e. At the point where the code is compiled the compiler needs to know what typename is going to be.

You can't change this at runtime.

Hope this helps.

Upvotes: 2

hina
hina

Reputation: 1

ctype is used to convert in object type.

Upvotes: -1

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239664

Since List(Of T) implements the non-generic IList interface, you could change your function declaration to:

Private Function RetrieveIDandName(ByVal obj As System.Collections.IList, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)

And then your troublesome line would become (with also using the property name parameters):

Dim Item As IntStringPair = New IntStringPair(CInt(obj(i).GetType().GetProperty(idPropName).GetValue(obj(i), Nothing)), _
CStr(obj(i).GetType().GetProperty(namePropName).GetValue(obj(i), Nothing)))

Of course, you could still have the first parameter by Object, and then attempt to cast to IList, but that's up to you.

Upvotes: 1

Related Questions