[ACCEPTED]-Accessing variables in other Windows Form class-forms

Accepted answer
Score: 11

So you have information in the parent form 12 (form2) that you want to access in a method 11 of the child form (form3).

  1. Create properties in form3 for the information that it will need.
  2. When form2 creates an instance of form3 it should set those properties.

You should think 10 of this not as having the child form ask 9 for information from it's parent, but rather 8 that the parent is giving information to 7 its child. If you shift your mindset accordingly 6 the code becomes not only easier to write, but 5 also will be more in line with good coding 4 practices (lower coupling, not exposing 3 more information externally than needed, etc.)

To 2 create a property you can do something like 1 this in form3:

//TODO: give real name; adjust type as needed
public string SomePropertyName { get; set; }

then in form2 you can do:

f3.SomePropertyName = "hello from form2";

or

f3.SomePropertyName = someVariableInForm2;
Score: 1

Man,

Try to create an overload of the constructor 2 method of Form3, passing variable values 1 ​​from form2 as method arguments.

Score: 0

If you have made the variables in question 7 public on Form2, then your issue is that 6 you've also made them static. When you define 5 them as static, you are placing them on the type 4 (Form2) not on the instance (f2).

Remove 3 the static from the variable declaration 2 and they should appear in intellisense for 1 f2.

Score: 0

I think since i make f2 as modaless form, i 12 should be able to access by simply using 11 f2.myvariables, but the intellisense does 10 not give me f2 object. Why is that?

Once 9 you create instance of a class all the variables 8 and methods declared as public should be 7 available.Just recheck if you have declared 6 your variables as public.

Since all the values 5 are assigned during runtime, how could static variable 4 do this?

No, Static variables and methods 3 are defined with the start of the program.They 2 dont need instances to be created to refer 1 them.

More Related questions