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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:37:43+00:00 2026-06-10T09:37:43+00:00

I have an interface IDatabase that interfaces a few different ways to access a

  • 0

I have an interface IDatabase that interfaces a few different ways to access a database. For example using RESTful, or MySqlDirectConnect.

Next I have a class foo that has member variables A, B, C that need to pull there data out of the database.

public class foo
{
    private string a;
    private int b;
    private int c;
}

Originally, I was going to create a Get method for every single member variable. Using Generics, you specify what interface you want to use.

public string GetA<T>() where T: IDatabase
{
   //example might be GetA<RESTful>();
}

public int GetB<T>() where T: IDatabase
{
   //example might be GetB<MySQL>();
}

and so on….

problem I see is that if the database changes I have to go back and change all these methods. In some cases class foo might have around 20 variables with a Get() method for each.

So I would like to create a really generic GetValue() where I can specify both the interface and the variable i would like to query for.

something along the lines of:

public object GetValue<T>(the class variable I want to query) where T: IDatabase
{
    //query database using type T and return it to the variable specified
}

I would like to avoid having a conditional for every member variable inside the method.

So is this possible?

  • 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-10T09:37:44+00:00Added an answer on June 10, 2026 at 9:37 am

    Here is a great solution I worked out

    public void GetValue<T>(string property) where T: IDatabase, new()
    {
        IDatabase database = new T();
        database.RegisterObserver(this);
    
        string FROM = typeof(theCurrentClass).Name;
        string SELECT = editProperty = property;
        string WHERE = "1";
    
        database.Select(new Query(SELECT, FROM, WHERE));        
    }
    
    private string fieldName;  //contains the name of the field I want to edit
    
    void IObserver.Update(object data)
    {       
        FieldInfo field = this.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
    
        if(field != null)
        {
            field.SetValue(this, data);         
        }
    }
    

    EDIT: I finally figured it out, just updated the above code with the final answer

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

Sidebar

Related Questions

We have an Informix 4GL forms interface to our database, accessed using a terminal
I have a Microsoft Access Database with an interface for usability ... But now,
I have a few repository classes that are meant to talk to different kinds
I have a few custom tables in my Drupal database that were created manually.
I'm writing a program using jdbc that will be an interface to database(smth like
I am using a cursor to access a sqlite database I have in my
Hi Guys I have a winform which acts as an interface for a database.
Let's say we have interface window_creator that responsible for creation of windows. For simplicity
I have a Silveright app that allows users to specify filters on a few
I have a User Interface that receives asynchronous notifications from a Service that prompts

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.