3

I will appreciate if anyone can help me on this.

I have a windows form app that has three forms: form1, form2, form3. form1 starts when the app is activated. on form1, there is a button that brings up form2, and hide form1. there is also one button that brings up form3 and hides form2 on form2.

public partial class Form1 : Form
{

    Form2 f2= new Form2();
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();            
        f2.Show();        
    }
}


public partial class Form2 : Form
{
            Form3 f3 = new Form3();
    private void button1_Click(object sender, EventArgs e)
    {
         this.Hide();
         f3.Show();                
    }
 }

The question is on form3, i tried to access some of the variables that are assigned with values on runtime in form2. I think since i make f2 as modaless form, i should be able to access by simply using f2.myvariables, but the intellisense does not give me f2 object. Why is that? I found a way to declare those variables public static, so i could access by using form2.myvariables..Here is another thing that confuses me. Since all the values are assigned during runtime, how could static variable do this? I am a newbie on C#, and i already did a lot of searches on this, but seems no place answers my question exactly. Thanks for help in advance!!

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
John
  • 190
  • 1
  • 4
  • 15
  • I dont know how to fix the bad format...sorry..new here – John Aug 20 '12 at 17:37
  • 2
    Possible duplicte of: http://stackoverflow.com/questions/2155050 – Uwe Keim Aug 20 '12 at 17:37
  • These are programming fundamentals that are a little overly broad to try to answer in an SO post (IHMO). – CodingGorilla Aug 20 '12 at 17:40
  • Your form classes may be in different namespaces. For intelesense to access the properties you would need to add a 'using' statement at the top of your class with those names spaces in them. – Belmiris Aug 20 '12 at 17:41
  • This all confusion starts when u **think** of form as something `different` than a `class`.Form **is a** `class`. **Think how would you access a variable of another class** – Anirudha Aug 20 '12 at 17:44
  • This question was closed yet the answer may very well have helped with my current issue. – ptay Nov 21 '16 at 20:03

4 Answers4

11

So you have information in the parent form (form2) that you want to access in a method 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 of this not as having the child form ask for information from it's parent, but rather that the parent is giving information to its child. If you shift your mindset accordingly the code becomes not only easier to write, but also will be more in line with good coding practices (lower coupling, not exposing more information externally than needed, etc.)

To create a property you can do something like 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;
Servy
  • 202,030
  • 26
  • 332
  • 449
  • Hi Servy, Thanks for your post. I dont know how to use properties to solve this? is it like declare constructors in form3, so when create an instance of form3 in form2, pass all the variables thru constructors? – John Aug 20 '12 at 18:47
  • @John That's certainly a perfectly valid alternative to this solution that's almost exactly the same. I'll edit to show the syntax for creating a property. – Servy Aug 20 '12 at 18:48
  • Thanks so much..Could you tell me why f2 instance is not accessible in f3? is it because child form cant access parent form? Also, why making those variables public static could also do this? I mean those variables are assigned with values that are bases on user input during runtime. based on my understanding, public static only work for values that are not going to change during runtime. Sorry for so many questions. I just want to understand the whole thing rather than just solve the problem. – John Aug 20 '12 at 18:57
  • @John In your original code `f2` is a private instance variable, which means it can only be accessed from within that class by non-static methods. If you make it public (which you shouldn't do, as it's considered bad practice to use public instance variables) then you would be able to access it if you had an instance of `Form1` (which is the class that has the `f2` variable). The problem now is that you don't have an instance of Form1, so you still can't access it. If `f2` is static, then you don't need an instance of `Form1` to access it's fields, you just need the class name. – Servy Aug 20 '12 at 19:02
  • As I have said elsewhere though, you don't want to do that. These form variables should be both non-static and private. You should use public properties on `Form3`, or parameters in the constructor to pass the values rather than making them static or exposing the various form variables even more. – Servy Aug 20 '12 at 19:03
1

Man,

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

Rodrigo Reis
  • 1,097
  • 12
  • 21
  • thanks. I figure that would work, but why the parent form (f2) instance is not accessible in child form (f3) instance? – John Aug 20 '12 at 18:49
  • @John because it is out of scope. Would work if you build a link between the two Form objects, simply creating a parent property of type Form in Form3, and when you call the constructor of Form3 assign to this property the form2 `form3.Parent = form2` – Rodrigo Reis Aug 20 '12 at 19:01
  • Note that doing what Rodrigo suggested is generally considered a bad practice. Just like making everything `static` it involves exposing more information publicly than should be exposed. – Servy Aug 20 '12 at 19:04
  • Ok @Servy be my guest to answer the question. – Rodrigo Reis Aug 20 '12 at 19:08
  • @RodrigoReis I was referring to the comment of passing a reference of the parent form, which is just bad code smell in my eyes, not to the content of your answer (which is a great solution +1). Oh, and for the record, I posted an answer to this question a while ago. – Servy Aug 20 '12 at 19:13
  • Microsoft Documentation do the same thing see @ [ContainerControl.ParentForm Property](http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.parentform.aspx) – Rodrigo Reis Aug 20 '12 at 19:16
0

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

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

Brian S
  • 5,675
  • 1
  • 22
  • 22
  • And per the link shared by @Uwe Keim's above, I'd highly recommend making them public properties, not public variables. – Brian S Aug 20 '12 at 17:42
  • the problem is in form3, f2 does not even exist in intellisense no matter wether the static is removed or not. – John Aug 20 '12 at 18:41
0

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

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

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

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

perilbrain
  • 7,961
  • 2
  • 27
  • 35
  • everything is prefixed with public, and i know static variable is used without the need to create an object of a class, but my confusion is, the values of the variables on form2 can not be pre-determined but assigned based on user input on form2; however in this case, making those variables static do solve the problem...how? – John Aug 20 '12 at 18:45
  • @John Making everything `public static` is just making everything a global variable. In this case, you don't really want that to be the case; it makes sense to have a copy of each variable per form. It only works because there is only one form. While it does work in the simple cases, it's not good practice and it would basically be a crutch preventing you from learning how to do is correctly. – Servy Aug 20 '12 at 18:54