Call to Methods
-
Hi Friends, How can I handle the call to specific methods based on a string value without comapring the string value using any of the control statements . The requirement is explained as follows: I have 3 methods here. 1. CallMethod(String s) 2. ProcessAgencyDataTable() 3. ProcessAuthorityDataTable() Now, I should be able to call the method ProcessAgencyDataTable() when (s="Agency") for CallMethod(String s) and ProcessAuthorityDataTable() when (s="Authority") for CallMethod(String s). I should achieve this without using any of the control statements (like if, if-else, switch etc). The CallMethod() should be able to call the specific method based on the value of s parameter. Note: There will be some similarity in the Method names that will be called.
public void CallMethod(String s) { switch (s) { case "Agency": ProcessAgencyDataTable(); break; case "Authority": ProcessAuthorityDataTable(); break; default: break; } } private void ProcessAgencyDataTable() { // Implementation here } private void ProcessAuthorityDataTable() { // Implementation here }
Thanks for some suggestions.Subrahmanyam K
-
Hi Friends, How can I handle the call to specific methods based on a string value without comapring the string value using any of the control statements . The requirement is explained as follows: I have 3 methods here. 1. CallMethod(String s) 2. ProcessAgencyDataTable() 3. ProcessAuthorityDataTable() Now, I should be able to call the method ProcessAgencyDataTable() when (s="Agency") for CallMethod(String s) and ProcessAuthorityDataTable() when (s="Authority") for CallMethod(String s). I should achieve this without using any of the control statements (like if, if-else, switch etc). The CallMethod() should be able to call the specific method based on the value of s parameter. Note: There will be some similarity in the Method names that will be called.
public void CallMethod(String s) { switch (s) { case "Agency": ProcessAgencyDataTable(); break; case "Authority": ProcessAuthorityDataTable(); break; default: break; } } private void ProcessAgencyDataTable() { // Implementation here } private void ProcessAuthorityDataTable() { // Implementation here }
Thanks for some suggestions.Subrahmanyam K
You can build the method name dynamically and use Reflection to call the method. using System.Reflection; ... string methodName = String.Format("Process{0}DataTable", s); Type t = this.GetType(); BindingFlags flags = BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance; t.InvokeMember(methodName, flags , null, this, null); ...
-
You can build the method name dynamically and use Reflection to call the method. using System.Reflection; ... string methodName = String.Format("Process{0}DataTable", s); Type t = this.GetType(); BindingFlags flags = BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance; t.InvokeMember(methodName, flags , null, this, null); ...
Hi, Thanks a ton for your wonderful suggestion. Regards,
Subrahmanyam K
-
Hi Friends, How can I handle the call to specific methods based on a string value without comapring the string value using any of the control statements . The requirement is explained as follows: I have 3 methods here. 1. CallMethod(String s) 2. ProcessAgencyDataTable() 3. ProcessAuthorityDataTable() Now, I should be able to call the method ProcessAgencyDataTable() when (s="Agency") for CallMethod(String s) and ProcessAuthorityDataTable() when (s="Authority") for CallMethod(String s). I should achieve this without using any of the control statements (like if, if-else, switch etc). The CallMethod() should be able to call the specific method based on the value of s parameter. Note: There will be some similarity in the Method names that will be called.
public void CallMethod(String s) { switch (s) { case "Agency": ProcessAgencyDataTable(); break; case "Authority": ProcessAuthorityDataTable(); break; default: break; } } private void ProcessAgencyDataTable() { // Implementation here } private void ProcessAuthorityDataTable() { // Implementation here }
Thanks for some suggestions.Subrahmanyam K
I don't know what this is for, but it could be that this could be better designed - for example, having separate classes or object instances for operations on each type of table. Then you could say:
agencyDataSource.ProcessTable();
authorityDataSource.ProcessTable();In either case, you can have a dictionary to store the methods or objects by name:
//the object way
Dictionary<string, MyDataSourceObject> tableObjs = new Dictionary<string, MyDataSourceObject>();
...
tableObjs["Agency"]=agencyDataSource;
tableObjs["Authority"]=authorityDataSource;
...
tableObjs["Agency"].ProcessTable();
tableObjs["Authority"].ProcessTable();//the method way
delegate void EmptyDelegate();
Dictionary<string, EmptyDelegate> methods = new Dictionary<string, EmptyDelegate>();
...
methods["Agency"]=ProcessAgencyDataTable;
methods["Authority"]=ProcessAuthorityDataTable;
...
methods["Agency"]();
methods["Authority"]();Or you could do it using Reflection like Brady mentioned but it's slower as far as perf.
--Justin Microsoft MVP, C#
C# / Web / VG.net / MyXaml expert currently looking for (telecommute) contract work![^]
-
I don't know what this is for, but it could be that this could be better designed - for example, having separate classes or object instances for operations on each type of table. Then you could say:
agencyDataSource.ProcessTable();
authorityDataSource.ProcessTable();In either case, you can have a dictionary to store the methods or objects by name:
//the object way
Dictionary<string, MyDataSourceObject> tableObjs = new Dictionary<string, MyDataSourceObject>();
...
tableObjs["Agency"]=agencyDataSource;
tableObjs["Authority"]=authorityDataSource;
...
tableObjs["Agency"].ProcessTable();
tableObjs["Authority"].ProcessTable();//the method way
delegate void EmptyDelegate();
Dictionary<string, EmptyDelegate> methods = new Dictionary<string, EmptyDelegate>();
...
methods["Agency"]=ProcessAgencyDataTable;
methods["Authority"]=ProcessAuthorityDataTable;
...
methods["Agency"]();
methods["Authority"]();Or you could do it using Reflection like Brady mentioned but it's slower as far as perf.
--Justin Microsoft MVP, C#
C# / Web / VG.net / MyXaml expert currently looking for (telecommute) contract work![^]
Thanks Justin, When looking for the performance, your suggestion would serve better. I will have to make a trade-off which way best suits for my Application. Anyway, Thank you once again for your valuable suggestion. Regards,
Subrahmanyam K
-
You can build the method name dynamically and use Reflection to call the method. using System.Reflection; ... string methodName = String.Format("Process{0}DataTable", s); Type t = this.GetType(); BindingFlags flags = BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance; t.InvokeMember(methodName, flags , null, this, null); ...
-
Hi Friends, How can I handle the call to specific methods based on a string value without comapring the string value using any of the control statements . The requirement is explained as follows: I have 3 methods here. 1. CallMethod(String s) 2. ProcessAgencyDataTable() 3. ProcessAuthorityDataTable() Now, I should be able to call the method ProcessAgencyDataTable() when (s="Agency") for CallMethod(String s) and ProcessAuthorityDataTable() when (s="Authority") for CallMethod(String s). I should achieve this without using any of the control statements (like if, if-else, switch etc). The CallMethod() should be able to call the specific method based on the value of s parameter. Note: There will be some similarity in the Method names that will be called.
public void CallMethod(String s) { switch (s) { case "Agency": ProcessAgencyDataTable(); break; case "Authority": ProcessAuthorityDataTable(); break; default: break; } } private void ProcessAgencyDataTable() { // Implementation here } private void ProcessAuthorityDataTable() { // Implementation here }
Thanks for some suggestions.Subrahmanyam K