Thread: Some help with "templates"

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Some help with "templates"

    Hallo,

    I am trying to build a generic debug class for printing information about different objects to the screen, and also change them if wanted, but I am having some problems with adding the ability to change them.

    Here is my DebugItemClass.

    The problem is in the update function, the code does not compile as c# does not like it when you try to assign other types of classes to the T object. Any ideas on what the correct way to do this is?
    Code:
    abstract class GenericDebugMenuItem
        {
            public abstract void Update(int value);
            public abstract void Draw(GameRender render, Vector2 position);
        }
    
        class DebugMenuItem<T> : GenericDebugMenuItem
        {
            T m_item;
            string m_text;
            float m_inputScale;
            DebugItemValueType m_type;
    
            bool m_selected;
            public bool Selected
            {
                get { return m_selected; }
                set { m_selected = value; }
            }
    
            public DebugMenuItem(ref T item, string text, float inputScale, DebugItemValueType type)
            {
                m_item = item;
                m_text = text;
                m_inputScale = inputScale;
                m_type = type;
    
                m_selected = false;
            }
    
            public override void Draw(GameRender render, Vector2 position)
            {
                string output = m_text + m_item.ToString();
    
                if (m_selected)
                    render.AddTextToRender(output, position, Color.Yellow);
                else
                    render.AddTextToRender(output, position, Color.Red);
            }
    
            public override void Update(int value)
            {
                switch (m_type)
                {
                    case DebugItemValueType.E_double:
                        m_item = m_item + (value * m_inputScale);
                        break;
                    case DebugItemValueType.E_bool:
                        if (value == -1)
                            m_item = false;
                        else if (value == 1)
                            m_item = true;
                        break;
                }
            }
        }

    Edit:
    I also have an other question, how can I store the pointer to the object that is passed it? Object are normally passed by value, but I want to store the pointer, so that if I change the value it gets updated where it was used.
    Last edited by h3ro; 03-03-2010 at 03:31 PM.

Popular pages Recent additions subscribe to a feed