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 8472019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:57:11+00:00 2026-06-10T16:57:11+00:00

Assuming I have an Object Car with one property: Model I would populate the

  • 0

Assuming I have an Object Car with one property: Model

I would populate the object using the database as follows:

Public Class Car

    Public Property Model() As String
        Get
            Return _Model
        End Get
        Set(ByVal value As String)
            _Model = value
        End Set
    End Property
    Private _Model As String

    Private Sub SetObjectData(ByVal theObjReader As System.Data.SqlClient.SqlDataReader)
        Try
            Me._Model = theObjReader("Model").ToString()
        Catch ex As Exception
            Throw New Exception("Unable to Initialize Car.")
        End Try
    End Sub

    Public Sub New(ByVal Car_ID As Integer)
        Dim connection As New SqlConnection(DBTool.DataConnectionString)
        Try
            Dim cmd As SqlCommand
            cmd = New SqlCommand("getCarByCar_ID", connection)
            cmd.CommandType = CommandType.StoredProcedure

            cmd.Parameters.AddWithValue("@Car_ID ", Car_ID)

            connection.Open()
            Dim objReader As SqlDataReader = cmd.ExecuteReader()

            Do While objReader.Read()
                SetObjectData(objReader)
            Loop

            objReader.Close()
            connection.Close()
        Catch ex As Exception
            connection.Close()
        End Try
    End Sub

End Class

Now, lets assume for the sake of the argument that this class is wrapped in a DLL.

What I wish to do is to inherit it, while expending my properties. For instance, lets say we want to have a new Object “Expensive_Car” with a new property “Price”.

How can I utilize the previous Car object and initialize this derived class in the most “organized” way without writing code twice? The field “Price” is saved in the same SQL table as the field “Model”. Of course we need to obtain the “Price” from the Database, just like we pulled Model.

My goal is to utilize existing classes and to expend them using new derived classes without changing the parent class at all. The SQL database structure can change using additional fields as needed.

Thanks!

Nick

  • 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-06-10T16:57:13+00:00Added an answer on June 10, 2026 at 4:57 pm

    You might want to consider one of the many ORMs out there like Entity Framework or NHibernate.

    If you really want a roll-your-own approach the simplest way from here is probably to add two protected overridable methods to the base class, one to create the SqlCommand object with the correct stored proc name and the other to load properties specific to derived classes.

    You should probably also wrap the creation of connection, command and data reader objects in Using statements to make sure everything gets cleaned up correctly.

    Here’s a c# example because my VB.Net is really rusty:

    public class Car
    {
      public Car(int carId) {
        CarId = carId;
        LoadProperties();
      }
    
      public int CarId { get; set; }
      public string Model { get; set; }
    
      protected virtual SqlCommand CreateCommand() {
        // Override this method in derived classes to call a different procedure.
        return new SqlCommand("getCarByCar_ID");
      }
    
      protected virtual void LoadMoreProperties(SqlDataReader reader)
      {
        // Override this method in derived classes to load additional properties.
      }
    
      private void LoadProperties() {
        using( var connection = new SqlConnection(DBTool.ConnectionString) ) {
    
          using( cmd = CreateCommand() ) {
            using( var reader = cmd.ExecuteReader() ) {
              // Read Car specific properties
    
              Model = // get model from reader.
    
    
              LoadMoreProperties(reader);
            }
          }
        }
      }
    }
    
    public class ExpensiveCar : Car {
    
      public decimal Price { get; set; }
    
      protected override SqlCommand CreateCommand() {
        return new SqlCommand("getExpensiveCarByCar_ID");
      }
    
      protected override void LoadMoreProperties(SqlDataReader reader) {
        Price = // get price from reader.
      }
    }
    

    It won’t compile but should give you the idea. You want to make just the parts that can change overridable – calling a different stored proc and loading the properties specific to the derived class.

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

Sidebar

Related Questions

Assuming I have some object like, with LOTS of properties: public class SomeObject {
assuming I have an object similar to this one: struct MenuDef { int titleResourceId;
Assuming I have an array of User objects where each object is actually one
Assuming I have some objects like this: Class NetworkSwitch { private String _name; String
Assuming I have an object Person with long id String firstName String lastName String
Assuming you have a string containing the name of a method, an object that
Using SQL Server 2008: assuming I have a valid geography object geog1 , converting
Assuming I have only the class name of a generic as a string in
Assuming I have a class like public class FooImpl { public void bar(){}; }
Assuming that I have objects similar to this: public class User { public int

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.