none
Derived classes - how to find derived class' attributes from base class RRS feed

  • Question

  • Hi everyone,

    I come from a PHP background and I'm starting a new project in C#.net 2005 (express). My question regards base classes and getting a list of attributes of the instantiated derived class from within the original base class - i.e: Have base class declare a function that will set all the attributes of the instantiated class from a provided hashtable.

    Pseudo-code:
    ---------------------------------
    class base()
    {

        protected populate_from_array(Hashtable in_vals)
        {
            foreach(object[] entry in in_vals)
            {
                if (entry[0] is one of the calling classes attributes)
                    attribute = entry[1]
            }
        }

    }

    class whatever : base
    {
        public string name;
    }

    //main programming loop:

    Hashtable w_vals = new Hashtable();
    w_vals.Add("name", "Timmay");

    whatever w = new whatever;
    w.populate_from_array(w_vals);


    // after this is done the w object should have its attributes set to: name - Timmay

    --------------------------------
    What I have so far in c#
    --------------------------------

    BASE.cs:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;

    namespace Notes.Class
    {
        class BASE
        {
            public void populate_from_array(Hashtable in_hash)
            {
                MemberInfo membInfo = this.GetType();
                object[] attributes = membInfo.GetCustomAttributes(true); // this is null :(
            }
        }
    }


    DB.cs
    namespace Notes.Class
    {
        class DB:BASE
        {
            private string DB_host_name;
            private string DB_user_id;
            private string DB_password;
        }
    }

    Main thread
     Hashtable db_vals = new Hashtable();
    db_vals.Add("DB_host_name", "media");
    db_vals.Add("DB_user_id", "smeh");
    db_vals.Add("DB_password", "swordfish");

    DB db = new DB();
    db.populate_from_array(db_vals);



    Anyone have any ideas how I can achieve this? Have a generic function in the base class that will handle the assignment of attributes of all derived classes. Basically, I hate having 30 argument constructors or 30 functions to set the class vars. Cheers!
    Sunday, February 18, 2007 9:15 AM

Answers

  • In general a derived class adds specialisation to the base class. Derived classes usually have different constructors because they specialise and the point at which you instantiate the object is the best time to set it up. Can you provide more information the problem you have. It may be there is a 'standard' OO way of doing it.
    Sunday, February 18, 2007 10:12 AM

All replies

  • In general a derived class adds specialisation to the base class. Derived classes usually have different constructors because they specialise and the point at which you instantiate the object is the best time to set it up. Can you provide more information the problem you have. It may be there is a 'standard' OO way of doing it.
    Sunday, February 18, 2007 10:12 AM
  • The thing is that it wont be different for each derived class, it will be standard across the board. This way I can have one function for all my classes that will populate the object's attributes in one fell swoop. As far as data goes, most of my intended classes will have standard functions:

    bool populate_from_array(hashtable in_hash)
    bool populate_from_id(int id, string table)   // id is primary key, table is table name in DB.
    hashtable get_value(string key)
    bool set_value(string key, object value)
    Sunday, February 18, 2007 10:54 AM
  • One idiom for this is to use another method to instantiate the objects' data -- a method that you call explicitly after the objects constructors have all run -- therefore, after all vtables are set up.

    As I recall, MFC serialization used this idiom. First the object would be constructed entirely, and then the caller would call, e.g., object.Construct().

    Also, I think ATL used this idiom?

    Anyway, as long as this Construct* method is called after all constructors are completed, it should be able to enumerate all the final object properties (I'm assuming reflection is the way to go to get them).

     

     

    *"Construct" or whatever name you like, e.g., "FinalConstruct"

     

    Sunday, February 18, 2007 11:55 PM
  • Fair enough, I'll use something like
    DB.cs
    class DB
    {
        private DB_host_name;
        ....

        public DB
        {
        }

        public populate_from_array(hashtable in_array)
        {
           //reflection code, get list of my attributes
           foreach (string att_name in my_atts)
           {
                 // itterate hashtable, assign if match on attribute name and hashtable key
           }
        }
    }

    Its a shame there isn't a generic way of doing this, since the logic wont change and I'll have duplicated code for all my classes.
    Monday, February 19, 2007 1:50 AM