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. Trying to retrieve a variable from a program

Trying to retrieve a variable from a program

Scheduled Pinned Locked Moved C#
csharptestingbeta-testingjsonquestion
9 Posts 3 Posters 3 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.
  • J Offline
    J Offline
    JTRizos
    wrote on last edited by
    #1

    I have a form (form1.cs) that sends a transaction record variable (TranRec) to a program (Transporter.cs) by calling it using Transporter.ConnectToDMV(TranRec);. Transporter.cs does what it needs to do and returns to form1.cs for the user to enter and submit another transaction. Here's part of the Transporter.cs code. Transporter.cs public partial class Transporter { public static void ConnectToDMV(string TranRec) { rest of code processing TranRec ... What I also need Transporter.cs to do is return a variable (logLU) back to form1.cs. For this I am using the following code; Transporter.cs Form1 aForm = new Form1(); aForm.SetValue(logLU); form1.cs public void SetValue(string luValue) { logLU = luValue; // for testing purposes MessageBox.Show("Here's the value of (form1)luValue: " + luValue); MessageBox.Show("Here's the value of (form1)logLU: " + logLU); } I can see both variables in the messageboxes so I know logLU gets to form1.cs but then it returns to null when I need it in other parts of form1.cs. What am I missing??? Similar code works in other parts of my program but not here. BTW, I'm a newbie to C# and this is my first application. Any advise will be very much apprecaited. :confused:

    R A 2 Replies Last reply
    0
    • J JTRizos

      I have a form (form1.cs) that sends a transaction record variable (TranRec) to a program (Transporter.cs) by calling it using Transporter.ConnectToDMV(TranRec);. Transporter.cs does what it needs to do and returns to form1.cs for the user to enter and submit another transaction. Here's part of the Transporter.cs code. Transporter.cs public partial class Transporter { public static void ConnectToDMV(string TranRec) { rest of code processing TranRec ... What I also need Transporter.cs to do is return a variable (logLU) back to form1.cs. For this I am using the following code; Transporter.cs Form1 aForm = new Form1(); aForm.SetValue(logLU); form1.cs public void SetValue(string luValue) { logLU = luValue; // for testing purposes MessageBox.Show("Here's the value of (form1)luValue: " + luValue); MessageBox.Show("Here's the value of (form1)logLU: " + logLU); } I can see both variables in the messageboxes so I know logLU gets to form1.cs but then it returns to null when I need it in other parts of form1.cs. What am I missing??? Similar code works in other parts of my program but not here. BTW, I'm a newbie to C# and this is my first application. Any advise will be very much apprecaited. :confused:

      R Offline
      R Offline
      ricmil42
      wrote on last edited by
      #2

      Form1 aForm = new Form1();
      aForm.SetValue(logLU);

      Looks like you are creating a new Form1 everytime you set this value. The new form will have the value, but any existing form will not. Try making aForm a field instead of a local variable and assigning the value to the field version of aForm.

      J 1 Reply Last reply
      0
      • R ricmil42

        Form1 aForm = new Form1();
        aForm.SetValue(logLU);

        Looks like you are creating a new Form1 everytime you set this value. The new form will have the value, but any existing form will not. Try making aForm a field instead of a local variable and assigning the value to the field version of aForm.

        J Offline
        J Offline
        JTRizos
        wrote on last edited by
        #3

        Thank you for your reply. (Remember newbie) I think I tried that but had syntax issues. What would the code you recommend look like? Thanx again!

        R 1 Reply Last reply
        0
        • J JTRizos

          Thank you for your reply. (Remember newbie) I think I tried that but had syntax issues. What would the code you recommend look like? Thanx again!

          R Offline
          R Offline
          ricmil42
          wrote on last edited by
          #4

          At the top of your code define the form.

          private Form1 aForm ;

          Where you are going to open the form:

          aForm = new Form1 ( ) ;

          Do some stuff here... When you want to return the value to the form:

          aForm.SetValue ( logLU ) ;

          When you are done with the form, dispose of it.

          J 1 Reply Last reply
          0
          • R ricmil42

            At the top of your code define the form.

            private Form1 aForm ;

            Where you are going to open the form:

            aForm = new Form1 ( ) ;

            Do some stuff here... When you want to return the value to the form:

            aForm.SetValue ( logLU ) ;

            When you are done with the form, dispose of it.

            J Offline
            J Offline
            JTRizos
            wrote on last edited by
            #5

            I end up with the same result. Although I had to do the following because the use of private generated an invlaid expression term 'private' error. namespace DMV_Test_1 { public partial class Transporter { public static void ConnectToDMV(string TranRec) { Form1 aForm; Thanx for the suggestion.

            R 1 Reply Last reply
            0
            • J JTRizos

              I end up with the same result. Although I had to do the following because the use of private generated an invlaid expression term 'private' error. namespace DMV_Test_1 { public partial class Transporter { public static void ConnectToDMV(string TranRec) { Form1 aForm; Thanx for the suggestion.

              R Offline
              R Offline
              ricmil42
              wrote on last edited by
              #6

              You need to put it here, outside of any method. Then it will be available anywhere in the class.

              namespace DMV_Test_1
              {
              public partial class Transporter
              {
              Form1 aForm;

                  public static void ConnectToDMV(string TranRec)
              

              {

              J 1 Reply Last reply
              0
              • R ricmil42

                You need to put it here, outside of any method. Then it will be available anywhere in the class.

                namespace DMV_Test_1
                {
                public partial class Transporter
                {
                Form1 aForm;

                    public static void ConnectToDMV(string TranRec)
                

                {

                J Offline
                J Offline
                JTRizos
                wrote on last edited by
                #7

                When I did that I got the following error on the two instances of aForm An object reference is required for the non-static field, method, or property aForm = new Form1(); aForm.SetValue(logLU); Thanx for the suggestion.

                1 Reply Last reply
                0
                • J JTRizos

                  I have a form (form1.cs) that sends a transaction record variable (TranRec) to a program (Transporter.cs) by calling it using Transporter.ConnectToDMV(TranRec);. Transporter.cs does what it needs to do and returns to form1.cs for the user to enter and submit another transaction. Here's part of the Transporter.cs code. Transporter.cs public partial class Transporter { public static void ConnectToDMV(string TranRec) { rest of code processing TranRec ... What I also need Transporter.cs to do is return a variable (logLU) back to form1.cs. For this I am using the following code; Transporter.cs Form1 aForm = new Form1(); aForm.SetValue(logLU); form1.cs public void SetValue(string luValue) { logLU = luValue; // for testing purposes MessageBox.Show("Here's the value of (form1)luValue: " + luValue); MessageBox.Show("Here's the value of (form1)logLU: " + logLU); } I can see both variables in the messageboxes so I know logLU gets to form1.cs but then it returns to null when I need it in other parts of form1.cs. What am I missing??? Similar code works in other parts of my program but not here. BTW, I'm a newbie to C# and this is my first application. Any advise will be very much apprecaited. :confused:

                  A Offline
                  A Offline
                  Alan N
                  wrote on last edited by
                  #8

                  Hi, Change your method to return the value directly public static **void** ConnectToDMV(string TranRec) public static **string** ConnectToDMV(string TranRec) Alan.

                  J 1 Reply Last reply
                  0
                  • A Alan N

                    Hi, Change your method to return the value directly public static **void** ConnectToDMV(string TranRec) public static **string** ConnectToDMV(string TranRec) Alan.

                    J Offline
                    J Offline
                    JTRizos
                    wrote on last edited by
                    #9

                    Tried that but it caused other problems in the code. I fixed the problem by doing the following in form1.cs namespace DMV_Access_App { public partial class Form1 : Form { public static string logLU; public Form1() { Rest of form1.cs code ... This took care of the problem. Seems so simple. Of course, being a newbie, I am not sure what ramifications there are but it does work. This is great forum! :-D

                    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