Rob
Rob

Reputation: 109

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?

Thanks!

Upvotes: 1

Views: 34589

Answers (5)

WernerCD
WernerCD

Reputation: 2157

MSDN: Properties

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

Upvotes: 3

David
David

Reputation: 219037

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.

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.

Upvotes: 1

Vasile Tomoiaga
Vasile Tomoiaga

Reputation: 1747

Try like this:

In case (1) you can have MyClass.MyInt private readonly.

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;
    }
}

Upvotes: 0

Neil Knight
Neil Knight

Reputation: 48587

Make the variable static. Then you can call it like Form1.MyVariable.

Upvotes: 0

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22114

It depends on the scenario. But ideally, Form elements are passed to any functions that will need to use them.

Upvotes: 1

Related Questions