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. Get return value from one class to another

Get return value from one class to another

Scheduled Pinned Locked Moved C#
csharpwinformshelpquestion
14 Posts 6 Posters 1 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.
  • M Martin 0

    Easy would be, if the Form1 creates the Form2. Form1 code. private Form2 f2; //Constructorcode f2 = new Form2(); f2.Show(); Than you can set every public Method or Variable (public int m_nSessionId) from your Form1. this.f2.m_SessionId = 551; or this.f2.SessionID(551); All the best, Martin

    S Offline
    S Offline
    Saamir
    wrote on last edited by
    #4

    Thank you Martin for your reply. I tried what you suggested and no luck. Here is what I have after your suggestion: Form 1 m_nSessionId=processObj.SessionID; if(m_nSessionId!=-1) { string test=m_nSessionId.ToString(); MessageBox.Show(this,test);//This does show the value that I want ProcessReport report = new ProcessReport(); report.ShowDialog(); report.SessionId=m_nSessionId;//Your suggestion pnlProgress.Visible=false; } Form 2 When I try to output SessionId it shows 0 string test=SessionId.ToString(); MessageBox.Show(this,test); Please help!!! sasa

    A M 2 Replies Last reply
    0
    • S Saamir

      Thank you Martin for your reply. I tried what you suggested and no luck. Here is what I have after your suggestion: Form 1 m_nSessionId=processObj.SessionID; if(m_nSessionId!=-1) { string test=m_nSessionId.ToString(); MessageBox.Show(this,test);//This does show the value that I want ProcessReport report = new ProcessReport(); report.ShowDialog(); report.SessionId=m_nSessionId;//Your suggestion pnlProgress.Visible=false; } Form 2 When I try to output SessionId it shows 0 string test=SessionId.ToString(); MessageBox.Show(this,test); Please help!!! sasa

      A Offline
      A Offline
      Alexander Wiseman
      wrote on last edited by
      #5

      sasa, ShowDialog is a synchronous function, which means it will only return and execute code after it when the form is closed. Using this function to display the form is a good idea if you want to force the user to respond to that form before continuing manipulation of the main form. If you are using this function, however, you will need to set all your variables before you call that function, like this:

      report.SessionId = m_nSessionId;
      report.ShowDialog();

      Your code which you have does it in the reverse order, which is why it does not work. Now Martin had suggested setting the variables after the call to the function called Show, which actually displays the form and returns immediately. His code works fine because the call to Show returns immediately. Depending on whether you want the user to be able to manipulate both forms at the same time or whether you want to force the user to manipulate only the form you display until the user closes it, you can use Show or ShowDialog. If you use the latter, you will need to set the variables before the call to that function. Hope that helps! Sincerely, Alexander Wiseman

      S 1 Reply Last reply
      0
      • S Saamir

        Thank you Martin for your reply. I tried what you suggested and no luck. Here is what I have after your suggestion: Form 1 m_nSessionId=processObj.SessionID; if(m_nSessionId!=-1) { string test=m_nSessionId.ToString(); MessageBox.Show(this,test);//This does show the value that I want ProcessReport report = new ProcessReport(); report.ShowDialog(); report.SessionId=m_nSessionId;//Your suggestion pnlProgress.Visible=false; } Form 2 When I try to output SessionId it shows 0 string test=SessionId.ToString(); MessageBox.Show(this,test); Please help!!! sasa

        M Offline
        M Offline
        Mike Poz
        wrote on last edited by
        #6

        You didn't do what Martin suggested and so it fails because in this:

        SASA_1 wrote:

        Form 2 When I try to output SessionId it shows 0 string test=SessionId.ToString(); MessageBox.Show(this,test);

        You're assuming that "SessionID" from Form 1 is global and it's not. What you're not entirely clear on is the architecture on how Form 1 and Form 2 are actually created. Do they both get created in Form 0 or does Form 2 get spawned from Form 1? How are they created? That will dictate how you pass the data between the two. Generally the easiest way is through the use of a public variable created at a level that makes it global. Mike Poz

        R 1 Reply Last reply
        0
        • A Alexander Wiseman

          sasa, ShowDialog is a synchronous function, which means it will only return and execute code after it when the form is closed. Using this function to display the form is a good idea if you want to force the user to respond to that form before continuing manipulation of the main form. If you are using this function, however, you will need to set all your variables before you call that function, like this:

          report.SessionId = m_nSessionId;
          report.ShowDialog();

          Your code which you have does it in the reverse order, which is why it does not work. Now Martin had suggested setting the variables after the call to the function called Show, which actually displays the form and returns immediately. His code works fine because the call to Show returns immediately. Depending on whether you want the user to be able to manipulate both forms at the same time or whether you want to force the user to manipulate only the form you display until the user closes it, you can use Show or ShowDialog. If you use the latter, you will need to set the variables before the call to that function. Hope that helps! Sincerely, Alexander Wiseman

          S Offline
          S Offline
          Saamir
          wrote on last edited by
          #7

          Hi Alexander; Thank you for your suggestion, I learned something new today. But, my code still gives me a 0 value for m_nSessionId; when the variable has a value. In my Form 2, All I have is Public SessionId; and then I have a load function which manipulates data based on the m_nSessionId. With a value of 0 it doesn't do anything. Please help!! and please don't mind my ignorance. sasa

          A 1 Reply Last reply
          0
          • S Saamir

            Hi Alexander; Thank you for your suggestion, I learned something new today. But, my code still gives me a 0 value for m_nSessionId; when the variable has a value. In my Form 2, All I have is Public SessionId; and then I have a load function which manipulates data based on the m_nSessionId. With a value of 0 it doesn't do anything. Please help!! and please don't mind my ignorance. sasa

            A Offline
            A Offline
            Alexander Wiseman
            wrote on last edited by
            #8

            Sorry I must have misunderstood something. When you say that you still have a value of 0 for m_nSessionId, where in the code do mean this? Inside any code of Form2, you should be accessing SessionId, the public variable which you set equal to m_nSessionId before you call ShowDialog. Inside the load function (which I presume is on Form2), you should be accessing the SessionId variable, not m_nSessionId, which I thought belonged to Form1. Could you clarify that, and maybe post the section of your Form2 loading code that tries to look at the session ID? Sincerely, Alexander Wiseman

            S 1 Reply Last reply
            0
            • M Mike Poz

              You didn't do what Martin suggested and so it fails because in this:

              SASA_1 wrote:

              Form 2 When I try to output SessionId it shows 0 string test=SessionId.ToString(); MessageBox.Show(this,test);

              You're assuming that "SessionID" from Form 1 is global and it's not. What you're not entirely clear on is the architecture on how Form 1 and Form 2 are actually created. Do they both get created in Form 0 or does Form 2 get spawned from Form 1? How are they created? That will dictate how you pass the data between the two. Generally the easiest way is through the use of a public variable created at a level that makes it global. Mike Poz

              R Offline
              R Offline
              Rojan Gh
              wrote on last edited by
              #9

              Sorry to meddle! But why don't you use the events? Try this: 1)Create an EventArgs class. 2)A delegate. 3)An event of type your delegate. 4)Raise the event and pass the SessionId as the EventArgs Property. Read this article and contact me if you had any problem.:) EventArgs Class Sojaner!

              1 Reply Last reply
              0
              • A Alexander Wiseman

                Sorry I must have misunderstood something. When you say that you still have a value of 0 for m_nSessionId, where in the code do mean this? Inside any code of Form2, you should be accessing SessionId, the public variable which you set equal to m_nSessionId before you call ShowDialog. Inside the load function (which I presume is on Form2), you should be accessing the SessionId variable, not m_nSessionId, which I thought belonged to Form1. Could you clarify that, and maybe post the section of your Form2 loading code that tries to look at the session ID? Sincerely, Alexander Wiseman

                S Offline
                S Offline
                Saamir
                wrote on last edited by
                #10

                Hi Alexander; Thank you for helping me with this. Here is the code snippet: Form1. m_nSessionId=processObj.SessionID; if(m_nSessionId!=-1) { //string test=m_nSessionId.ToString(); //MessageBox.Show(this,test);//m_nSessiodId at this point is holding a value. ProcessReport report = new ProcessReport();//creating form2 object and assigning m_nSessiodId to public variable report.SessionId=m_nSessionId; report.ShowDialog(); } form 2 //creating SessionId variable public long SessionId; //Function in form 2 where I need the value of m_nSessionId from form1 private void LoadReport() try { if(SessionId!=-1) { cryShared.TableLogOnInfo tbLogInfo=null; CBResult rpt=new CBResult(); foreach ( cryEngine.Table tb in rpt.Database.Tables) { tbLogInfo=tb.LogOnInfo; tbLogInfo.ConnectionInfo.ServerName=m_sServer; tbLogInfo.ConnectionInfo.DatabaseName=m_sDBName; tbLogInfo.ConnectionInfo.UserID=m_sUserId; tbLogInfo.ConnectionInfo.Password=m_sPassword; tb.ApplyLogOnInfo(tbLogInfo); tb.Location="\"" + m_sDBName+".dbo.\"" + tb.Location.Substring(tb.Location.LastIndexOf(".")+1); } cryShared.ParameterValues pvs=new cryShared.ParameterValues(); cryShared.ParameterDiscreteValue pv=new cryShared.ParameterDiscreteValue(); pv.Value=SessionId; pvs.Add(pv); rpt.DataDefinition.ParameterFields["Ad_Parameter1"].ApplyCurrentValues(pvs); crystalReportViewer1.ReportSource=rpt; } } Thank you again for all your help. sasa

                A 1 Reply Last reply
                0
                • S Saamir

                  Hi Alexander; Thank you for helping me with this. Here is the code snippet: Form1. m_nSessionId=processObj.SessionID; if(m_nSessionId!=-1) { //string test=m_nSessionId.ToString(); //MessageBox.Show(this,test);//m_nSessiodId at this point is holding a value. ProcessReport report = new ProcessReport();//creating form2 object and assigning m_nSessiodId to public variable report.SessionId=m_nSessionId; report.ShowDialog(); } form 2 //creating SessionId variable public long SessionId; //Function in form 2 where I need the value of m_nSessionId from form1 private void LoadReport() try { if(SessionId!=-1) { cryShared.TableLogOnInfo tbLogInfo=null; CBResult rpt=new CBResult(); foreach ( cryEngine.Table tb in rpt.Database.Tables) { tbLogInfo=tb.LogOnInfo; tbLogInfo.ConnectionInfo.ServerName=m_sServer; tbLogInfo.ConnectionInfo.DatabaseName=m_sDBName; tbLogInfo.ConnectionInfo.UserID=m_sUserId; tbLogInfo.ConnectionInfo.Password=m_sPassword; tb.ApplyLogOnInfo(tbLogInfo); tb.Location="\"" + m_sDBName+".dbo.\"" + tb.Location.Substring(tb.Location.LastIndexOf(".")+1); } cryShared.ParameterValues pvs=new cryShared.ParameterValues(); cryShared.ParameterDiscreteValue pv=new cryShared.ParameterDiscreteValue(); pv.Value=SessionId; pvs.Add(pv); rpt.DataDefinition.ParameterFields["Ad_Parameter1"].ApplyCurrentValues(pvs); crystalReportViewer1.ReportSource=rpt; } } Thank you again for all your help. sasa

                  A Offline
                  A Offline
                  Alexander Wiseman
                  wrote on last edited by
                  #11

                  Well your code looks fine, so the only thing I can think of is that LoadReport is being called before you set the session ID - that is, it is being called from the constructor of form 2. If you are calling LoadReport from the constructor of form 2, then SessionId will not have the proper value in it, because you are setting its value only after the constructor is finished. So the main question is: when are you calling LoadReport in Form2? If you are calling it in the constructor of Form2, then I recommend instead that you make an event handler for the Form.Load event and in that, call the function LoadReport. Let me know if that gets you anywhere. Sincerely, Alexander Wiseman

                  S 1 Reply Last reply
                  0
                  • A Alexander Wiseman

                    Well your code looks fine, so the only thing I can think of is that LoadReport is being called before you set the session ID - that is, it is being called from the constructor of form 2. If you are calling LoadReport from the constructor of form 2, then SessionId will not have the proper value in it, because you are setting its value only after the constructor is finished. So the main question is: when are you calling LoadReport in Form2? If you are calling it in the constructor of Form2, then I recommend instead that you make an event handler for the Form.Load event and in that, call the function LoadReport. Let me know if that gets you anywhere. Sincerely, Alexander Wiseman

                    S Offline
                    S Offline
                    Saamir
                    wrote on last edited by
                    #12

                    Thank you again. I am calling it from the constructor and with that said, I am sorry man but how do I create and event handler? Please give an example? Thanks again sasa -- modified at 22:33 Wednesday 28th June, 2006

                    S 1 Reply Last reply
                    0
                    • S Saamir

                      Thank you again. I am calling it from the constructor and with that said, I am sorry man but how do I create and event handler? Please give an example? Thanks again sasa -- modified at 22:33 Wednesday 28th June, 2006

                      S Offline
                      S Offline
                      Saamir
                      wrote on last edited by
                      #13

                      Hi Alexander; I created an event handler and got it to work. Thank you man, I really really appreciate your help. You are my guru. sasa

                      A 1 Reply Last reply
                      0
                      • S Saamir

                        Hi Alexander; I created an event handler and got it to work. Thank you man, I really really appreciate your help. You are my guru. sasa

                        A Offline
                        A Offline
                        Alexander Wiseman
                        wrote on last edited by
                        #14

                        Excellent! I'm glad it worked out for you. :cool: Sincerely, Alexander Wiseman

                        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