1

I made 2 forms in C# and I need to export some variables in one of them and import them to another. I keep hitting google but All I get is something about 'Environment.GetEnvironmentVariable'. Is this the right method to do?

  • 1
    why dont you create a third class, and store and read variables from that class? You would need to create a singleton object to that class. – Kaveesh Sep 03 '21 at 08:27
  • *Is this the right method to do?* - no. Think about literally every other time in C# that you passed data around inside your app. Look at: `Console.WriteLine("Hello world")` - did we load "Hello world" into a system environment variable and then call `Console.WriteLine()` and it went and picked the data up out of the environment variable? No.. Forms are not special; theyre classes like everything else. They can have properties you can assign data to (they do already: `yourForm.Text = "form title"`). They can have methods you call and pass data to (they do already: `form.Show(ownerForm)`) – Caius Jard Sep 03 '21 at 08:55
  • So all you need to do is provide methods or properties on your form B and call them from your form A, passing the relevant data – Caius Jard Sep 03 '21 at 08:56
  • Thank you @CaiusJard! That makes a lot of sense! – Irimia Nicolae Sep 03 '21 at 09:09

2 Answers2

0

You can pass your value one form to another form. you don't need to pass variable.

Exporter Activity code:

Intent i = new Intent(this,typeof(Activity2));      
i.PutExtra("Name",txt_Name.Text.ToString());    
StartActivity(i);

Importer Activity code:

string name = Intent.GetStringExtra("Name");    
txt_Result.Text ="Hello, "+name;
Kayes Fahim
  • 672
  • 5
  • 16
0

I'm not sure what do you want, but you could pass arguments like:

Form form2 = new Form(argument); //pass an argument
form2.Show();
this.Hide();

And in the second form is like:

public Form2(string argument){
    InitializeComponent();
    // do whatever you want with passed argument
}

Or you can create a public static variable, like:

// in the main form
public static int value = 2;

And in the second form:

int value2 = Form.value;

I hope this helps you

ToTosty
  • 36
  • 5