title: 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?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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"</p>
  
  <p>Thanks for your help.</p>
  
  <p>Rick</p>
  
  <pre><code>'+++++++++++++++++++++++++++++++
  
  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 &gt; 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
  </code></pre>
  
  <p>'+++++++++++++++++++++++++++++++</p>
  
  <p>' Below are the supporting classes if you need to see what else is happening:</p>
  
  <pre><code>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
  </code></pre>