Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
GeneralRe: Getter Pin
C-P-User-35-Nov-12 4:30
C-P-User-35-Nov-12 4:30 
GeneralRe: Getter Pin
Dave Kreskowiak5-Nov-12 4:43
mveDave Kreskowiak5-Nov-12 4:43 
GeneralRe: Getter Pin
J4amieC5-Nov-12 4:44
J4amieC5-Nov-12 4:44 
GeneralRe: Getter Pin
Pete O'Hanlon5-Nov-12 4:47
mvePete O'Hanlon5-Nov-12 4:47 
AnswerRe: Getter Pin
Abhinav S5-Nov-12 5:37
Abhinav S5-Nov-12 5:37 
GeneralRe: Getter Pin
C-P-User-35-Nov-12 7:18
C-P-User-35-Nov-12 7:18 
GeneralRe: Getter Pin
Pete O'Hanlon5-Nov-12 8:01
mvePete O'Hanlon5-Nov-12 8:01 
AnswerRe: Getter Pin
DaveyM695-Nov-12 8:06
professionalDaveyM695-Nov-12 8:06 
As others have said, it's simply a way of getting a value using a property:
C#
private YourType yourValue;

public YourType YourProperty
{
    get{ return yourValue; // this is the 'getter' }
}

The next obvious question is why we use a getter. In the example above we could have just made yourValue public. The disadvantage of that is then yourValue can also be changed. In our code, it is read only as there is no corresponding setter.

Including a setter rather than exposing the variable still has advantages however. For example, we could include some validation, raise an event or start some other activity in the setter:
C#
public event EventHandler YourPropertyChanged;

private YourType yourValue;

public YourType YourProperty
{
    get{ return yourValue; // this is the 'getter' }
    set
    {
        if(!yourValue.Equals(value))
        {
            yourValue = value;
            OnYourPropertyChanged(EventArgs.Empty);
        }
    }
}

protected virtual void OnYourPropertyChanged(EventArgs e)
{
    EventHandler eh = YourPropertyChanged;
    if(eh != null)
        eh(this, e);
}

Also, the getter could be static as in DateTime.Now, where it returns a value that is calculated on demand.

There are other advantages too, but that should be enough to get you going Smile | :)
Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: Getter Pin
C-P-User-35-Nov-12 8:29
C-P-User-35-Nov-12 8:29 
GeneralRe: Getter Pin
DaveyM695-Nov-12 8:41
professionalDaveyM695-Nov-12 8:41 
GeneralRe: Getter Pin
Pete O'Hanlon5-Nov-12 8:45
mvePete O'Hanlon5-Nov-12 8:45 
QuestionFORMAT ASSEMBLY Pin
Member 91410335-Nov-12 2:01
Member 91410335-Nov-12 2:01 
AnswerRe: FORMAT ASSEMBLY Pin
Eddy Vluggen5-Nov-12 2:13
professionalEddy Vluggen5-Nov-12 2:13 
GeneralRe: FORMAT ASSEMBLY Pin
Member 91410335-Nov-12 2:23
Member 91410335-Nov-12 2:23 
GeneralRe: FORMAT ASSEMBLY Pin
Eddy Vluggen5-Nov-12 2:27
professionalEddy Vluggen5-Nov-12 2:27 
GeneralRe: FORMAT ASSEMBLY Pin
Member 91410335-Nov-12 2:29
Member 91410335-Nov-12 2:29 
QuestionRe: FORMAT ASSEMBLY Pin
Eddy Vluggen5-Nov-12 2:42
professionalEddy Vluggen5-Nov-12 2:42 
AnswerRe: FORMAT ASSEMBLY Pin
Member 91410335-Nov-12 3:15
Member 91410335-Nov-12 3:15 
GeneralRe: FORMAT ASSEMBLY Pin
Dave Kreskowiak5-Nov-12 3:21
mveDave Kreskowiak5-Nov-12 3:21 
GeneralRe: FORMAT ASSEMBLY Pin
Member 91410335-Nov-12 3:35
Member 91410335-Nov-12 3:35 
GeneralRe: FORMAT ASSEMBLY Pin
Dave Kreskowiak5-Nov-12 4:48
mveDave Kreskowiak5-Nov-12 4:48 
AnswerRe: FORMAT ASSEMBLY Pin
Eddy Vluggen5-Nov-12 4:39
professionalEddy Vluggen5-Nov-12 4:39 
QuestionCreate a button in the context menu for each selected contact. Pin
Cristian Capannini5-Nov-12 0:22
Cristian Capannini5-Nov-12 0:22 
AnswerRe: Create a button in the context menu for each selected contact. Pin
Mycroft Holmes5-Nov-12 0:39
professionalMycroft Holmes5-Nov-12 0:39 
GeneralRe: Create a button in the context menu for each selected contact. Pin
Cristian Capannini5-Nov-12 2:36
Cristian Capannini5-Nov-12 2:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.