I’ve always wondered when looking at C# what get and set methods are used for. In basic terms I found that the get and set method was mainly for reading (get) or writing (set) a property. After scratching my head for some time, I tried to give it a go as I have problems setting a string to a particular value within one of my C# projects.
I setup another class within my project called “tgtNotes”, which would contain my get and set:
class tgtNotesJK
{
private string LocNotes = null;
public string NotesLoc
{
get
{
return LocNotes;
}
set
{
LocNotes = value;
}
}
I then thought, “surely it’s not that easy”! I then went back to the code on the form, and defined the class within the code for button1:
tgtNotesJK notestgt = new tgtNotesJK();
Getting and setting is really easy. To set a value:
notesgt.NotesLoc = "Some text";
And to get the value for example:
Console.WriteLine(notesgt.NotesLoc);
Another great benefit of this is that you will be able to access the methods from anywhere within the C# project. So because I ‘setted’ a value within button1, will mean for example that I can still access the value within other parts (for example button2).