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. General Programming
  3. C#
  4. classes as enum (or something like an enum)

classes as enum (or something like an enum)

Scheduled Pinned Locked Moved C#
comquestion
6 Posts 3 Posters 0 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.
  • N Offline
    N Offline
    Nathan Gloyn
    wrote on last edited by
    #1

    I'm working with a third party web service that exposes a status property on one of its classes but this property is in fact another class itself. Whilst this is no great shakes I'm trying to make use of this web service easier for other developers in the company since the web service is abstracted away and we have our own adapter classes exposing just the properties/methods we need and I have been trying to come up with a way that will allow me to treat the status object much like a enum What I would like to end up with is something like object.status = StatusAdapter.<value> which would allow something along the lines of if(foo.Status == StatusAdapter.NotStarted){...} Before anybody says 'simply use an enum' the reason I am not simply using an enum is that the status data that the web service object represents can be added to by a user at any time which means that I need to create all relevant classes required at run-time. If I used an enum I'd have to change the class library and re-deploy to all applications that use it. I cannot use an interface as was suggested here [^] simply because the consumers of the class library will not be the ones extending the list of statuses they are just needing to use the statuses I provide. Any ideas?

    K P 2 Replies Last reply
    0
    • N Nathan Gloyn

      I'm working with a third party web service that exposes a status property on one of its classes but this property is in fact another class itself. Whilst this is no great shakes I'm trying to make use of this web service easier for other developers in the company since the web service is abstracted away and we have our own adapter classes exposing just the properties/methods we need and I have been trying to come up with a way that will allow me to treat the status object much like a enum What I would like to end up with is something like object.status = StatusAdapter.<value> which would allow something along the lines of if(foo.Status == StatusAdapter.NotStarted){...} Before anybody says 'simply use an enum' the reason I am not simply using an enum is that the status data that the web service object represents can be added to by a user at any time which means that I need to create all relevant classes required at run-time. If I used an enum I'd have to change the class library and re-deploy to all applications that use it. I cannot use an interface as was suggested here [^] simply because the consumers of the class library will not be the ones extending the list of statuses they are just needing to use the statuses I provide. Any ideas?

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      Nathan Gloyn wrote:

      exposes a status property on one of its classes but this property is in fact another class itself

      What do you mean by this excatly, is there a subtype-per-status (good OO, but exposing the problem of translating this to Web/WCF services)? If so the following code will be possible:

      ReturnObjectType value = Method();
      if(value.Status is StatusType)
      {
      ...
      }

      _Method_ is the service method invoked _ReturnObjectType_ is the return type of the Service method call. _StatusType_ is the type of the status you are looking for.

      CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

      N 1 Reply Last reply
      0
      • K Keith Barrow

        Nathan Gloyn wrote:

        exposes a status property on one of its classes but this property is in fact another class itself

        What do you mean by this excatly, is there a subtype-per-status (good OO, but exposing the problem of translating this to Web/WCF services)? If so the following code will be possible:

        ReturnObjectType value = Method();
        if(value.Status is StatusType)
        {
        ...
        }

        _Method_ is the service method invoked _ReturnObjectType_ is the return type of the Service method call. _StatusType_ is the type of the status you are looking for.

        CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

        N Offline
        N Offline
        Nathan Gloyn
        wrote on last edited by
        #3

        No subtype just different property values. But what I wanted to do is present to users of my class library the values in a form something akin to an enum so that if they create an object and need to set the status of it you wouldn't have to use some sort of 'magic string'

        K 1 Reply Last reply
        0
        • N Nathan Gloyn

          No subtype just different property values. But what I wanted to do is present to users of my class library the values in a form something akin to an enum so that if they create an object and need to set the status of it you wouldn't have to use some sort of 'magic string'

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #4

          Pity. The "magic string" effect is a consequence of [good] advice from Microsoft WRT to maintainability of the service. If they had exposed an enum, adding new values would involve re-gen'ing the client & rebuild, also the numeric values of the enums can change. Really the message contract types aren't OO'd in the full sense, they are close to object like data bags. To get round this problem we have converters to convert Data Contracts from the server to pukka objects on the client side. This can be automated with a bit thought.

          CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

          1 Reply Last reply
          0
          • N Nathan Gloyn

            I'm working with a third party web service that exposes a status property on one of its classes but this property is in fact another class itself. Whilst this is no great shakes I'm trying to make use of this web service easier for other developers in the company since the web service is abstracted away and we have our own adapter classes exposing just the properties/methods we need and I have been trying to come up with a way that will allow me to treat the status object much like a enum What I would like to end up with is something like object.status = StatusAdapter.<value> which would allow something along the lines of if(foo.Status == StatusAdapter.NotStarted){...} Before anybody says 'simply use an enum' the reason I am not simply using an enum is that the status data that the web service object represents can be added to by a user at any time which means that I need to create all relevant classes required at run-time. If I used an enum I'd have to change the class library and re-deploy to all applications that use it. I cannot use an interface as was suggested here [^] simply because the consumers of the class library will not be the ones extending the list of statuses they are just needing to use the statuses I provide. Any ideas?

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

            Make a static class that exposes properties that are of the proper type?

            public static class StatusWrapper
            {
            private static StatusClass notstarted = new StatusClass ( whatever means not started ) ;

            public static StatusClass NotStarted { get { return notstarted ; } }
            
            ...
            

            }

            StatusClass status = StatusWrapper.NotStarted ;

            Or, if that's too ummm... static... the wrapper could use a Dictionary to map enumerated values to the status type.

            N 1 Reply Last reply
            0
            • P PIEBALDconsult

              Make a static class that exposes properties that are of the proper type?

              public static class StatusWrapper
              {
              private static StatusClass notstarted = new StatusClass ( whatever means not started ) ;

              public static StatusClass NotStarted { get { return notstarted ; } }
              
              ...
              

              }

              StatusClass status = StatusWrapper.NotStarted ;

              Or, if that's too ummm... static... the wrapper could use a Dictionary to map enumerated values to the status type.

              N Offline
              N Offline
              Nathan Gloyn
              wrote on last edited by
              #6

              Thanks PIEBALDconsult both of those answers may be of some use, I must admit to leaning towards the dictionary simply because its a) simple b) can hold as many statuses as the user may want to create. Admittedly not sure how the end developer may interact with the dictionary but perhaps thats me just being dumb as not coded it yet. :)

              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