Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3938572
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:12:00+00:00 2026-05-20T00:12:00+00:00

The code below works for the class that I hard coded XCCustomers in my

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:12:00+00:00Added an answer on May 20, 2026 at 12:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that overloads operator new . The code below works fine under
I've made a class (code below) that handles the creation of a matching quiz
(please excuse that I didn't use aliases). I would like my query output to
The code below works. But if I comment out the line Dim objRequest As
So far i have got the code below which works lovely when trying an
See code below, for some reason it only works when I put a breakpoint
The code below is the code i am using. It works fine in thunderbird
The code below shows a sample that I've used recently to explain the different
In the code base I was maintaining I found this exact class, pasted below.
I've some simple code below that uses a ToggleButton.IsChecked property to set the Visibility

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.