I have the code behind the code three times in this example. Every time the switch is shutting down the option that is sent to it. Each time the code inside the case is exactly the same, apart from this the parameter is left on the basis of case. Is this the best way to do a switch / case and method? Should I think of using some type of design method to avoid repetition switches / case structure?
string option = dropdownlight. Selected value. Toasting (); Switch (option.ToUpper ()) {case "ALPHA": // repeat code method here; The only change parameter is the brake; Case "Bravo": // repetitive code method here; The only change parameter is the brake; Case "Charlie": // Dual code method here; The only change parameter is the brake; Case "DELTA": // Dual code method here; The only change parameter is the brake; Default: Break; }
The compilers are very good at switching / making case-making; The CLR will probably turn it into a lookup table or something will be faster, so it's recommended to roll your own version like Henk Holterman, what I would recommend. CLR can do a better job than you choose a better algorithm.
If this is the issue of beauty or maintenance, and you have many switches / cases about the same class doing the same thing, then one way to improve it is to use all the procedures related to the same "case" Is included in the frequency of its class, such as:
class MyOption {public static text only MyOption alpha = new MyOption (1, 10, "alpha text"); Public Static Readonly Myoption Bravo = New Prepress (2, 100, "Bravo Text"); Public Static Readonly Mioption Charlie = New Myoption (3, 1000, "Charlie Text"); // ... Other Options ... MyOption default for public static = New MyOption (0, 0, blank); Public MyOption (int id, int value, string text) {this.ID = ID; this. Value = value; this. Text = text; } Get public int id { Private set; } Public int value {get; Private set; } Public string text {get; Private set; In your class / control / page: Fixed MyOption GetOption (string option name) {switch (optionName) {case "ALPHA":} Return MyOption.Alpha; Case "Bravo": Return MyOption.Bravo; Case "Charlie": Return MyOption.Charlie; // ... other options ... Default: MyOption return.Default; }} Private MyOption GetOptionFromDropDown () {string optionName = GetOptionNameFromDropDown (); Return GetOption (optionName); } Private string GetOptionNameFromDropDown () {// ... your code ...}
Then you can start churning events and other methods:
Private Zero Control 1_SomeEvent (Object Sender, EventArgs e) {MyOption Options = GetOptionFromDropDown (); DoSomething (option.ID); } Private Zero Control 2_SomeEvent (Object Sender, EventArgs e) {MyOption Options = GetOptionFromDropDown (); DoSomethingElse (option.Value); }
Of course, this is just a useful pattern, if you have many of these switches / cases that you want to refresh in one. If you have only one switch / case, then you are just going to eliminate many more code, so leave it alone!
Other possibilities to improve include Maintabilityability:
- Change the string in an Enum type (Convert optionName by using Enum.Parse);
- Moving all MyOption / GetOption stuff into your class (if you have many categories / controls / pages that need to be operated on the same set of all options);
- If you really need to call a different method for each, add a method representative for the MyOption class;
- Put a direct reference to MyOption example, if possible, to your DropdownList or other control store.
It's about it It's easy to write, it's easy to understand, it's easy to keep up, if you have many switch / case creation, it will save you time, And it still allows CLR to optimize the best possible. The only cost is small amounts of memory needed to keep those redone areas.
No comments:
Post a Comment