CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    How to wrap my Enum in a Class

    For reasons I want to wrap my enum in a class.
    I also want create a method which returns the int representation of the enum but I can't seem to figure out how to return it.
    see the ToInt property, how to I return an int representation of the enum they are currently using?
    e.g. if the caller has an enum instance version of value "Previous" then they can call version.ToInt and get the in of 2.


    Code:
    public class RowVersion
        {
            public int ToInt
            {
                get { return (int)RowVersion; }
            }
            public static RowVersion Original
            {
                get { return RowVersionEnum.Original; }
            }
            public static RowVersion Current
            {
                get { return RowVersionEnum.Current; }
            }
            public static RowVersion Previous
            {
                get { return RowVersionEnum.Previous; }
            }
            enum RowVersionEnum
            {
                Original = 0,
    
                Current = 1,
    
                Previous = 2,
            }
        }

  2. #2
    Join Date
    Nov 2003
    Posts
    2,185

    Re: How to wrap my Enum in a Class

    I don't see ANY reason why you want to wrap an enum into a class. Check the Enum class for enum specific functions.

    You can simply cast a value to an int if required.

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: How to wrap my Enum in a Class

    Because you are casting RowVersion, not RowVersionEnum in the ToInt method.

    Nevertheless, I don't understand why you are trying to do something like that. It indicates error in your desing.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How to wrap my Enum in a Class

    Let me give an example. This is what I use in of my programs:
    Code:
            public enum ImapiErrorValues : uint //Some Of The Returned Errors By ICDBurn Interface
            {
                Ok = 0, //Everything Fine
                False = 1,
                PropertiesIgnored = 2147746304, //Ignored Unknown Property Passed
                BufferTooSmall = 2147746305, //Buffer Too Small
                NotOpened = 2147746315, //DiscMaster Has Not Been Opened Yet
                NotInitialized = 2147746316, //Recorder Object Not Initialized
                UserAbort = 2147746317, //Cancelled By The USer
                Generic = 2147746318, //Generic Error
                MediumNotPresent = 2147746319, //No Disk In Drive
                MediumInvalidType = 2147746320, //Disk Not Correct Type
                DeviceNoProperties = 2147746321, //Recorder Doesn't Support Properties
                DeviceNotAccessible = 2147746322, //Device Cannot Be Used / Already In Use
                DeviceNotPresent = 2147746323, //Device Not Present
                DeviceInvalidType = 2147746324, 
                InitializeWrite = 2147746325, //Drive Interface Cannot Be Initialized
                InitializeEndWrite = 2147746326, //Drive Interface Cannot Be Initilaized For Closing
                FileSystem = 2147746327, //File System Error
                FileAccess = 2147746328, //Error While Writing Image File
                DiskInfo = 2147746329, //Error While REading Disk Data
                TrackNotOpen = 2147746330, //Audio Track Not Open For Writing
                TrackOpen = 2147746331, //Audio Track Already Staged / Opened For Writing
                DiskFull = 2147746332, //Disk Is Full
                BadJolietName = 2147746333, //Badly named Element
                InvalidImage = 2147746334, //Image Is Corrupted / Dmaaged / Cleared
                NoActiveFormat = 2147746335, //Recordin Format Not Selected Yet
                NoActiveRecorder = 2147746336, //Recorder Not Selected Yet
                WrongFormat = 2147746337,
                AlreadyOpen = 2147746338, //DiscMaster Already Open
                WrongDisk = 2147746339, //Disk Was Removed From Active Recorder
                FileExists = 2147746340, //File Already Exists
                StashInUse = 2147746341, //Stash File Already In Use By Different Application
                DeviceStillInUse = 2147746342, //A Differnt Application IS Already Using The Device
                LossOfStreaming = 2147746343, //Content Streaming Was Lost
                CompressedStash = 2147746344, //Stash On Compressed Volume
                EncryptedStash = 2147746345, //Stash On Encrypted Volume
                NotEnoughDiskForStash = 2147746346, //Not Enough Free Space to Create Stash File
                RemovableStash = 2147746347, //Stash On Removable Volume
                CannotWriteToMedia = 2147746348, //Cannot Write To Media
                TrackNotBigEnough = 2147746349, //Track Not Big Enough
                BootImageAndNonBlankDisk = 2147746350 //Attempted To Create Boot Image On Non - Blank Disk
            }
    That is the enum, and I created it above the Class Constructor. Don't worry about what it does

    Now, if I want to use this, IOW, return a value from this, I can do this:
    Code:
            /// <summary> 
            /// Copy From Stage To CD 
            /// </summary> 
            public ImapiErrorValues Burn(IntPtr h)
            {
                return (ImapiErrorValues)SBICDBurn.SBBurn(h); //Burn
    
            }
    This is a normal method, which is created of type ImapiErrorValues, which return one of the enums values.

    I can also do this :
    Code:
            /// <summary>
            /// Get Drive Letter
            /// </summary>
            private string SBGetDriveLetter()
            {
                // Only one drive on the system can have "allow cd burning on this drive" set on it's 
                // properties. 
    
                if (SBHasRecordableDrive == false) //If No Drive Found
                    return "No Available Drive";
    
                if (SBICDBurn == null) //If We Cannot Create ICDBurn Interface
                    throw new Exception("ICDBurn is nothing!");
    
                string SBDrive = " "; //Initialize Drive Letter Variable
    
                uint SBResult = (uint)SBICDBurn.GetRecorderDriveLetter(SBDrive, 4); //Get Drive Letter
    
                if (SBResult != (uint)ImapiErrorValues.Ok) //If Everything Not OK
                {
                    ImapiErrorValues SBErrorCode  = (ImapiErrorValues)SBResult; //Get Returned Error Code
                        throw new Exception("Error Returned By IMAPI " + SBErrorCode);
                }
    
                //Make Returned Drive Letter String More Readable
                SBDrive = SBDrive.TrimEnd(new char[] { char.MinValue });
    
                return SBDrive; //Retrun Value
            }
    if you look at the above bolded line, you will see that I have created an ImapiErrorValues object, then cast - ed SBResult to that type.

    This should give you a good hint on how to go about doing this.

    I hope my advice was helpful
    PS, sorry for all the other code, I just wanted to show a proper example of usage

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured