Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming

Similar presentations


Presentation on theme: "Object Oriented Programming"— Presentation transcript:

1 Object Oriented Programming
C# Part 2 Object Oriented Programming

2 Ternary operators Ternary operators allow you to make several lines of code into 1 Example: x = (i > 5) ? 3 : 2; or if (i > 5) x = 3; else x = 2;

3 Accessibility

4 Fyi, variables inside of a class without an explicit accessibility modifier is private

5 Polymorphism This is used to inherit a class from a base class and override some methods Example: Every mammal speaks, but each mammal speaks in a different way (lets say a different word)

6 To override a method, use the new keyword
class c1 { public void fun()… class c2 : c1 { public new void fun()… (See p2)

7 Class Example Write a class with a private, public, and protected member, and attempt to access both the public and private member. Also include functions.

8 To make sure a method will always execute for a given object, use the virtual keyword and override in the new function Ie public virtual void fun() {… public override void fun() {… This will prevent the executing of the fun function from the base class (See p3)

9 A virtual method will always be virtual
….until you used the sealed keyword public sealed override void fun() {…

10 Get/set… You knew these as mutator functions
(Ok…maybe not) C# introduces the concept of a field A field is way to access private data without the implicit use of get/set methods

11 Example: public Int32 X { get { return x; } set { x = value; } } When defining properties you can use 2 functions:

12 get – returns data back to the caller
set – sets the data in the class Example: Point p1 = new Point(); p1.X = 10; Console.WriteLine(“{0}”, p1.X);

13 Class Exercise Lets create a bike shed! (BSD joke) Create 2 classes:
Shed, and bikeshed Shed should have a field for get/set the color Also should override the ToString method and output “I am a bikeshed” The bikeshed should derive from the shed

14 When using the new keyword and you do a cast to the baseclass, the functions in the old baseclass will be executed: Ie. Class2 B = new Class2(); Class1 A = (Class1) B; A.fun(); //will execute A.fun();

15 HOWEVER…. When you use the virtual and override keywords to define the functions a new set of rules come into play: Class2 B = new Class2(); Class1 A = (Class1) B; A.fun(); //will execute B.fun();

16 Trick questions Given the following code: This is halfway

17

18 Given the following code:

19

20 Constructor As you may already know from a previous TV show (namely C++ or Java)…. Constructors allow for you to create a function to fire on object creation Example: public classname(){ this.x = 5; } (See p6)

21 Callbacks Callbacks allow you to treat a method like a pointer
This is useful to allow other code to plug into your classes (See p8)

22 Events Events allow you to trigger an event upon a condition (ie a variable is changed) This is useful if you want to trigger an error or important message when a variable hits a certain point (See p7)

23 GUI (finally) Open Visual Studio
File -> New Project -> Visual C# -> Windows Forms Application

24 GUI Best Practices Labels prefix with lbl Buttons prefix with btn
Checkboxes prefix with chk Integers prefix with int Longs prefix with lng Doubles prefix with dbl Ect…

25 GUI Elements

26 Textbox -> Allows users to input data
CommandButton -> Allow users to interact with the form Checkbox -> Usually used for turning on/off settings

27 To add controls just drag and drop to the form
All controls have a name property (It is usually a good idea to change this from the default) To access control properties just use dot notation – Textbox1.text = “Some text”; //Don’t forget semicolon!!!!

28 Class Example Create a form that has 2 input boxes and adds them up.
Output the result to a label

29 Class Exercise I burn backups to CDs and DVDs. I need a program that will allow me to input 2 textboxes (1 for number of CDs and 1 for number of DVDs) and calculate how much data total I have on media (to make this easy lets stick with megabytes). CD = 700MB DVD = 4813MB (rounded)

30 Credits


Download ppt "Object Oriented Programming"

Similar presentations


Ads by Google