Click here to Skip to main content
16,015,707 members
Home / Discussions / C#
   

C#

 
AnswerRe: To the whole commuity(solve this) Pin
Pete O'Hanlon8-May-08 3:12
mvePete O'Hanlon8-May-08 3:12 
AnswerRe: To the whole commuity(solve this) Pin
phannon868-May-08 3:20
professionalphannon868-May-08 3:20 
AnswerRe: To the whole commuity(solve this) Pin
Ashfield8-May-08 3:21
Ashfield8-May-08 3:21 
AnswerRe: To the whole commuity(solve this) Pin
Gareth H8-May-08 3:23
Gareth H8-May-08 3:23 
QuestionIssues making a generic class with System.Enum as a parameter [modified] Pin
carbon_golem8-May-08 2:47
carbon_golem8-May-08 2:47 
AnswerRe: Issues making a generic class with System.Enum as a parameter Pin
S. Senthil Kumar8-May-08 6:05
S. Senthil Kumar8-May-08 6:05 
AnswerRe: Issues making a generic class with System.Enum as a parameter Pin
PIEBALDconsult8-May-08 14:54
mvePIEBALDconsult8-May-08 14:54 
GeneralRe: Issues making a generic class with System.Enum as a parameter Pin
carbon_golem9-May-08 2:43
carbon_golem9-May-08 2:43 
I put the double <<>> to make the CP html generator show at least that it was generic. What I want to do is define named bits in a generic way. The named bits would be of standard lengths, 8, 16, 32, 64. I hoped to use an enum to provide a templating mechanism to do this, but so far no luck, and the solutions I have worked out aren't very type safe. What this is for is representing bit fields in embedded device logs. Here is my latest. A lot of it is shetchy, but...
public class StatusField<t>
        where T : struct {

        #region Members
        private T template;
        private ValueType sword;
        #endregion

        private StatusField(ValueType initialValue) {
            template = new T();
            if (template is Enum) {
                Type t = template.GetType();
                if (t.GetCustomAttributes(typeof(FlagsAttribute), false).Length == 0) {
                    throw new System.InvalidCastException("Requires Enum that defines FlagsAttribute");
                }
                sword = initialValue;
            } else {
                throw new System.InvalidCastException("Could  not convert generic type");
            }
        }

        public Object UnderlyingValue {
            get {
                return sword;
            }
            set {
                sword = (ValueType)value;
            }

        }

        public Boolean this[T index] {
            get {
                if (Enum.IsDefined(typeof(T), index)) {
                    Object x = Convert.ChangeType(index, typeof(Int64));
                    Int64 temp1 = (Int64)x;
                    Object y = Convert.ChangeType(sword, typeof(Int64));
                    Int64 temp2 = (Int64)y;
                    return (temp1 & temp2) == temp1;
                } else {
                    throw new IndexOutOfRangeException("Field is not defined");
                }
            }
            set {
                if (Enum.IsDefined(typeof(T), index)) {
                    // setter not defined
                } else {
                    throw new IndexOutOfRangeException("Field is not defined");
                }
            }
        }

        public static StatusField<t> New(ValueType value) {
            return new StatusField<t>(value);            
        }       
    }
</t></t></t>


And Usage:
            StatusField<alarms> field = StatusField<alarms>.New(Alarms.Battery | Alarms.Motion);
            Console.WriteLine(field.UnderlyingValue);
            Console.WriteLine(field[Alarms.Battery]);
            Console.ReadLine();
</alarms></alarms>


Thanks for the attention!

Scott P

“It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.”
-Edsger Dijkstra

GeneralRe: Issues making a generic class with System.Enum as a parameter Pin
PIEBALDconsult9-May-08 12:23
mvePIEBALDconsult9-May-08 12:23 
QuestionJPEG EXIF metadata Pin
Jim Warburton8-May-08 2:40
Jim Warburton8-May-08 2:40 
AnswerRe: JPEG EXIF metadata Pin
Luc Pattyn8-May-08 6:34
sitebuilderLuc Pattyn8-May-08 6:34 
QuestionConverting an excel spreadsheet to PDF Pin
Sue68-May-08 2:34
Sue68-May-08 2:34 
AnswerRe: Converting an excel spreadsheet to PDF Pin
Harvey Saayman8-May-08 4:52
Harvey Saayman8-May-08 4:52 
QuestionC# code to triple a number Pin
Oluwayomi8-May-08 2:20
Oluwayomi8-May-08 2:20 
AnswerRe: C# code to triple a number Pin
J4amieC8-May-08 2:22
J4amieC8-May-08 2:22 
GeneralRe: C# code to triple a number Pin
Christian Graus8-May-08 2:24
protectorChristian Graus8-May-08 2:24 
GeneralRe: C# code to triple a number Pin
Xmen Real 8-May-08 2:32
professional Xmen Real 8-May-08 2:32 
AnswerRe: C# code to triple a number Pin
Christian Graus8-May-08 2:24
protectorChristian Graus8-May-08 2:24 
GeneralRe: C# code to triple a number Pin
dan!sh 8-May-08 2:28
professional dan!sh 8-May-08 2:28 
AnswerRe: C# code to triple a number Pin
Pete O'Hanlon8-May-08 2:26
mvePete O'Hanlon8-May-08 2:26 
AnswerRe: C# code to triple a number Pin
#realJSOP8-May-08 2:50
professional#realJSOP8-May-08 2:50 
AnswerRe: C# code to triple a number Pin
BadKarma8-May-08 2:51
BadKarma8-May-08 2:51 
GeneralRe: C# code to triple a number Pin
J4amieC8-May-08 3:00
J4amieC8-May-08 3:00 
GeneralRe: C# code to triple a number Pin
BadKarma8-May-08 4:26
BadKarma8-May-08 4:26 
GeneralRe: C# code to triple a number Pin
J4amieC8-May-08 4:55
J4amieC8-May-08 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.