Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Enumeration Types do not Enumerate! Working around .NET and Language Limitations

Rate me:
Please Sign up or sign in to vote.
4.95/5 (74 votes)
10 Mar 2017CPOL31 min read 247.9K   641   111  
Generic classes for enumeration-based iteration and array indexing
/*
    Enumeration classes:
    Generic class Enumeration provides iteration capabilities based on enumeration or any other type with static fields;
    Generic class EnumerationIndexedArray implements enumeration-indexed arrays
    Generic class CartesianSquareIndexedArray implements indexed arrays indexed by a Cartesian Square based on an enumeration
    
    Copyright (C) 2008-2010 by Sergey A Kryukov
    http://www.SAKryukov.org
*/

namespace SA.Universal.Enumerations {
    using Cardinal = System.UInt32;

    /// <summary>
    /// Generic class EnumerationIndexedArray implements enumeration-indexed arrays
    /// </summary>
    /// <typeparam name="INDEX">Type representing the type for array indices; enum type is recommended, but can be any type with any static fields used for indexing</typeparam>
    /// <typeparam name="ELEMENT">Type representing the type for array values</typeparam>
    public class EnumerationIndexedArray<INDEX, ELEMENT> {

        public EnumerationIndexedArray() {
            InitializeBody(default(ELEMENT), false);
        } //EnumerationIndexedArray

        public EnumerationIndexedArray(ELEMENT sameInitialValue) {
            InitializeBody(sameInitialValue, true);
        } //EnumerationIndexedArray

        /// <summary>
        /// Indexed property used to manipulate array elements.
        /// </summary>
        /// <param name="index">INDEX value used to index the array; must have the same value as one of the static INDEX fields</param>
        /// <returns>Element of the array; may cause out-of-range exception:
        /// Indexing only works if the value passed as index is the same as one of the static INDEX values;
        /// otherwise it returns -1. For example if INDEX is System.Int32, this property works
        /// if index == System.Int32.MaxValue or index == System.Int32.MinValue and cause exception otherwise.
        /// </returns>
        public ELEMENT this[INDEX index] {
            get { return Body[Enumeration<INDEX>.GetIntegerIndexFromEnumValue(index)]; }
            set { Body[Enumeration<INDEX>.GetIntegerIndexFromEnumValue(index)] = value; }
        } //this

        #region implementation
        
        private ELEMENT[] Body;
        private void InitializeBody(ELEMENT sameInitialValue, bool useInitialValue) {
            Body = new ELEMENT[Enumeration<INDEX>.CollectionLength];
            if (!useInitialValue) return;
            for (int jj = 0; jj < Body.Length; jj++)
                Body[jj] = sameInitialValue;
        } //InitializeBody

        #endregion implementation

    } //EnumerationIndexedArray

} //namespace SA.Universal.Enumerations

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Physics, physical and quantum optics, mathematics, computer science, control systems for manufacturing, diagnostics, testing, and research, theory of music, musical instruments… Contact me: https://www.SAKryukov.org

Comments and Discussions