Trying to retrieve a variable from a program
-
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:
-
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:
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.
-
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.
-
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!
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.
-
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.
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.
-
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.
-
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)
{
-
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:
-
Hi, Change your method to return the value directly
public static **void** ConnectToDMV(string TranRec)
public static **string** ConnectToDMV(string TranRec)
Alan.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