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. passing a value from one method to another method without calling the method, infact no relation exists between two methods, just value is needed.

passing a value from one method to another method without calling the method, infact no relation exists between two methods, just value is needed.

Scheduled Pinned Locked Moved C#
database
6 Posts 4 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
    suni_dotnet
    wrote on last edited by
    #1

    Hi, i have a senario, 3 buttons and 2 gridviews.The buttons(three buttons) related to the information in first Gridview is clicked, it populates the second gridview with the corresponding table in database. Now my_method needs to know which button is clicked, so that it uses stored procedure to do some database operations on that particular table. Is there any way to get the value from button_clicked() to my_method, without calling my_method. The value will be used only when my_method() is called by some other operation. Thanks

    C C 2 Replies Last reply
    0
    • S suni_dotnet

      Hi, i have a senario, 3 buttons and 2 gridviews.The buttons(three buttons) related to the information in first Gridview is clicked, it populates the second gridview with the corresponding table in database. Now my_method needs to know which button is clicked, so that it uses stored procedure to do some database operations on that particular table. Is there any way to get the value from button_clicked() to my_method, without calling my_method. The value will be used only when my_method() is called by some other operation. Thanks

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      You could store the information in a field on the class in which the method resides.

      Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

      S 1 Reply Last reply
      0
      • S suni_dotnet

        Hi, i have a senario, 3 buttons and 2 gridviews.The buttons(three buttons) related to the information in first Gridview is clicked, it populates the second gridview with the corresponding table in database. Now my_method needs to know which button is clicked, so that it uses stored procedure to do some database operations on that particular table. Is there any way to get the value from button_clicked() to my_method, without calling my_method. The value will be used only when my_method() is called by some other operation. Thanks

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        sounds like you want to use a delegate.

        Christian Graus Driven to the arms of OSX by Vista.

        realJSOPR 1 Reply Last reply
        0
        • C Christian Graus

          sounds like you want to use a delegate.

          Christian Graus Driven to the arms of OSX by Vista.

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          You're starting to sound like "Clippy".

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          1 Reply Last reply
          0
          • C Colin Angus Mackay

            You could store the information in a field on the class in which the method resides.

            Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

            S Offline
            S Offline
            suni_dotnet
            wrote on last edited by
            #5

            Hi, class { protected void Button1_Click(object sender, EventArgs e) { resbutton = "abcMenu"; ..... } protected void Button2_Click(object sender, EventArgs e) { resbutton = "defMenu"; ..... } protected void Button3_Click(object sender, EventArgs e) { resbutton = "jklMenu"; ..... } protected void Button4_Click(object sender, EventArgs e) { resbutton; // here i need the value of resbutton for different above button_click events ..... } } fields are not working properly. can u provide me some related code. Thanks

            C 1 Reply Last reply
            0
            • S suni_dotnet

              Hi, class { protected void Button1_Click(object sender, EventArgs e) { resbutton = "abcMenu"; ..... } protected void Button2_Click(object sender, EventArgs e) { resbutton = "defMenu"; ..... } protected void Button3_Click(object sender, EventArgs e) { resbutton = "jklMenu"; ..... } protected void Button4_Click(object sender, EventArgs e) { resbutton; // here i need the value of resbutton for different above button_click events ..... } } fields are not working properly. can u provide me some related code. Thanks

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              suni_dotnet wrote:

              fields are not working properly.

              I don't see any evidence from this code that you have a field at all. Here is your code re-written:

              class MyForm
              {
              private string resbutton; // This is the field

              protected void Button1_Click(object sender, EventArgs e)
              {
              resbutton = "abcMenu";
              // .....
              }

              protected void Button2_Click(object sender, EventArgs e)
              {
              resbutton = "defMenu";
              // .....
              }

              protected void Button3_Click(object sender, EventArgs e)
              {
              resbutton = "jklMenu";
              // .....
              }

              protected void Button4_Click(object sender, EventArgs e)
              {
              switch(resbutton)
              {
              case "abcMenu":
              // Do stuff based on Button1 being pressed
              break;
              case "defMenu":
              // Do stuff based on Button2 being pressed
              break;
              case "jklMenu":
              // Do stuff based on Button3 being pressed
              break;
              default:
              // Do suff based on none of the buttons being pressed
              break;
              }
              }
              }

              Of course there may be better ways of doing this, but since we don't know what your actual overall goal is we can't say. Christian's suggestion of using delegates has merit also. I'd also suggest using something other than a string for the field. Creating a specific enum that enumerates each type of button would be good.

              Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

              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