2

I have the following method which takes a type of class as a parameter:

public void test(Type proType){ } 

I currently have a large if else which looks like:

if(proType == typeof(Class)){} 

As there is about ten this looks untidy.

I tried turning this in to a switch but couldn't get it to work.

Is there any better practice to this or away to get a switch statement to work?

 switch (proType) { case typeof(ClassName): break; } 

"A constant value required"

The function is been called like test(typeof(class))

So the aim is i have a Big object which contains many small classes.

The typeof(class) switch/if statement allows me to decide what container to go in to get the objects out.

6
  • 1
    Put you code here, it's probably something stupid, a switch should work without any problem Commented Oct 1, 2013 at 13:48
  • 1
    What is the purpose of method test(Type proType)? How is it called? Are you doing something like test(typeof(someObj))? How about encapsulating the code for each case in a known interface method on each of the types you are testing? Commented Oct 1, 2013 at 13:49
  • probably should be looking for a generic solution Commented Oct 1, 2013 at 13:50
  • switch doesn't work on an object of type Type - "expected integral type" error Commented Oct 1, 2013 at 13:50
  • 5
    Switching on type is usually a good sign of a broken design. Object Oriented languages usually offer a better way. Commented Oct 1, 2013 at 13:51

4 Answers 4

7

So, how about making all the objects that you are testing share a common interface?

 interface ITestable { void DoSomething(); } 

and each object implements this interface differently:

 class MySomething : ITestable { public void DoSomething() { //type specific implementation } } class MyOtherSomething : ITestable { public void DoSomething() { //type specific implementation } } 

Now:

 foreach(ITestable testable in myTestablesList) { testable.DoSomething(); } 

and all your switching logic disappears. Tada!

Sign up to request clarification or add additional context in comments.

Comments

3

What is it that you are really trying to achieve. I would guess that 9 out of 10 times, when you are switching over the type of some object, your design is flawed. Virtual dispatch or polymorphism (or both) are what you are really looking for in most of these cases, but without knowing what the problem is that you are trying to solve, one cannot say for certain.

1 Comment

Updated question with more details
3

I normally go with a Dictionary to create a cache of actions to take for each type; load it up at startup if types are known up front, or use TryGetValue and populate when it fails.

1 Comment

Which is just what a switch statement does. The two solutions are pretty much identical.
1

You can use switch(Type.GetTypeCode(proType)) if the types you're interested in are in the TypeCode enum. For example, from the docs:

static void WriteObjectInfo(object testObject) { TypeCode typeCode = Type.GetTypeCode( testObject.GetType() ); switch( typeCode ) { case TypeCode.Boolean: Console.WriteLine("Boolean: {0}", testObject); break; case TypeCode.Double: Console.WriteLine("Double: {0}", testObject); break; default: Console.WriteLine("{0}: {1}", typeCode.ToString(), testObject); break; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.