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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:04:33+00:00 2026-05-13T17:04:33+00:00

I have a number of classes that reflect tables in a database. I would

  • 0

I have a number of classes that reflect tables in a database. I would like to have a base class that has some basic functionality (say, it would have a “isDirty” flag), and a static array of strings with the column names as they appear in the database. The following code doesn’t work but illustrates what I would like to do:

public class BaseRecord {  
  public bool isDirty;  
  public object [] itemArray;  
  public static string [] columnNames;    
}

public class PeopleRec : BaseRecord {  
}

public class OrderRec : BaseRecord {  
}

public static void Main() {
  PeopleRec.columnNames = new string[2];
  PeopleRec.columnNames[0]="FIRST_NAME";
  PeopleRec.columnNames[1]="LAST_NAME";

  OrderRec.columnNames = new string[4];
  OrderRec.columnNames[0] = "ORDER_ID";
  OrderRec.columnNames[1] = "LINE";
  OrderRec.columnNames[2] = "PART_NO";
  OrderRec.columnNames[3] = "QTY";
}

public class DoWork<T> where T : BaseRecord {
  public void DisplayColumnNames() {
    foreach(string s in T.columnNames)
      Console.Write("{0}", s);
    }
  public void DisplayItem(T t) {
    for (int i=0; i<itemValues.Length; i++) {
      Console.Write("{0}: {1}",t.columnNames[i],t.itemValues[i])
    }
  }
}

I would like each derived class to have it’s own static array of strings of database column names, and I would like the generic class to access this static member without the need for an instance.

But it doesn’t work:
(A) columnNames is the identical array in BaseRec, PeopleRec and OrderRec. I cannot have columnNames be different. BaseRec.columnNames.Length would be 3 because the columnNames in OrderRec is initialized last.
(B) The notation T.columnNames does not compile.

Any ideas on how to fix this?

  • 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-13T17:04:33+00:00Added an answer on May 13, 2026 at 5:04 pm

    The issue is you that you want to associate some data with the types, not with instances of the types. I’m not sure that there’s a neat way of doing this in C#, but one possibility is using a static Dictionary<Type, string[]> on BaseRecord. An example is below, you could neaten this up by adding some generic static members on BaseRecord for initializing/accessing the record names (and add some error checking…):

    using System;
    using System.Collections.Generic;
    
    namespace Records
    {
        public class BaseRecord
        {
            public bool isDirty;
            public object[] itemArray;
    
            public static Dictionary<Type, string[]> columnNames = new Dictionary<Type, string[]>();
        }
    
        public class PeopleRec : BaseRecord
        {
            static PeopleRec()
            {
                string[] names = new string[2];
                names[0] = "FIRST_NAME";
                names[1] = "LAST_NAME";
                BaseRecord.columnNames[typeof(PeopleRec)] = names;
            }
        }
    
        public class DoWork<T> where T : BaseRecord
        {
            public void DisplayColumnNames()
            {
                foreach (string s in BaseRecord.columnNames[typeof(T)])
                    Console.WriteLine("{0}", s);
            }
    
            public void DisplayItem(T t)
            {
                for (int i = 0; i < t.itemArray.Length; i++)
                {
                    Console.WriteLine("{0}: {1}", BaseRecord.columnNames[typeof(T)][i], t.itemArray[i]);
                }
            }
        }
    
        class Program
        {
            public static void Main()
            {
                PeopleRec p = new PeopleRec
                {
                    itemArray = new object[] { "Joe", "Random" }
                };
    
                DoWork<PeopleRec> w = new DoWork<PeopleRec>();
                w.DisplayColumnNames();
                w.DisplayItem(p);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python module that defines a number of classes: class A(object): def
I have a basic class that derived subclasses inherit from, it carries the basic
I have a number of data classes representing various entities. Which is better: writing
I have a project with a number of different classes querying and modifying data
In Ruby I have often written a number of small classes, and had a
I'm working with a number of 'helper' classes, which affectively have a bunch of
Does Information Hiding mean I should minimize the number of properties my classes have?
I have created an application (a web-based application) which now has a large number
I would like to reflect the XML tree in my object structure, but I
I'm working on a project were we have number (5 at the moment) of

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.