Overuse of switch statement overhead

I have an xml file with specific steps and actions that should be performed on html controls:

<Item control="Dropdown" action="Click" value=""/> <Item control="Button" action="GetText" value=""/> <Item control="Input" action="WriteText" value="Sample Text"/> 

There are many controls that I will use, and each control has several methods, such as Click , GetText , Exsists ..

I am currently using switches to handle this, but I heard that is not a good practice. I have one switch for selecting a specific control, and also each control has a switch for calling a specific method.

 switch (control) { case "button": return new Button; case "table": return new Table; (...) } (...) switch (action) { case "click": this.Click(); return true; case "gettext": this.GetText(); return true; default: return false; } 

I don't have much experience with coding, so I'm not sure if this is a good way to handle this problem. Could you give me some tips on how to do this better? Maybe there are some coding patterns that might be useful here?

+4
source share
5 answers

you can create a class for each type of control that you have

 namespace SomeNamespace { class Button { } public void Click(){ } } 

and then when you read xml

 //assemblyName is the full name of the assembly holding your control classes var type = System.GetType(assemblyName, "SomeNamespace." + control); var control = Activator.CreateInstance(type); //requires a default constructor type.GetMethod(methodName).Invoke(control); //methodName could be click 
0
source

I would suggest exploring the creation of the "Submission Table." Here you can see a C # example: https://stackoverflow.com/questions/715457/how-do-you-implement-a-dispatch-table-in-your-language-of-choice

+2
source

To make it more strongly typed (but it also leads to some overhead in that you have to create classes), you can create classes for each control you want and get serialized / deserialized.

so instead of the element you would have, for example:

 <Button action="Click" value="" /> 

They all inherit from the same base class or implement the same interface, so at the end you will get deserialized IMyControls :)

and you can make a “switch case” depending on the type that they have.

0
source
 public class BaseControl { public object Click(){ } public object GetText() { } public virtual object ExecuteCommand() { } } public class TableControl : BaseControl { public override object ExecuteCommand() { //Do your method return base.GetText(); } } 

Then return TableControl instead of Table and call / override the Execute command, which will call your specific command based on what type it is.

Create another type that inherits your base management class, depending on what you want to do, override the executable and trigger the action. Hope this helps?

0
source

if you can rename the names of the controls according to the names of the classes, you can create instances through reflection;

 string controlName = "System.Windows.Forms.Button"; Assembly assembly = typeof(Form).Assembly; Type type = assembly.GetType(controlName); object controlObject = Activator.CreateInstance(type); 

In addition, you can also refer to methods through reflection;

 MethodInfo methodInfo = controlObject.GetType().GetMethod("Method Name"); methodInfo.Invoke(controlObject,null); 
-1
source

All Articles