fguihen Posted November 27, 2006 Posted November 27, 2006 im writing an app that scripts a database out to a directory. the user can select to only script tables, or permissions, or sprocs, or all. i dont wana have code like: if(scripttables == true) { //script tables } if(scriptSprocs == true) { //script sprocs } if(scriptPermissions == true)....... that could go on forever, depending on how many groups of things you want to script. can delegates be used or something that looks a little better? Quote
mskeel Posted November 27, 2006 Posted November 27, 2006 Sadly I believe you are going to have to perform those if checks somewhere. You might be able to defer them to different methods or objects, but the if check will still have to be done because you could have any combination of the various elements. Delegates are just pointers to functions and would not be helpful here as far as I can tell. I suggest you do your best to make it as readable as possible. I'd start by dropping the "== true" for booleans. It's redundant. Possibly add comments and whitespace, and modularize as much as possible by moving functionality within the if statements into methods. It might also be possible to redesign the logic behind the booleans. Perhaps you can implicitly infer that scripttables is false if there are not any tables? or something like that. Quote
fguihen Posted November 27, 2006 Author Posted November 27, 2006 cool enough. just looks a bit messy. thought there might be a "sexy" way to do it! Quote
MrPaul Posted November 27, 2006 Posted November 27, 2006 Delegates approach If the number of boolean conditions were to increase drastically, then you could theoretically store a delegate to each "script x" method in a collection and then iterate through them, invoking the methods which are set by your boolean conditions. public class DelegateExample { private delegate void ScriptProdecure(); public void GenerateScripts(bool[] options) { ScriptProcedure[] procs = {ScriptTables, ScriptSProcs, ScriptPermissions}; //Iterate through options and if true, invoke appropriate proc for (int i = 0; i < options.Length; i++) { if (options[i]) procs[i](); } } private void ScriptTables() {} private void ScriptSProcs() {} private void ScriptPermissions() {} //Test example public static void Main(string[] args) { DelegateExample d = new DelegateExample(); //Script tables and permissions d.GenerateScripts(new bool[] {true, false, true}); } } Then all you would need to do to add a new scripting procedure would be to define the method, and add it to the procs array. Note that the above code does not verify that the options array is the same length as the procs array. I would only advocate this approach if you have many more boolean conditions - for just 3 it is certainly overkill. Good luck :cool: Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.