How do I use accessors?

I am a full-time programmer and am trying to learn proper code design. I am trying to learn how to use the accessories, but I can't get it to work correctly. This is what I am trying to run:

class Classy
    {
        public void changeLine(string change)
        {
            Form1 form1 = new Form1();
            form1.Line = change;
        }
    }

public partial class Form1 : Form
    {
        string line = "";
        public string Line
        {
            get
            {
                return line;
            }
            set
            {
                line = value;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            line = "";
            string change = "cake";
            Classy classy = new Classy();
            classy.changeLine(change);
            MessageBox.Show(line);
        }

      

When I click the button it shows blank text. As far as I understand, these are the following steps:

  • changing the variable with the value "cake" is passed to the cool changeLine method.
  • The changeLine method sets Form1 to change the variable with the value "cake".
  • The MessageBox displays the linear value "cake".

Why doesn't it work?

+3


source to share


3 answers


This is because you are creating a new instance Form

using the method changeLine

. This new instance is different from the one on which the event was fired or the button was clicked.

To use the correct link Form

, you can pass the actual link as an argument:

public void changeLine(Form1 form, string line) {
    form.Line = line;
}

      



You would call this method (from a form) like this:

classy.changeLine(this, change);

      

this

- the current instance Form1

is the one you want to change. You don't want to create a new one.

+4


source


Good. You have an object Form1

, and the displayed window and controls are displayed on the screen. You click a button and it calls a method.

line = "";

      

This line

is a private field that is a helper field for a property line

. At this point, access to the field line

or property line

will be returned ""

.

string change = "cake";

      

A string local to this method.

Classy classy = new Classy();

      

Ok, we have a new type object Classy

called Classy

.

classy.changeLine(change);

      

See what this call does:

Form1 form1 = new Form1();

      

Now you have another Form1

object. If you called form1.Show()

, you will now have two windows on your screen.

form1.Line = change;

      

Sets the property line

(and therefore the field line

) of this new, different Form1

.



Now back to the calling method:

MessageBox.Show(line);

      

Shows the field value of the originalline

object . Form1

To demonstrate a property set from outside the class, you can do something like:

class Classy
{
    public void changeLine(Form1 form1, string change)
    {
        form1.Line = change;
    }
}

public partial class Form1 : Form
{
    string line = "";
    public string Line
    {
        get
        {
            return line;
        }
        set
        {
            line = value;
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        string change = "cake";
        Classy classy = new Classy();
        classy.changeLine(this, change);
        MessageBox.Show(line);
    }
}

      

Here, since it Form1

passes itself ( this

refers to the object of the running method) to Classy

now its own property will be used line

.

As an alternative:

class LineStorer
{
    private string _line;
    public string Line
    {
        get { return _line; }
        set { _line = value; }
    }
}

public partial class Form1 : Form
{
    private void button2_Click(object sender, EventArgs e)
    {
        var storer = new LineStorer();
        storer.Line = "cake";
        MessageBox.Show(storer.Line);
    }
}

      

Here, the form is first set and then gets a property in another object.

Note also that objects can use their own properties instead of the backing field:

public partial class Form1 : Form
{
    private string _line;
    public string Line
    {
        get { return _line; }
        set { _line = value; }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        Line = "cake";
        MessageBox.Show(Line);
    }
}

      

From the inside, so to speak, there really isn't much difference whether a field or a property is used, but using properties has the advantage that if on some day you change that simple property (read and write from and to the field and nothing do not) for a more complex one (common cases, including check validation for all settings, but you are far from limited to that), then you will only need to change one place where the property is defined, not possibly very many places where it is used.

(It might seem like using line

is more than just using line

, because in the end it calls out more code and therefore using properties rather than fields would be a little inefficiency that would add up over time.Fortunately, jitter is smart enough. to "inline" simple getters and setters when the code is running, so there's really no cost to them).

+1


source


What you do is what you get. You want to show a value line

, but before you show it, you never set it to a different value. Everything you do in these three lines of code has nothing to do with changing the value line

IN THIS SPECIAL INSTALLATION OF THE CLASS SHAPE.

        string change = "cake";//You declare and initialize a variable 
        Classy classy = new Classy();//You create an instance of class Classy
        classy.changeLine(change);//You invoke a method in that class that has no clue of what is going on this side

      

Yes, it sets the value line

to "cake", but to a DIFFERENT OBJECT. But you can still get the behavior you want in at least two ways:

  • You can have an instance variable of the Classy class Form Form1 and have a constructor that will receive the Form1 object as a parameter. Then, in your method, changeLine

    instead of creating a completely new Form1 object, you can change the string of that particular instance variable, for example:

    public class Classy{
      private Form1 _form1;
      public Classy(Form1 form1){
       _form1=form1;
      }
    
      public void changeLine(string change){
       _form1.Line=change;
      }
    }
    
          

  • You can change yours changeMethod

    to accept an additional parameter of type Form1 and then pass it this

    when you call it inside Form1

     public void changeLine(string change,Form1 form1){
        form1.line=change;
     }
    
          

And on the Form1 side:

 classy.changeLine("cake",this);

      

0


source







All Articles