C# winform: Accessing public properties from other forms & difference between static and public properties

前端 未结 5 688
说谎
说谎 2020-12-03 18:54

I am trying to understand whats the difference between a static and public properties. But when I tried to access my public property \'Test\' in other form it says \'null\'.

相关标签:
5条回答
  • 2020-12-03 19:23

    The first instance of Form1 shows an instance of Form2, and then Form2 creates another instance of Form1. This could work, but you set _test in the Form.Load event, which:

    Occurs before a form is displayed for the first time.

    You do not show the instance of Form1 you're trying to read Test from, so its Load event will not occur and Test remains null.

    You could add a constructor overload or property to pass the Form1 reference as @JohnKoerner mentions, but I would prefer to only pass the required variable, perhaps even encapsulated in an event, to reduce coupling. Form2 usually doesn't need to know all about Form1.

    0 讨论(0)
  • 2020-12-03 19:26

    Your property is an instance variable, so the value can be different across different instances of Form1.

    If you are trying to access instance variables from a parent form, the easiest way to do that is to pass Form1 in to the constructor of Form2.

    public partial class Form2 : Form
    {
        private Form1 f1;
        public Form2(Form1 ParentForm)
        {
            InitializeComponent();
            f1 = ParentForm;
        }
    
        private void Form2_Load(object sender, EventArgs e)
        {
            label1.Text = f1.Test;
        }
    }
    

    Then when you create a new Form2 from Form1, you can do this:

    Form2 frm2 = new Form2(this);
    

    If you want your property to be read only, you can simply not specify a setter:

    public string Test
    {
        get { return _test; }
    }
    
    0 讨论(0)
  • 2020-12-03 19:38

    public

    The type or member can be accessed by any other code in the same assembly or another assembly that references it.

    static

    The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.

    A static class is basically the same as a non-static class, but there is one difference: a static class cannot be externally instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

    However, there is a such thing as a static constructor. Any class can have one of these, including static classes. They cannot be called directly & cannot have parameters (other than any type parameters on the class itself). A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. Looks like this:

    static class Foo() { static Foo() { Bar = "fubar"; }

    public static string Bar { get; set; }
    

    }

    Static classes are often used as services, you can use them like so:

    MyStaticClass.ServiceMethod(...);

    0 讨论(0)
  • 2020-12-03 19:40

    The frm1 not your main form object. It is newly created object where property Test initializes when it loads (in Form1_Load event handler).

    0 讨论(0)
  • 2020-12-03 19:41

    Use of this method 'static'

    At first Control label property Modifiers=Public

    in Program code below

    public static Form1 frm1 = new Form1();
    public static Form2 frm2 = new Form2();
    

    in Form1 code below

    Program.frm2.show();
    

    in Form2 code below

    label1.Text=Program.frm1.text; 
    
    0 讨论(0)
提交回复
热议问题