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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:58:58+00:00 2026-05-25T09:58:58+00:00

I have a database table which contains an ID column and a Name column.

  • 0

I have a database table which contains an ID column and a Name column. I am tasked with designing a program that accepts one of the IDs as an argument to Main().

Bold is edit 2

I need to use that ID which must exist in the database, to correspond to some code to run. Each row in the table corresponds to slightly different code, but a lot of them share a lot of code. I need a design that will minimize code duplication.

So far what I’ve developed is an abstract base class that has an abstract Int32 field ID to enforce derived classes having their corresponding ID in the database. That way I can reflect over the derived classes to find the one whose ID matches the Main() argument and instantiate that class. Then I just call the virtual methods from Main() which runs the most derived code that has been defined.

public abstract class Base {
    public abstract Int32 Id { get; }    
    public void Foo() {
        // Do something
    }
}

public class Derived {
    public override Int32 Id { get { return 42; } } 
    public void Foo() {
        // Do something more specific
    }
}

Does anyone have any better ideas how to achieve what I want? I like the idea of keeping the ID right in the class definition, but I’m open to changing that if it makes sense.

Thanks!

EDIT:

One thing I don’t like about this is that I have to reflect over each derived type and instantiate that type to check the ID. Does anyone have a better idea on how to do that?

  • 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-25T09:58:58+00:00Added an answer on May 25, 2026 at 9:58 am

    Instead of using a property to define the ID of the class, use a custom attribute. That way, you don’t have to instantiate the object to check what its ID is.

    When your program runs, it can scan the assembly for all classes with that attribute tag, and find the one with the matching ID, instantiate that class, and then run it’s Foo method. If you perform this kind of lookup multiple times per application run, you could instatiate all the classes with your custom attribute and then put them into a Dictionary to provide quick lookups by ID.

    Your code might look something like this:

    [AttributeUsage(AttributeTargets.Class)]
    public class CommandAttribute {
        public CommandAttribute(int id) {
            ID = id;
        }
    
        public int ID { get; private set; }
    }
    
    public abstract class Command {
        public abstract void Execute();
    }
    
    [Command(2)]
    public class MyCommand : Command {
        public override void Execute() {
            //Do something useful
        }
    }
    

    The other advantage of using a custom attribute is that you have to explicitly tag everything that is a candidate for being instantiated and executed by ID, rather than assuming than anything derived from your base class is a candidate. If you are sharing code between the classes, you might want to make a common base class for them that derives from your base class, but should not be instantiated or executed on its own.

    One thing I don’t understand is, what is the point of the “Name” field if the class you want to run is identified by the ID? If you can decide what the name of each ID is, then you could use the name field as the fully qualified type name of the class you want to execute, which then avoid having to scan through all the types in your assembly (or application domain, depending upon the scope of your search). That setup is a bit more prone to typos, however.

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

Sidebar

Related Questions

i have a large mysql database table in which one column contains values ranging
we have a table in an Oracle Database which contains a column with the
I have one database table which contains 8 columns. One of the columns is
I have a database structure that has a Person table which contains fields such
I have a database with a table that contains Name and CompanyName . They
I have a database table which is full-text indexed and i use the CONTAINS-function
In a SQL server database, I have a table which contains a TEXT field
Greetings friends, In my MySQL database, I have 'MAB' table which contains information about
I have a database (NexusDB (supposedly SQL-92 compliant)) which contains and Item table, a
Should a database table that contains two columns that are foreign keys have a

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.