Object Oriented Programming C# Part 2 Object Oriented Programming
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;
Accessibility
Fyi, variables inside of a class without an explicit accessibility modifier is private
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)
To override a method, use the new keyword class c1 { public void fun()… class c2 : c1 { public new void fun()… (See p2)
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.
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)
A virtual method will always be virtual ….until you used the sealed keyword public sealed override void fun() {…
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
Example: public Int32 X { get { return x; } set { x = value; } } When defining properties you can use 2 functions:
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);
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
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();
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();
Trick questions Given the following code: This is halfway
Given the following code:
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)
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)
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)
GUI (finally) Open Visual Studio File -> New Project -> Visual C# -> Windows Forms Application
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…
GUI Elements
Textbox -> Allows users to input data CommandButton -> Allow users to interact with the form Checkbox -> Usually used for turning on/off settings
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!!!!
Class Example Create a form that has 2 input boxes and adds them up. Output the result to a label
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)
Credits http://devhood.com/training_modules/dist-a/WritingOOCSharp/WritingOOCSharp.htm#_Toc520858423 http://msdn.microsoft.com/en-us/library/ms173152(VS.80).aspx http://weblogs.asp.net/dburke/archive/2004/03/26/97195.aspx