Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. EncodeFlags

EncodeFlags

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpdatabase
23 Posts 14 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #1

    An expert of programming and C# with some 25+ years of experience in software engineering wrote a really useful function:

    private string EncodeFlags(EventType _eventType)
    {
    if (_eventType == EventType.Alarm)
    {
    return "A";
    }
    if (_eventType == EventType.PreAlarm)
    {
    return "P";
    }
    if (_eventType == EventType.Malfunction)
    {
    return "M";
    }
    return "";
    }

    Already this function alone is some kind of WTF. But let's look at the definition of the input parameter which he translates:

    [Flags]
    public enum EventType
    {
    None = 0,
    PreAlarm = 1,
    Alarm = 2,
    Malfunction = 4
    }

    So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually. :~ X|

    K P P M F 7 Replies Last reply
    0
    • B Bernhard Hiller

      An expert of programming and C# with some 25+ years of experience in software engineering wrote a really useful function:

      private string EncodeFlags(EventType _eventType)
      {
      if (_eventType == EventType.Alarm)
      {
      return "A";
      }
      if (_eventType == EventType.PreAlarm)
      {
      return "P";
      }
      if (_eventType == EventType.Malfunction)
      {
      return "M";
      }
      return "";
      }

      Already this function alone is some kind of WTF. But let's look at the definition of the input parameter which he translates:

      [Flags]
      public enum EventType
      {
      None = 0,
      PreAlarm = 1,
      Alarm = 2,
      Malfunction = 4
      }

      So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually. :~ X|

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #2

      He has a bug in in code by not returning a useful defalut value. And its also missing in the enum. If he is an expert in Solitaire I may know him? :~

      Press F1 for help or google it. Greetings from Germany

      T 1 Reply Last reply
      0
      • B Bernhard Hiller

        An expert of programming and C# with some 25+ years of experience in software engineering wrote a really useful function:

        private string EncodeFlags(EventType _eventType)
        {
        if (_eventType == EventType.Alarm)
        {
        return "A";
        }
        if (_eventType == EventType.PreAlarm)
        {
        return "P";
        }
        if (_eventType == EventType.Malfunction)
        {
        return "M";
        }
        return "";
        }

        Already this function alone is some kind of WTF. But let's look at the definition of the input parameter which he translates:

        [Flags]
        public enum EventType
        {
        None = 0,
        PreAlarm = 1,
        Alarm = 2,
        Malfunction = 4
        }

        So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually. :~ X|

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        I suspect it happened the other way around. The database was already screwed -- storing a character flag (yuck) -- but the dev is smart enough to use an enum in the code instead. However, I (not being an average bear) would add a DescriptionAttribute to each member of the enum:

        [Flags]
        public enum EventType
        {
        [Description("")]
        None = 0,
        [Description("P")]
        PreAlarm = 1,
        [Description("A")]
        Alarm = 2,
        [Description("M")]
        Malfunction = 4
        }

        Then, to get the textual representation of the state, you get the Description from the enum -- and members can be added and removed to/from the enum without having to update the code. To take it a step further, I store the enum members and their textual representations in a Dictionary for easy look-up.

        Richard DeemingR F 2 Replies Last reply
        0
        • P PIEBALDconsult

          I suspect it happened the other way around. The database was already screwed -- storing a character flag (yuck) -- but the dev is smart enough to use an enum in the code instead. However, I (not being an average bear) would add a DescriptionAttribute to each member of the enum:

          [Flags]
          public enum EventType
          {
          [Description("")]
          None = 0,
          [Description("P")]
          PreAlarm = 1,
          [Description("A")]
          Alarm = 2,
          [Description("M")]
          Malfunction = 4
          }

          Then, to get the textual representation of the state, you get the Description from the enum -- and members can be added and removed to/from the enum without having to update the code. To take it a step further, I store the enum members and their textual representations in a Dictionary for easy look-up.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Surely it would be simpler to cheat? :)

          public enum EventType : ushort
          {
          None = 0,
          PreAlarm = 'P',
          Alarm = 'A',
          Malfunction = 'M',
          }

          var foo = (EventType)'P'; // foo == PreAlarm
          char bar = (char)foo; // bar == 'P'


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          P 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Surely it would be simpler to cheat? :)

            public enum EventType : ushort
            {
            None = 0,
            PreAlarm = 'P',
            Alarm = 'A',
            Malfunction = 'M',
            }

            var foo = (EventType)'P'; // foo == PreAlarm
            char bar = (char)foo; // bar == 'P'


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Yes. Many years ago, in C, I used a short to hold two characters for some text file parsing I needed to do.

            1 Reply Last reply
            0
            • B Bernhard Hiller

              An expert of programming and C# with some 25+ years of experience in software engineering wrote a really useful function:

              private string EncodeFlags(EventType _eventType)
              {
              if (_eventType == EventType.Alarm)
              {
              return "A";
              }
              if (_eventType == EventType.PreAlarm)
              {
              return "P";
              }
              if (_eventType == EventType.Malfunction)
              {
              return "M";
              }
              return "";
              }

              Already this function alone is some kind of WTF. But let's look at the definition of the input parameter which he translates:

              [Flags]
              public enum EventType
              {
              None = 0,
              PreAlarm = 1,
              Alarm = 2,
              Malfunction = 4
              }

              So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually. :~ X|

              P Offline
              P Offline
              Plamen Dragiyski
              wrote on last edited by
              #6

              Bernhard Hiller wrote:

              store that value in a database eventually.

              Did I miss important new trend in databases? When did they stopped supporting integers?

              F 1 Reply Last reply
              0
              • P Plamen Dragiyski

                Bernhard Hiller wrote:

                store that value in a database eventually.

                Did I miss important new trend in databases? When did they stopped supporting integers?

                F Offline
                F Offline
                F ES Sitecore
                wrote on last edited by
                #7

                Looking at "QA" they also seem to have dropped support for dates.

                Richard DeemingR 1 Reply Last reply
                0
                • P PIEBALDconsult

                  I suspect it happened the other way around. The database was already screwed -- storing a character flag (yuck) -- but the dev is smart enough to use an enum in the code instead. However, I (not being an average bear) would add a DescriptionAttribute to each member of the enum:

                  [Flags]
                  public enum EventType
                  {
                  [Description("")]
                  None = 0,
                  [Description("P")]
                  PreAlarm = 1,
                  [Description("A")]
                  Alarm = 2,
                  [Description("M")]
                  Malfunction = 4
                  }

                  Then, to get the textual representation of the state, you get the Description from the enum -- and members can be added and removed to/from the enum without having to update the code. To take it a step further, I store the enum members and their textual representations in a Dictionary for easy look-up.

                  F Offline
                  F Offline
                  Fabio Franco
                  wrote on last edited by
                  #8

                  PIEBALDconsult wrote:

                  Then, to get the textual representation of the state

                  It's a flagged enum, so how would he represent the state Alarm | Malfunction ?

                  To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                  R 1 Reply Last reply
                  0
                  • B Bernhard Hiller

                    An expert of programming and C# with some 25+ years of experience in software engineering wrote a really useful function:

                    private string EncodeFlags(EventType _eventType)
                    {
                    if (_eventType == EventType.Alarm)
                    {
                    return "A";
                    }
                    if (_eventType == EventType.PreAlarm)
                    {
                    return "P";
                    }
                    if (_eventType == EventType.Malfunction)
                    {
                    return "M";
                    }
                    return "";
                    }

                    Already this function alone is some kind of WTF. But let's look at the definition of the input parameter which he translates:

                    [Flags]
                    public enum EventType
                    {
                    None = 0,
                    PreAlarm = 1,
                    Alarm = 2,
                    Malfunction = 4
                    }

                    So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually. :~ X|

                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #9

                    Bernhard Hiller wrote:

                    with some 25+ years of experience in software engineering

                    var experience = EventType.None | EventType.Alarm | EventType.Malfunction; ;) Marc

                    Latest Article - Create a Dockerized Python Fiddle Web App Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                    K 1 Reply Last reply
                    0
                    • B Bernhard Hiller

                      An expert of programming and C# with some 25+ years of experience in software engineering wrote a really useful function:

                      private string EncodeFlags(EventType _eventType)
                      {
                      if (_eventType == EventType.Alarm)
                      {
                      return "A";
                      }
                      if (_eventType == EventType.PreAlarm)
                      {
                      return "P";
                      }
                      if (_eventType == EventType.Malfunction)
                      {
                      return "M";
                      }
                      return "";
                      }

                      Already this function alone is some kind of WTF. But let's look at the definition of the input parameter which he translates:

                      [Flags]
                      public enum EventType
                      {
                      None = 0,
                      PreAlarm = 1,
                      Alarm = 2,
                      Malfunction = 4
                      }

                      So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually. :~ X|

                      F Offline
                      F Offline
                      Fabio Franco
                      wrote on last edited by
                      #10

                      It makes no sense: 1 - There are combinations not covered by the encoder. ie Alarm | Malfunction 2 - It only makes sense to encode it if whatever is using does not support integers (or maybe it's meant to have a meaningful representation on a digital display, still what if it's alarming and malfunctioning). 3 - Should have used enum's HasFlag method. Now, an expert in C# and 25+ years in programming could only do this if he's hung over, lazy, dumb or having an idea so bright nobody can understand.

                      To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                      P T 2 Replies Last reply
                      0
                      • B Bernhard Hiller

                        An expert of programming and C# with some 25+ years of experience in software engineering wrote a really useful function:

                        private string EncodeFlags(EventType _eventType)
                        {
                        if (_eventType == EventType.Alarm)
                        {
                        return "A";
                        }
                        if (_eventType == EventType.PreAlarm)
                        {
                        return "P";
                        }
                        if (_eventType == EventType.Malfunction)
                        {
                        return "M";
                        }
                        return "";
                        }

                        Already this function alone is some kind of WTF. But let's look at the definition of the input parameter which he translates:

                        [Flags]
                        public enum EventType
                        {
                        None = 0,
                        PreAlarm = 1,
                        Alarm = 2,
                        Malfunction = 4
                        }

                        So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually. :~ X|

                        T Offline
                        T Offline
                        thund3rstruck
                        wrote on last edited by
                        #11

                        Bernhard Hiller wrote:

                        So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually.

                        I learned decades ago that its a fool errand to insult the previous developer(s), especially if one was not there participating in the decision making process. There are a multitude of factors and/or external forces that impact decisions like this and arrogantly criticizing these choices accomplishes little.

                        P J B 3 Replies Last reply
                        0
                        • F F ES Sitecore

                          Looking at "QA" they also seem to have dropped support for dates.

                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #12

                          It's worse than that: based on QA, they've also dropped support for parameterized queries! :rolleyes:


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          N 1 Reply Last reply
                          0
                          • F Fabio Franco

                            It makes no sense: 1 - There are combinations not covered by the encoder. ie Alarm | Malfunction 2 - It only makes sense to encode it if whatever is using does not support integers (or maybe it's meant to have a meaningful representation on a digital display, still what if it's alarming and malfunctioning). 3 - Should have used enum's HasFlag method. Now, an expert in C# and 25+ years in programming could only do this if he's hung over, lazy, dumb or having an idea so bright nobody can understand.

                            To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #13

                            First, assume a legacy database schema...

                            1 Reply Last reply
                            0
                            • T thund3rstruck

                              Bernhard Hiller wrote:

                              So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually.

                              I learned decades ago that its a fool errand to insult the previous developer(s), especially if one was not there participating in the decision making process. There are a multitude of factors and/or external forces that impact decisions like this and arrogantly criticizing these choices accomplishes little.

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #14

                              Hear hear!

                              1 Reply Last reply
                              0
                              • T thund3rstruck

                                Bernhard Hiller wrote:

                                So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually.

                                I learned decades ago that its a fool errand to insult the previous developer(s), especially if one was not there participating in the decision making process. There are a multitude of factors and/or external forces that impact decisions like this and arrogantly criticizing these choices accomplishes little.

                                J Offline
                                J Offline
                                Jorgen Andersson
                                wrote on last edited by
                                #15

                                Especially knowing that I am also a previous developer to someone else. :rolleyes:

                                Wrong is evil and must be defeated. - Jeff Ello

                                1 Reply Last reply
                                0
                                • F Fabio Franco

                                  PIEBALDconsult wrote:

                                  Then, to get the textual representation of the state

                                  It's a flagged enum, so how would he represent the state Alarm | Malfunction ?

                                  To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                                  R Offline
                                  R Offline
                                  Rob Grainger
                                  wrote on last edited by
                                  #16

                                  Well spotted.

                                  "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                  1 Reply Last reply
                                  0
                                  • T thund3rstruck

                                    Bernhard Hiller wrote:

                                    So he translates an enum with a Flags attribute to a string, ignoring the fact that they are flags, in order to store that value in a database eventually.

                                    I learned decades ago that its a fool errand to insult the previous developer(s), especially if one was not there participating in the decision making process. There are a multitude of factors and/or external forces that impact decisions like this and arrogantly criticizing these choices accomplishes little.

                                    B Offline
                                    B Offline
                                    Bernhard Hiller
                                    wrote on last edited by
                                    #17

                                    thund3rstruck wrote:

                                    I learned decades ago that its a fool errand to insult the previous developer(s), especially if one was not there participating in the decision making process. There are a multitude of factors and/or external forces that impact decisions like this and arrogantly criticizing these choices accomplishes little.

                                    I agree with you. I know the guy who wrote that code, and the circumstances: it's fresh code written this week. Some time ago, I discussed with him his obsession of encoding everything in magical ints or one-letter-strings, sometimes also providing "endode"/"decode" functions for converting between the magical ints and the one-letter-strings and the other way round...

                                    T 1 Reply Last reply
                                    0
                                    • Richard DeemingR Richard Deeming

                                      It's worse than that: based on QA, they've also dropped support for parameterized queries! :rolleyes:


                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                      N Offline
                                      N Offline
                                      Nelek
                                      wrote on last edited by
                                      #18

                                      It is even worst than that, based on the QA they have also dropped support for brains

                                      M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                                      1 Reply Last reply
                                      0
                                      • M Marc Clifton

                                        Bernhard Hiller wrote:

                                        with some 25+ years of experience in software engineering

                                        var experience = EventType.None | EventType.Alarm | EventType.Malfunction; ;) Marc

                                        Latest Article - Create a Dockerized Python Fiddle Web App Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                                        K Offline
                                        K Offline
                                        kalberts
                                        wrote on last edited by
                                        #19

                                        Experience is the ability to recognize a mistake when you repeat it.

                                        1 Reply Last reply
                                        0
                                        • K KarstenK

                                          He has a bug in in code by not returning a useful defalut value. And its also missing in the enum. If he is an expert in Solitaire I may know him? :~

                                          Press F1 for help or google it. Greetings from Germany

                                          T Offline
                                          T Offline
                                          TheGreatAndPowerfulOz
                                          wrote on last edited by
                                          #20

                                          KarstenK wrote:

                                          useful defalut

                                          What's that?

                                          #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups