Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Call to Methods

Call to Methods

Scheduled Pinned Locked Moved C#
question
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Subrahmanyam K
    wrote on last edited by
    #1

    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

    B J A 3 Replies Last reply
    0
    • S 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

      B Offline
      B Offline
      Brady Kelly
      wrote on last edited by
      #2

      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); ...

      S S 2 Replies Last reply
      0
      • B Brady Kelly

        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); ...

        S Offline
        S Offline
        Subrahmanyam K
        wrote on last edited by
        #3

        Hi, Thanks a ton for your wonderful suggestion. Regards,

        Subrahmanyam K

        1 Reply Last reply
        0
        • S 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

          J Offline
          J Offline
          J Dunlap
          wrote on last edited by
          #4

          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![^]

          S 1 Reply Last reply
          0
          • J J Dunlap

            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![^]

            S Offline
            S Offline
            Subrahmanyam K
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • B Brady Kelly

              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); ...

              S Offline
              S Offline
              soneliso
              wrote on last edited by
              #6

              This is cool i helped me too. Because i have about 10 methods to load on details on Form load example ReturnPersonalDetails ReturnEducationalDetails ReturnProfileDetails ReturnWordDetails Thanks again

              1 Reply Last reply
              0
              • S 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

                A Offline
                A Offline
                AFSEKI
                wrote on last edited by
                #7

                Create: 1) Dictionary

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups