0

I have a windows form app with 2 forms, and I need to press a button in form one to go to form 2(this is done already) then form 2 will be able to create an object using the add customer method to add to the system. My question is: 1)if I create an Object in Form 2, how could other forms(form3,form4 etc.) have access to this object? As far as I have learned, I can only call the method through an object. 2)if I created an object in Form1, and other forms inherited from form 1, will this object still work in other forms? 3)Objects can be inhereited or not? is this a good practice in real world? 4) How to allow different forms using one object different method?

Form1

Form2

2
  • It's not clear enough ... post the code sample of what have you tried so far
    – Rahul
    Nov 17, 2018 at 13:37
  • I don't have code to put it, I would like to use CustomerManager aa=new CustomerManager() ; use aa in form 2 with aa.someMethod. but when I declare aa in program.cs, it won't be able to use in form 2 or form 1
    – David Shi
    Nov 17, 2018 at 13:39

2 Answers 2

0

A static field or property as suggested in zdimension's answer is possible, of course, but it shouldn't be your first option. There are lots of ways to pass data between forms, and it depends on your application which one is best. For example, one way of doing it is:

class Form2 : Form
{
    public Form2()
    {
         InitializeComponent();
    }

    public AirlineCoordinator Coordinator {get; set;}

    ...
}

class Form1 : Form
{
    public Form1()
    {
         InitializeComponent();
    }

    public AirlineCoordinator Coordinator {get; set;}

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Coordinator = new AirlineCoordinator(...);

       ...
    }

    ...

    private void ShowForm2Button_Click(object sender, EventArgs e)
    {
        using(var form2 = new Form2())
        {
            form2.Coordinator = this.Coordinator;
            form2.ShowDialog(this);
        }
    }

}

In this hypothetical example, Form1 has a button ShowForm2Button; clicking on this button shows Form2 using the same AirlineCoordinator as is used by Form1.

6
  • do you mean this AirlineCoordinator is a class?then where should I initiate the class with object? both or either?
    – David Shi
    Nov 17, 2018 at 14:08
  • @DavidShi - I've updated it with a correction of a typo which was probably confusing you. AirlineCoordinator is a class. Both Form and Form2 have a public property of type AirlineCoordinator, I've called this property Coordinator in this updated version. Form1 constructs the object (here in the Form1_Load handler, when Form1 is loaded), and passes it to Form2
    – Joe
    Nov 17, 2018 at 15:55
  • does this mean what I input in form2 will automatically go into Class AirlineCoordinator? or I still have to create an object? if so where do I create it, in program.cs or form 1 or form2?
    – David Shi
    Nov 17, 2018 at 18:05
  • If you pass an object around, any changes you make to it will be seen by everyone who has a reference to the object. You can create it where you want: in the example it's created by Form1 when it's loading. Try it out and step through the code with a debugger to learn,
    – Joe
    Nov 17, 2018 at 18:31
  • I am sorry but I am new to C# and I don't know what is your code means. I have updated my question, do you mind take a look again?
    – David Shi
    Nov 18, 2018 at 20:43
0

The usual way to make something available to "everyone" is to use a static field, like this:

public class GlobalStuff
{
    public static MyType SomeVariable;
}

Here, the GlobalStuff obviously only ever contains global things, so you could consider making it static too to indicate it will never be instanciated.

Here's what MSDN say about it:

Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.

3
  • If I want form 2 using CustomerManager, what should I do? initialize CustomerManager aa=new CustomerManager() at program class?and make who public or static?
    – David Shi
    Nov 17, 2018 at 13:37
  • Will there ever be 2 CustomerManager objects or is it some sort of "god object" that manages customers at the app level?
    – zdimension
    Nov 17, 2018 at 13:38
  • I think only one, as if I created two customerManager Objects, the information I stored will be in different object and then I was not able to view or modify them easily
    – David Shi
    Nov 17, 2018 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.