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. A better way than multiple case in switch statement

A better way than multiple case in switch statement

Scheduled Pinned Locked Moved C#
cssdesigntutorialquestion
5 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.
  • A Offline
    A Offline
    allenmpcx
    wrote on last edited by
    #1

    Hello. I have the following function: public BaseClass SpawnClass( ObjectType type ) { BaseClass retval = null; float X = 5; float scale = 0.3429f; switch(type) { case ObjectType.Class1: { retval = new Class1(X,scale); break; } case ObjectType.Class2: { retval = new Class2(X,scale); break; } case ObjectType.Class3: { retval = new Class3(X,scale); break; } } return retval; } I have about 11 classes but I didn't want this to get too long. This works, but it's very ugly. Is there a better way to do this? I've thought about the "Gang of Four" design patterns, but I'm not sure if they would be less ugly. I noticed the TypeConverter class, but I don't know how to use it. Any suggestions?

    I love to program!

    V 1 Reply Last reply
    0
    • A allenmpcx

      Hello. I have the following function: public BaseClass SpawnClass( ObjectType type ) { BaseClass retval = null; float X = 5; float scale = 0.3429f; switch(type) { case ObjectType.Class1: { retval = new Class1(X,scale); break; } case ObjectType.Class2: { retval = new Class2(X,scale); break; } case ObjectType.Class3: { retval = new Class3(X,scale); break; } } return retval; } I have about 11 classes but I didn't want this to get too long. This works, but it's very ugly. Is there a better way to do this? I've thought about the "Gang of Four" design patterns, but I'm not sure if they would be less ugly. I noticed the TypeConverter class, but I don't know how to use it. Any suggestions?

      I love to program!

      V Offline
      V Offline
      vineas
      wrote on last edited by
      #2

      Assuming the constructors are identical, I'd probably do something like:public BaseClass SpawnClass(System.Type type) { float X = 5; float scale = 0.3429f; return (BaseClass) type.InvokeMember(type.FullName, System.Reflection.BindingFlags.CreateInstance, null, null, new object[] {X, scale}); }
      That should work from what I remember, but it may need some tweaks to be work correctly.

      ----- In the land of the blind, the one eyed man is king.

      A 2 Replies Last reply
      0
      • V vineas

        Assuming the constructors are identical, I'd probably do something like:public BaseClass SpawnClass(System.Type type) { float X = 5; float scale = 0.3429f; return (BaseClass) type.InvokeMember(type.FullName, System.Reflection.BindingFlags.CreateInstance, null, null, new object[] {X, scale}); }
        That should work from what I remember, but it may need some tweaks to be work correctly.

        ----- In the land of the blind, the one eyed man is king.

        A Offline
        A Offline
        allenmpcx
        wrote on last edited by
        #3

        That function looks like it depends on System.type, however the function I have depends on a custom enumeration (ObjectType). Would this way still work? Would there be a way to create a type using a string?

        I love to program!

        S 1 Reply Last reply
        0
        • V vineas

          Assuming the constructors are identical, I'd probably do something like:public BaseClass SpawnClass(System.Type type) { float X = 5; float scale = 0.3429f; return (BaseClass) type.InvokeMember(type.FullName, System.Reflection.BindingFlags.CreateInstance, null, null, new object[] {X, scale}); }
          That should work from what I remember, but it may need some tweaks to be work correctly.

          ----- In the land of the blind, the one eyed man is king.

          A Offline
          A Offline
          allenmpcx
          wrote on last edited by
          #4

          nevermind on that. Your method worked perfect because I can call Type.GetType() and pass in a string to get the type, so thats exactly what I did. Thanks a lot.

          I love to program!

          1 Reply Last reply
          0
          • A allenmpcx

            That function looks like it depends on System.type, however the function I have depends on a custom enumeration (ObjectType). Would this way still work? Would there be a way to create a type using a string?

            I love to program!

            S Offline
            S Offline
            Stefan Troschuetz
            wrote on last edited by
            #5

            allenmpcx wrote:

            Would there be a way to create a type using a string?

            Activator.CreateInstance


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            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