473,809 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can anyone help me with a simple ppt presention on get,set properties and exception h

5 New Member
I just need a simple ppt on get, set and exception handling in C#. I need to understand how and when to use get, set and also how to handle exceptions in C# programing.
Apr 16 '12 #1
2 1920
RhysW
70 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. public class Person
  2.     {
  3.         //constructor taking 8 parameters,these are entered strings for the varying pieces of information 
  4.         public Person(string firstName, string secondName, string address, string town, string county, string postCode, string phoneNumber)
  5.         {
  6.             //this block assigns each of the entered strings to its associated property
  7.             FirstName = firstName;
  8.             SecondName = secondName;
  9.             Address = address;
  10.             Town = town;
  11.             County = county;
  12.             PostCode = postCode;
  13.             PhoneNumber = phoneNumber;
  14.         }
  15.         public string FirstName { get; set; }//each of these properties are both gettable and settable to the outside environment
  16.         public string SecondName { get; set; }//has to be public settable or edits could not be made
  17.         public string Address { get; set; }
  18.         public string Town { get; set; }
  19.         public string County { get; set; }
  20.         public string PostCode { get; set; }
  21.         public string PhoneNumber { get; set; }
  22.     }
  23.  
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

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3. console.writeline("This is a test");
  4. }
  5. catch (Exception e)
  6. {
  7. console.writeline("Oh no an exception was thrown!");
  8. }
  9.  
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
Expand|Select|Wrap|Line Numbers
  1. catch(Exception e)
  2. {
  3. console.writeline("General error");
  4. }
  5. catch(IndexOutOfRangeException e)
  6. {
  7. console.writeline("out of range");
  8. }
  9.  
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
Apr 16 '12 #2
gladiator69
5 New Member
thanx a lot for the help. I really appreciate it.
Apr 16 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1537
by: Tris | last post by:
Apologies if this is the wrong forum for this question. Could anyone explain why this exception code is not working? The exception is thrown, but then reported as unhandled. The try catch in Main seems to be being ignored??? The exception being reported is a generic System.Exception, nothing exotic. This is only an illustration by the way :)
6
1999
by: Ali-R | last post by:
I'm using a .Net application to execute DTS packages within Sqlserver,I need to have very customizable and powerful Exception maangement systems which loggs different issues happend in the DTS package with my own errorCode. Can somebody give me some ideas? Thanks
5
1276
by: Flip | last post by:
I'm moving from java to C# and have a lot of fun doing it. But something I've noticed I want to double check on in case I'm not learning properly is checked exceptions. Does C#/.NET have such a concept? If I create a class that throws some exceptions, how can I ensure any other classes calling my method (like my own :>) catch and either rethrow or deal with the exceptions at compile time? Thanks.
10
1667
by: junw2000 | last post by:
Hi, Below is a simple code about exception. #include <iostream> using namespace std; struct E { const char* message; E(const char* arg) : message(arg) { } };
0
2715
by: markusa | last post by:
Hello all, my .NET 1.1 application runs on many desktop pc and sometimes produces a crash which is recorded by dr. watson. Although I log all exceptions at least in the Main method and in the ThreadException method the application crashes without any exception logging. The exception number in the dr. watson log is 80000007. The error occurs at address 7ffe0304. The stack trace looks like
2
1637
by: Eric | last post by:
Simple problem - binding Visible property of label to Checked property of radiobutton on the same form. How do I do that in VS2005?? Visible="<%# radiob.Checked %>" - DOESN'T WORK It was so simple in 2003 but how it can be done in VS05 using binding expressions. Theres no controls listing under 'Expressions' on the designer properties window. Please help. Eric
6
3821
by: Beorne | last post by:
I have to use XmlSerializer to serialize a class and I've found big problems serializing properties and indexers. I assumed that if you serialize a class with public properties (or an indexers) containing a field, even private, the serialization process would serialize that field too assuming it is needed to that public property. This is not the case ... For example I want to serialize the following class:
7
345
by: anjna22 | last post by:
Can anyone describe with example : exception(s) thrown by the function that must be handled by other functions, and explain the conditions causing the exception(s) to be thrown. Thanks
10
2143
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel <System.Web.Services.WebService(Namespace:="http:// wwwpreview.#deleted#.co.uk/~ptaylor/Customer.wsdl")_
3
1870
by: NIKHILUNNIKRISHNAN | last post by:
Experts, I am getting file not found error while enabling the Renaming option of dotfuscator 4.4 Professional. Can any who has experience in this field help me out ? with advance thanks, Nikhil Unnikrishnan.
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.