Get is used in a property when you want it to be externally gettable, by which I mean do you want other blocks of code to be able to say 'Hey tell me what the Name property is currently set to' if get is public then the property will reply 'sure buddy, the name is currently set to Fred!' if its set to private then it wont tell you, ever, it'll cling onto its information like its life depends on it.
The set property therefore is extremely similar, do you want other classes etc. to be able to say 'hey I know you're currently set to Fred but I want to change that to peter now' if its public it will reply 'sure thing dunno what Fred was but I now store peter!' otherwise like before it'll politely refuse.
Get and Set are to be used whenever you have a constructor, these gets and sets are the blocks of code held by each property telling it what to do when something tries to change its value or access it.
A good way to understand what a constructor is is to think of it like an assembly line that is given parts of a car and assembles them into a finished car object. it takes in the chassis, stores it in a property, takes in the wheels and stores that in a property, it takes in the door, the colour the make, year etc. and stores them all in individual properties, then at the end of it you have a finished car!
Here is an example of a constructor at work with properties: (here I will demonstrate the use of a constructor with properties through the creation of a person:
-
public class Person
-
{
-
//constructor taking 8 parameters,these are entered strings for the varying pieces of information
-
public Person(string firstName, string secondName, string address, string town, string county, string postCode, string phoneNumber)
-
{
-
//this block assigns each of the entered strings to its associated property
-
FirstName = firstName;
-
SecondName = secondName;
-
Address = address;
-
Town = town;
-
County = county;
-
PostCode = postCode;
-
PhoneNumber = phoneNumber;
-
}
-
public string FirstName { get; set; }//each of these properties are both gettable and settable to the outside environment
-
public string SecondName { get; set; }//has to be public settable or edits could not be made
-
public string Address { get; set; }
-
public string Town { get; set; }
-
public string County { get; set; }
-
public string PostCode { get; set; }
-
public string PhoneNumber { get; set; }
-
}
-
here you can see that i have a class called Person, the frist thing in this class is a constructor, you give it strings, bits of information that define what that person is by their name, address, phone number etc, then it takes each of the things it has been given and puts them in properties (the bits further down the screen) it does this so they have somewhere to be safely stored so they arent lost when the constructor is finished. all of these are gettabble and settabble (you dont need to type that they are public because this is the default setting, however if you wanted to make it private you could just say private set {};
This might have you thinking that if its prvate set then surely it will never take a value? but what private set means is that once the constructor has given it a value it will keep that value forever, private set means it can never be
overridden once its set.
Thats a fairly simple briefing to the world of constructors, properties and get and sets. Exceptions are a whole new ball game
the most important piece of code for dealing with exceptions is the try,catch and finally blocks of code
if you think a piece of code might throw an exception you put it inside a try{} block, this means that the code will run as normal but if it throws an exception it will pass it to the catch {} block where you tell it what to do with it, for example
-
try
-
{
-
console.writeline("This is a test");
-
}
-
catch (Exception e)
-
{
-
console.writeline("Oh no an exception was thrown!");
-
}
-
thats a very basic piece of code that should never ever ever go wrong but if it does go wrong it will throw an exception, the catch block will grab onto it and instead of crashing the application it will just write the line saying that an exception was caught!
and last but not least is the finally {} block, ANY piece of code in the finally block will ALWAYS be executed, if an exception is caught finally will execute, if it isnt caught, finally will execute, infact finally will execute in all circumstances bar a nuclear apocalypse or its programming equivalent.
now a little bit more about the catch block, Just saying (exception e) means it will catch every single exception in existance and deal with is, but what if you want to deal with a specific exception slightly differently? what if you want to write "The list isnt that big" when the code gets an indexoutofrange exception? (an exception that is thrown when you try and do something with the 10th thing in the list when the list only has 3 things in it for example, like trying to get the 6th wheel on a car with 4 wheels)
first something you must understand is that a catch will stop once the exception falls into a slot its allowed in, so if you do
-
catch(Exception e)
-
{
-
console.writeline("General error");
-
}
-
catch(IndexOutOfRangeException e)
-
{
-
console.writeline("out of range");
-
}
-
and you get an indexoutofrange exception then the console will NEVER show out of range, this is because index out of range is a type of exception, and the first catch can catch anything, once an exception is caught and dealt with it doesnt bother trying to match up to any other catch blocks, why should it? its dealt with! so if you wanted to show a different emssage for a certain exception you would put the catch blocks the other way around, with (IndexOutOfRang e) being checked before (Exception), to be on the safe side it is always best to have catch (exception e) as the very LAST catch block, this means if the exception isnt special its dealt with in the general catch block
Sorry for the long post but i hope this explains it to you simply in a way thats easy to understand! feel free to ask questions if you want me to explain something a bit better!
RhysW