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. Methodology for Switch based on an object

Methodology for Switch based on an object

Scheduled Pinned Locked Moved C#
csharphelpquestionlearning
3 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.
  • S Offline
    S Offline
    Shayne Husson
    wrote on last edited by
    #1

    I am learning C#, coming from VB.NET I would like to know what method is commonly used to make a decision based on the contents of an object. For something like: if ( obj is MyType1 ) ... else if ( obj is MyType2 ) ... else if ( obj is MyType3 ) ... could be quite lengthy. Naively: switch( obj ) { case MyType1: ... Case MyType2: ... Case MyType3: ... } This, I realise is incorrect because switch only accepts integral types or strings. Is there another way of doing it? Any help appreciated, Shayne

    E 1 Reply Last reply
    0
    • S Shayne Husson

      I am learning C#, coming from VB.NET I would like to know what method is commonly used to make a decision based on the contents of an object. For something like: if ( obj is MyType1 ) ... else if ( obj is MyType2 ) ... else if ( obj is MyType3 ) ... could be quite lengthy. Naively: switch( obj ) { case MyType1: ... Case MyType2: ... Case MyType3: ... } This, I realise is incorrect because switch only accepts integral types or strings. Is there another way of doing it? Any help appreciated, Shayne

      E Offline
      E Offline
      exhaulted
      wrote on last edited by
      #2

      Try string temp = Object.GetType().ToString(); switch(temp) { case "typeA": break; case "typeB": break; default: break; } Kev Pearman MCP

      W 1 Reply Last reply
      0
      • E exhaulted

        Try string temp = Object.GetType().ToString(); switch(temp) { case "typeA": break; case "typeB": break; default: break; } Kev Pearman MCP

        W Offline
        W Offline
        Werdna
        wrote on last edited by
        #3

        Or, the best way is to use visitor pattern (http://www.dofactory.com/Patterns/PatternVisitor.aspx), especially if you cast your objects after doing "is". if (obj is MyType) { objOfMyType = (MyType)obj; ... } The visitor pattern will get rid of casts, and allow you to create new classes and not having to add if (obj is MyNewType) or case statements.

        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