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

  • SEARCH
  • Home
  • 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 905489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:13:39+00:00 2026-05-15T16:13:39+00:00

I am currently playing around with VB.Net using Visual Studio 2010 (.Net 4.0) and

  • 0

I am currently playing around with VB.Net using Visual Studio 2010 (.Net 4.0) and I need your help to create a collection class (let’s call it ‘cl_MyCollection’) with a constructor that accepts two arguments:

  1. An SqlDataReader object
  2. Some means of pointing to a custom class I have created

The scenario is; I have five database tables in SQL 2000 and five VB.Net custom classes.
Each custom class contains a variable for each field of the table the class represents.
For example: “tbl_User” has 2 fields “User_ID” and “User_Name” and tbl_Family” has 3 fields “Family_ID”, “Family_Name” and “Family_Location”.
Here is the VB.Net custom class I have created intended to read and store a single record of the “tbl_User” table:

Imports System.Data.SqlClient

Public Class cl_table_User
    Property User_ID As Integer
    Property User_Name As String

    Public Sub New(ByRef rdr As SqlDataReader)
        Me.User_ID = rdr("User_ID)
        Me.User_Name = rdr("User_Name")
    End Sub
End Class

I have created similar classes for the other database tables.

The idea is to perform an SQL statement and use the results to instantiate my intended collection class (‘cl_MyCollection’).
I was thinking something like this: (‘conn’ is an ‘SqlConnection’ object)

Dim cmd As SqlCommand = New SqlCommand("Select * From tbl_Users", conn)
Dim rdr As SqlDataReader = cmd.ExecuteReader()
Dim aCollection as new cl_MyCollection(rdr, "tbl_Users")

and later when I want to use “tbl_Family”:

Dim cmd As SqlCommand = New SqlCommand("Select * From tbl_Family", conn)
Dim rdr As SqlDataReader = cmd.ExecuteReader()
Dim aCollection as new cl_MyCollection(rdr, "tbl_Family")

As you can see the third line is passing two arguments, the SQLDataReader and some means of pointing to a custom class I have created. The constructor of this class will then iterate through the results and create an object of the custom class I created.

I want the second argument to (somehow) make it decide which custom class to use. The ‘somehow’ is why I am here asking for your help.

The only way I can think this will work is by setting up a select case and list each custom class and then create an object of the custom class but I am hoping there is a better way. Currently I have it set up like this:

Public Class cl_MyCollection
    Private records As New Collection

    Public Sub New(rdr As SqlDataReader, selectedClass as string)

         Select Case selectedClass
            Case "tbl_User"

                While (rdr.Read())
                    Dim thisRecord as new cl_table_User(rdr)
                    records.Add(thisRecord)
                End While

            Case "tbl_Family"

                While (rdr.Read())
                    dim thisRecord as new cl_table_Family(rdr)
                    records.Add(thisRecord)
                End While             

         End Select

    End Sub
End Class

Ideally I would like the following code to work, or at least the idea it represents:

Public Class cl_MyCollection
    Private records As New Collection

    Public Sub New(rdr As SqlDataReader, selectedClass as TYPE)

        While (rdr.Read())
            Dim thisRecord as new selectedClass(rdr)
            records.Add(thisRecord)
        End While

    End Sub
End Class

Is this possible? or do I need to stick with the select case?

P.S Sorry for the length of this post.
Edit: Fixed typo, the select case now points to the second argument (‘selectedClass’) as originally intended.

  • 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-15T16:13:40+00:00Added an answer on May 15, 2026 at 4:13 pm

    If you want to make things “simpler” and stay away from XML and ORM, you can use generics.

    http://msdn.microsoft.com/en-us/library/512aeb7t.aspx

    Above link tells about generics, but only examples in C#.

    To use a generic in VB.NET it uses (Of type1, type2) instead of <type1, type2>

    Otherwise follow Mitch Wheat’s suggestion. ORM is very simple once you know how to use it.

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

Sidebar

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.