Click here to Skip to main content
15,912,329 members
Home / Discussions / C#
   

C#

 
GeneralRe: Assigning Variables in C# for ORACLE Pin
Eddy Vluggen6-Nov-12 22:45
professionalEddy Vluggen6-Nov-12 22:45 
QuestionC#, Win'7, Port Mystery Pin
C-P-User-35-Nov-12 12:49
C-P-User-35-Nov-12 12:49 
AnswerRe: C#, Win'7, Port Mystery Pin
Pete O'Hanlon5-Nov-12 22:18
mvePete O'Hanlon5-Nov-12 22:18 
GeneralRe: C#, Win'7, Port Mystery Pin
C-P-User-36-Nov-12 6:03
C-P-User-36-Nov-12 6:03 
QuestionReading XML Pin
Kevin Marois5-Nov-12 7:34
professionalKevin Marois5-Nov-12 7:34 
AnswerRe: Reading XML Pin
Eddy Vluggen5-Nov-12 8:25
professionalEddy Vluggen5-Nov-12 8:25 
GeneralRe: Reading XML Pin
Kevin Marois5-Nov-12 10:09
professionalKevin Marois5-Nov-12 10:09 
GeneralRe: Reading XML Pin
Eddy Vluggen5-Nov-12 10:20
professionalEddy Vluggen5-Nov-12 10:20 
AnswerRe: Reading XML Pin
Clifford Nelson5-Nov-12 12:23
Clifford Nelson5-Nov-12 12:23 
QuestionGenerics? Pin
David Knechtges5-Nov-12 5:50
David Knechtges5-Nov-12 5:50 
AnswerRe: Generics? Pin
Alan Balkany5-Nov-12 5:53
Alan Balkany5-Nov-12 5:53 
AnswerRe: Generics? Pin
harold aptroot5-Nov-12 5:59
harold aptroot5-Nov-12 5:59 
AnswerRe: Generics? Pin
Matt T Heffron5-Nov-12 11:29
professionalMatt T Heffron5-Nov-12 11:29 
QuestionMy DataGridView - FirstDisplayedScrollingRowIndex Pin
Paramu19735-Nov-12 5:06
Paramu19735-Nov-12 5:06 
QuestionGetter Pin
C-P-User-35-Nov-12 2:49
C-P-User-35-Nov-12 2:49 
AnswerRe: Getter Pin
J4amieC5-Nov-12 3:01
J4amieC5-Nov-12 3:01 
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 

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.