C# Enumeration Inheritance Madness!

Started by
5 comments, last by RegularKid 16 years, 2 months ago
Hi! I'm trying to do something kinda wacky and can't seem to get it working right ( if it's even possible ). Any ideas on how to do this would be great! Ok, I have a base class name RKObject ( only included the important stuff ):

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Reflection;

namespace RKLight
{
    public class RKObject : Control
    {
        //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
        // ENUMERATIONS
        //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
        public enum RKObjectAnims
        {
            NUM_RK_OBJECT_ANIMS,
        }

        //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
        // MEMBER VARIABLES
        //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

        // List of animations associated with this object ( timelines )
        protected List<string> anims = new List<string>();

        //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
        // METHODS
        //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

        //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
        // RKObject()
        //
        // Constructor.
        //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
        public RKObject()
        {
            RKObjectAnims curAnimEnum;

            // Setup animations
            for (int i = 0; i < (int)RKObjectAnims.NUM_RK_OBJECT_ANIMS; i++)
            {
                curAnimEnum = (RKObjectAnims)i;
                anims.Add(curAnimEnum.ToString());
            }
        }
    }
}



And a derived class named Test ( again, only included the important stuff ):

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RKLight;

namespace Test
{
    public class Test : RKObject
    {
        new public enum RKObjectAnims
        {
            RectFade,
            RectRot,
            
            NUM_RK_OBJECT_ANIMS,
        }

        public void Tick()
        {
            if (Input.Get().KeyPressedNew(Key.LEFT))
            {
                PlayAnim((int)RKObjectAnims.RectFade);
            }
            if (Input.Get().KeyPressedNew(Key.RIGHT))
            {
                PlayAnim((int)RKObjectAnims.RectRot);
            }
        }
    }
}



This of course doesn't work, but the basic idea is this: The RKObjectAnims enumeration is defined in both the base class and my derived class. Since, I want my animation enumerations to correspond to their anim names ( strings ) without having to keep two tables, I initialize the anims list with the enumerations using the "ToString()" method. This would be great, except that I don't want to have to initialize it in every derived class: I want the base class to automatically do it for me. However, the base class only knows about it's RKObjectAnims enums, not my derived class version. What I would LOVE to happen is that I simply add an entry to the derived class enumeration ( in Test ) and the anims string list gets filled in automatically in the base class with that enum's ToString() version. Then, all I need to do is use the derived class enumeration index to play an animation ( seen in the Tick of Test class ). If anyone has any way for me to do this, I would be so happy :) Thanks in advance!
Advertisement
Anyone? Sorry for the bump, I'm just anxious to hear if this is actually possible :)
Here's one way to do it... although it involves a tiny bit of work in each derived class, so it's maybe not quite what you want.

public class RKObject{   public enum RKObjectAnims   {      NUM_RK_OBJECT_ANIMS,   }   public RKObject(Array enumValues)   {     // Use array here   }}public class Test : RKObject{   new public enum RKObjectAnims   {      Foo,      Bar,      Baz,      NUM_RK_OBJECT_ANIMS   }   public Test()      : base(Enum.GetValues(typeof(RKObjectAnims)))   {   }}
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
I can't tell what problem you are trying to solve. Do you actually use the enums later on or are they just a way to get these strings back into the base class?

If its just getting a list of strings back into the base class, you might want to consider using Attributes.
Well, I'd like to simply use the enumeration in the derived class to setup the list of strings ( anims in the base class ). Then, be able to use those enums to index into that base class anims array.

The end result is to eliminate me having to have a loop in the constructor of each derived class that adds their enum.ToString() list to the anims array. That way all each derived class has to do is override that enumeration and it'll be already setup by the base class.
Just have a comment. Is this just a play on design or are you really planning on designing you application like this? Because then I have to ask why do you want to hard code the range of animations you can perform?
regards/thallishI don't care if I'm known, I'd rather people know me
Martee:

That seems like a good way to do it. However, I'm using Silverlight with C# ( probably should have mentioned that, hehe ) and I don't have Enum.GetValues. It doesn't look like it's defined in the slimmed down .NET Framework for Silverlight. Is there a different way to do this?

Also, would this work if there was a class in between these two? As in, Sprite derives from RKObject and Test derives from Sprite ( with this special constructor of array values still being in RKObject only )?

thallish:

The reason I am going to hard code the animations is because they are created in another program ( Microsoft Expression ) and then I need to reference them in code by their string name. So, the PlayAnim() method I didn't include the source for basically finds an object of type Storyboard with the string name corresponding to the enum that I pass in and then calls Begin() on it.

This topic is closed to new replies.

Advertisement