locked
Extending enumeration RRS feed

  • Question

  • Hi all,

    I have a base class BaseClass in the Framework.dll. There will be a number of projects that will use the BaseClass. This class must allow for raising predefined errors. This is done via ErrorOccured event. I want to create a list of predefined errors types (in the Framework.dll and for the Framework.dll) that can be extended in the projects that have classes that inherit from the BaseClass.

    I thought of this solution:

    Create ErrorType enum that will have two errors: enum ErrorType { BaseSpecificError1, BaseSpecificError2 }. There will be a factory that will create an error of the specified type:

    ErrorFactory
    {
     CreateError(ErrorType);
    }

    I would like child classes to extend the ErrorType enum (it can have additional errors defined like ChildSpecificError). Also the ErrorFactory should be extended to allow for new additional types of errors.

    However, extending of enums is not possible.
    Do somebody have a good idea how to create the extendable errors set?

    Thanks,
    Vitaly

    P.S.: I don't want to use strings instead of enum values. It would be great to think about some solution that can be checked at compile time.
    Friday, November 21, 2008 7:43 AM

Answers

  • You can inherit from Exception and create your own exception classes.
    You don't need no ErrorFactory, you just create the specific exception class.
    Error handling allows different handling based on exception class if needed.
    • Marked as answer by Guo Surfer Tuesday, November 25, 2008 6:47 AM
    Friday, November 21, 2008 7:52 AM
  • Create a static class with public static readonly integer values:

    public class ErrorType  
    {  
        protected ErrorType() { }  
     
        public static readonly int ErrorOne = 1;  
        public static readonly int ErrorTwo = 2;  
    }  
     
    public class NewErrorType : ErrorType  
    {  
        protected NewErrorType() { }  
     
        public static readonly int ErrorThree = 3;  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Guo Surfer Tuesday, November 25, 2008 6:47 AM
    Friday, November 21, 2008 6:33 PM
  • Not sure what an error factory would do.  Generate exceptions based upon a parameter?  As for extending your enumeration, it has been noted that you cannot.  But you can extend dictionaries.  It also means using strings or someother type of key.

    interface IBaseClassEventArgs  
        {  
            // define common properties and methods  
        }  
        interface IInheritsBaseClass  
        {  
            event EventHandler<BaseClassEventArgs> ErrorOccured;  
        }  
        abstract class BaseClassEventArgs : EventArgs, IBaseClassEventArgs  
        {  
     
        }  
        abstract class MyBaseClass : IInheritsBaseClass  
        {  
            internal protected MyBaseClass()  
            {  
                Factories.AddBaseErrorTypes();  
            }
            #region IInheritsBaseClass Members  
     
            public event EventHandler<BaseClassEventArgs> ErrorOccured;
            #endregion  
        }  
        class DerivedClass : MyBaseClass  
        {  
     
            internal DerivedClass()  
            {  
                this.ErrorOccured += new EventHandler<BaseClassEventArgs>(DerivedClass_ErrorOccured);  
            }  
     
            void DerivedClass_ErrorOccured(object sender, BaseClassEventArgs e)  
            {  
                throw new Exception("The method or operation is not implemented.");  
            }  
        }  
     
        class Factories  
        {  
            public static void ErrorFactory(string typeName)  
            {  
                string fullName = ErrorType.ErrorTypes[typeName] + typeName;  
                //Type errorClassObject = Type.GetType(fullName);  
                //System.Activator.CreateInstance(errorClassObject);  
            }  
            public static void AddErrorType(string typeName, string nameSpace)  
            {  
                ErrorType.ErrorTypes.Add(typeName, nameSpace);  
            }  
            internal static void AddBaseErrorTypes()  
            {  
                // add your base error types.  
            }  
        }  
        class ErrorType  
        {  
            public static Dictionary<stringstring> ErrorTypes = new Dictionary<stringstring>();  
        } 

    I assume that you want Exceptions to be inherited from a base type, too.

    Rudedog   =|^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Guo Surfer Tuesday, November 25, 2008 6:47 AM
    Friday, November 21, 2008 7:19 PM

All replies

  • You can inherit from Exception and create your own exception classes.
    You don't need no ErrorFactory, you just create the specific exception class.
    Error handling allows different handling based on exception class if needed.
    • Marked as answer by Guo Surfer Tuesday, November 25, 2008 6:47 AM
    Friday, November 21, 2008 7:52 AM
  • Create a static class with public static readonly integer values:

    public class ErrorType  
    {  
        protected ErrorType() { }  
     
        public static readonly int ErrorOne = 1;  
        public static readonly int ErrorTwo = 2;  
    }  
     
    public class NewErrorType : ErrorType  
    {  
        protected NewErrorType() { }  
     
        public static readonly int ErrorThree = 3;  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Guo Surfer Tuesday, November 25, 2008 6:47 AM
    Friday, November 21, 2008 6:33 PM
  • Not sure what an error factory would do.  Generate exceptions based upon a parameter?  As for extending your enumeration, it has been noted that you cannot.  But you can extend dictionaries.  It also means using strings or someother type of key.

    interface IBaseClassEventArgs  
        {  
            // define common properties and methods  
        }  
        interface IInheritsBaseClass  
        {  
            event EventHandler<BaseClassEventArgs> ErrorOccured;  
        }  
        abstract class BaseClassEventArgs : EventArgs, IBaseClassEventArgs  
        {  
     
        }  
        abstract class MyBaseClass : IInheritsBaseClass  
        {  
            internal protected MyBaseClass()  
            {  
                Factories.AddBaseErrorTypes();  
            }
            #region IInheritsBaseClass Members  
     
            public event EventHandler<BaseClassEventArgs> ErrorOccured;
            #endregion  
        }  
        class DerivedClass : MyBaseClass  
        {  
     
            internal DerivedClass()  
            {  
                this.ErrorOccured += new EventHandler<BaseClassEventArgs>(DerivedClass_ErrorOccured);  
            }  
     
            void DerivedClass_ErrorOccured(object sender, BaseClassEventArgs e)  
            {  
                throw new Exception("The method or operation is not implemented.");  
            }  
        }  
     
        class Factories  
        {  
            public static void ErrorFactory(string typeName)  
            {  
                string fullName = ErrorType.ErrorTypes[typeName] + typeName;  
                //Type errorClassObject = Type.GetType(fullName);  
                //System.Activator.CreateInstance(errorClassObject);  
            }  
            public static void AddErrorType(string typeName, string nameSpace)  
            {  
                ErrorType.ErrorTypes.Add(typeName, nameSpace);  
            }  
            internal static void AddBaseErrorTypes()  
            {  
                // add your base error types.  
            }  
        }  
        class ErrorType  
        {  
            public static Dictionary<stringstring> ErrorTypes = new Dictionary<stringstring>();  
        } 

    I assume that you want Exceptions to be inherited from a base type, too.

    Rudedog   =|^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Guo Surfer Tuesday, November 25, 2008 6:47 AM
    Friday, November 21, 2008 7:19 PM