简体   繁体   English

C#,从类访问变量?

[英]C#, accessing a variable from a class?

Say i have 说我有

        public int MyVariable;

in the Form1.cs file, and I want to access it from Class1.cs , what do you think would be the best way to do that? 在Form1.cs文件中,我想从Class1.cs访问它,您认为哪种方法最好呢?

Thanks! 谢谢!

MSDN: Properties MSDN:属性

base class with property: 具有属性的基类:

class Person
{
    private string name;  // the name field
    public string Name    // the Name property
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
}

Auto Implemented Properties (if advanced work on "name" isn't needed): 自动实施的属性 (如果不需要“名称”的高级工作):

class Person
{
    public string Name { get; set; } // the Name property with hidden backing field
}

Class accessing the property: 类访问属性:

Person person = new Person();
person.Name = "Joe";  // the set accessor is invoked here                

System.Console.Write(person.Name);  // the get accessor is invoked here

It depends on the scenario. 这取决于场景。 But ideally, Form elements are passed to any functions that will need to use them. 但理想情况下,将Form元素传递给需要使用它们的任何函数。

You have a few options: 您有几种选择:

  1. Pass the value to the class/method that's using it. 将值传递给使用它的类/方法。 This is the preferred scenario. 这是首选方案。 If your class depends on this value, supply the value to the class. 如果您的班级依赖于该值,请将该值提供给该班级。 Don't make the class go looking for it. 不要让全班去寻找它。 (See: Dependency Inversion Principle) (请参阅:依赖反转原理)
  2. Make the value static. 将值设为静态。 Then any other class can refer to that value. 然后,任何其他类都可以引用该值。 Note the difference between instance and static, of course. 当然,请注意实例和静态之间的区别。 The value will always be the same and needs to be given in the definition of the member, not in a constructor or other logic. 该值将始终相同,并且需要在成员的定义中给出,而不是在构造函数或其他逻辑中给出。
  3. Create an instance of the form (which is itself just a class) within the class and access the public member on that instance. 在类中创建表单的实例(本身只是一个类),并访问该实例上的公共成员。 This is unlikely to be what you want because the instance you're creating isn't the instance that's running "on the page." 这不太可能是您想要的,因为您创建的实例不是在“页面上”运行的实例。 (It also violates the principle noted above.) (这也违反了上述原则。)
  4. Pass a reference to the form ( this ) to the class and refer to the member from that reference. 将对表单( this )的引用传递给类,并从该引用中引用成员。

On a side note, you'll want to get in the habit of making your public members properties instead of variables. 附带说明一下,您将养成使您的公共成员属性代替变量的习惯。 In most cases, the property will likely just get/set the variable and nothing more. 在大多数情况下,该属性可能只会获取/设置变量,仅此而已。 However, if something more ever needs to be added it can be done so without breaking compatibility. 但是,如果需要添加更多内容,可以在不破坏兼容性的情况下进行。 Changing a variable to a property changes the footprint of the class and breaks things which use that class. 将变量更改为属性会更改类的占用空间,并且会破坏使用该类的内容。

Make the variable static . 将变量设为static Then you can call it like Form1.MyVariable . 然后,您可以像Form1.MyVariable那样调用它。

Try like this: 尝试这样:

In case (1) you can have MyClass.MyInt private readonly. 在情况(1)中,您可以将MyClass.MyInt私有只读。

public class MyForm : System.Windows.Forms.Form
{
    int myInt;

    public MyForm()
    {
        myInt = 1;

        //1
        var myClass = new MyClass(myInt);

        //2
        myClass.MyInt = myInt;
    }
}

public class MyClass
{
    public int MyInt { get; set; }

    public MyClass(int myInt)
    {
        MyInt = myInt;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM