Agenda<br /><ul><li>Classes
Objects
Adding Properties & Methods to a class
Constructors
Using Delegates
Create a Project Using Class</li></li></ul><li>          Classes<br />Classes are code definitions or blueprints for  its ...
Adding Properties to the Class<br />public string Name<br />{<br />get<br />{<br />return name;<br />}<br />set<br />{<br ...
Adding Constructor<br />A constructor is a method that automatically runs when a class object is first created. In C#, the...
Adding a Method<br />public class Product<br />{<br />// (Additional class code omitted for clarity.)<br />public string G...
      Creating Objects<br />Objects are the instance of a class. They can be created by typing a keyword new, which means ...
Upcoming SlideShare
Loading in …5
×

Chapter 3 - part1

439 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
439
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 3 - part1

  1. 1. Agenda<br /><ul><li>Classes
  2. 2. Objects
  3. 3. Adding Properties & Methods to a class
  4. 4. Constructors
  5. 5. Using Delegates
  6. 6. Create a Project Using Class</li></li></ul><li> Classes<br />Classes are code definitions or blueprints for its instances or simply objects<br />public class Product<br />{<br />private string name;<br />private decimal price;<br />private string imageUrl;<br />public string Name { get; set; }<br />public decimal Price { get; set; }<br />public string ImageUrl { get; set; }<br />}<br />
  7. 7. Adding Properties to the Class<br />public string Name<br />{<br />get<br />{<br />return name;<br />}<br />set<br />{<br />name = value;<br />}<br />}<br />public decimal Price<br />{<br />get<br />{<br />return price;<br />}<br />set<br />{<br />price = value;<br />}<br />}<br />public string ImageUrl<br />{<br />get<br />{<br />return imageUrl;<br />}<br />set<br />{<br />imageUrl = value;<br />}<br />
  8. 8. Adding Constructor<br />A constructor is a method that automatically runs when a class object is first created. In C#, the constructor always has the same name as the name of the class. Unlike a normal method, the constructor doesn’t define any return type, not even void. <br />public class Product<br />{<br />// (Additional class code omitted for clarity.)<br />public Product(string name, decimal price)<br />{<br />Name = name;<br />Price = price;<br />}<br />public Product(string name, decimal price, string imageUrl)<br />{<br />Name = name;<br />Price = price;<br />ImageUrl = imageUrl;<br />}<br />}<br />
  9. 9. Adding a Method<br />public class Product<br />{<br />// (Additional class code omitted for clarity.)<br />public string GetHtml()<br />{<br />string htmlString;<br />htmlString = "<h1>" + name + "</h1><br />";<br />htmlString += "<h3>Costs: " + Price.ToString() + "</h3><br />";<br />htmlString += "<imgsrc='" + imageUrl + "' />";<br />return htmlString;<br />}<br />}<br />
  10. 10. Creating Objects<br />Objects are the instance of a class. They can be created by typing a keyword new, which means it grabs on to a piece of memory and creates the object there.<br />Product saleProduct = new Product();<br />// Optionally you could do this in two steps:<br />// Product saleProduct;<br />// saleProduct = new Product();<br />saleProduct.Name = "Kitchen Garbage";<br />saleProduct.Price = 49.99M;<br />saleProduct.ImageUrl = "http://mysite/garbage.png";<br />
  11. 11. Testing Product Class<br /><%@ Page Language="C#" %><br /><script runat="server"><br />private void Page_Load(object sender, EventArgs e)<br />{<br />Product saleProduct = new Product("Kitchen Garbage", 49.99M, "garbage.jpg");<br />Response.Write(saleProduct.GetHtml());<br />}<br /></script><br /><html><br /><head><br /><title>Product Test</title><br /></head><br /><body></body><br /></html><br />
  12. 12. Adding a Delegate<br />// Define the delegate<br />public delegate void PriceChangedEventHandler();<br />public class Product<br />{<br />// (Additional class code omitted for clarity.)<br />// Define the event using the delegate.<br />public event PriceChangedEventHandlerPriceChanged;<br />}<br />
  13. 13. Call delegate in the Settor that records change in Price<br />public decimal Price<br />{<br />get<br />{<br />return price;<br />}<br />set<br />{<br />price = value;<br />// Fire the event, provided there is at least one listener.<br />if (PriceChanged != null)<br />{<br />PriceChanged();<br />}<br />}<br />}<br />}<br />
  14. 14. Detect Price Change<br />public void ChangeDetected()<br />{<br />// This code executes in response to the PriceChanged event.<br />}<br />Product saleProduct = new Product("Kitchen Garbage", 49.99M);<br />saleProduct.PriceChanged = ChangeDetected;<br />// Now the event will occur in response to this code:<br />saleProduct.Price = saleProduct.Price * 2;<br />

×