tags:

views:

41

answers:

2

I have a program that shows a lot of data, and I am wondering what the best way would be to get and set the values of the labels in windows from other classes.

Update: I am basically wondering about this:

private string _name;
public string Name
{
    get
    {
        return _name;
    }
    set
    {
        _name = value;
    }
}

How would it work in this case, etc.

+2  A: 

Generally speaking, using DataBinding (versus hand-coding all the getting and setting of property values from controls) is usually your best bet. There are a million tutorials out there online, but here are a couple of good articles:

http://www.akadia.com/services/dotnet_databinding.html

http://support.microsoft.com/kb/313482

fdfrye
Databinding works fine against your own custom-classes with custom properties as well. By the way, don't forget you can always use the public string Name{ get; set; } shortcut while creating your properties if you don't have any special logic requirements...
fdfrye
How is that different than `public string Name;`?
Arlen Beiler
+2  A: 

Instead of storing the "name" in a private variable, just refer to the label:

public string Name
{
    get
    {
        return this.labelName.Text;
    }
    set
    {
        this.labelName.Text = value;
    }
}

This will give you a property that directly effects the label's Text.

Reed Copsey
Thanks, that is what I was wondering. However, data bindings looks simpler.
Arlen Beiler