<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-5723263340527744122</atom:id><lastBuildDate>Thu, 21 Apr 2011 20:25:57 +0000</lastBuildDate><category>C# 2.0</category><category>Design</category><category>Intermediate</category><category>SQL</category><category>General</category><category>Beginner</category><category>Web</category><category>Object-Oriented</category><title>TeachUrselfOrDieAFool</title><description /><link>http://teachurselfordieafool.blogspot.com/</link><managingEditor>noreply@blogger.com (Eslam Afifi)</managingEditor><generator>Blogger</generator><openSearch:totalResults>11</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Teachurselfordieafool" /><feedburner:info uri="teachurselfordieafool" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-8860362860998126470</guid><pubDate>Thu, 29 May 2008 12:36:00 +0000</pubDate><atom:updated>2008-05-29T16:36:10.225+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Object-Oriented</category><category domain="http://www.blogger.com/atom/ns#">Intermediate</category><category domain="http://www.blogger.com/atom/ns#">Design</category><title>UML and some stuff</title><description>&lt;p&gt;Here are some useful links for UML class diagrams&lt;br /&gt;&lt;a href="http://www.objectmentor.com/resources/articles/umlClassDiagrams.pdf"&gt;http://www.objectmentor.com/resources/articles/umlClassDiagrams.pdf&lt;/a&gt; &lt;br /&gt;&lt;a href="http://www.ibm.com/developerworks/rational/library/content/RationalEdge/sep04/bell/"&gt;http://www.ibm.com/developerworks/rational/library/content/RationalEdge/sep04/bell/&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;And a book, thanks to Asmaa Magdi &lt;a href="http://acmaamagdi.blogspot.com/" target="_blank"&gt;ACMaa&lt;/a&gt; &lt;br /&gt;&lt;a title="http://portal.aauj.edu/portal_resources/downloads/uml/sybex_mastering_uml_with_rational_rose2002.pdf" href="http://portal.aauj.edu/portal_resources/downloads/uml/sybex_mastering_uml_with_rational_rose2002.pdf"&gt;http://portal.aauj.edu/portal_resources/downloads/uml/sybex_mastering_uml_with_rational_rose2002.pdf&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;The difference between Aggregation and Association&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Aggregation&lt;/b&gt; is when an object contains a collection of other object. This is done mainly via data structures. Like a class that contains an Array, a List, a Dictionary, a HashTable, ...&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Association&lt;/b&gt; is when objects/classes (sub-systems) interact together in a larger system. This can be achieved either by letting the objects have references to other objects in the system (this involves aggregation), by using static classes, ...&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Examples,&lt;/p&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;In Asp.Net, the use of static classes is obvious. You have the MembershipProvider, the RuleProvider, the ConfigurationManager, ... which provide services (functionality) to other objects/classes.&lt;/li&gt;&lt;li&gt;Suppose you're designing a strategy game where soldier must know the location of other soldiers. So you will make a list of references to other soldiers in the soldier class.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Object_composition" target="_blank"&gt;Object Composition at wikipedia&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;This file contains a sample C# code and it's simple UML class diagram (in both Word 97-2003 and Word 2007 formats)&lt;br /&gt;&lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; padding-left: 0px; border-top: #dde5e9 1px solid; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginheight="0" src="http://cid-cb7122b358a93417.skydrive.live.com/embedrowdetail.aspx/TeachUrSelfOrDieAFool%20Documents/UML%20Class%20Diagram.rar" frameborder="0" marginwidth="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-8860362860998126470?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/kruV4AOUE4c" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/kruV4AOUE4c/uml-and-some-stuff.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>3</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/05/uml-and-some-stuff.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-6101002301406736474</guid><pubDate>Wed, 21 May 2008 11:54:00 +0000</pubDate><atom:updated>2008-05-21T15:06:20.396+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Object-Oriented</category><category domain="http://www.blogger.com/atom/ns#">Intermediate</category><category domain="http://www.blogger.com/atom/ns#">C# 2.0</category><title>Constructors and Destructors</title><description>&lt;p&gt;Constructors are special functions that are executed to initialize the object's data members at creation.&lt;br /&gt;Constructors have the same name of the class and do not have a return type.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Employee&lt;/span&gt;&lt;br /&gt;{&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;() &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Constructor overloading&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;You can have more than one constructor with different parameter signature i.e. different number of parameters or different datatype arrangement.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Employee&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;() &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;(&lt;span class="Code Keyword"&gt;string&lt;/span&gt; &lt;span class="Code Identifier"&gt;employeeName&lt;/span&gt;) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;(&lt;span class="Code Keyword"&gt;string&lt;/span&gt; &lt;span class="Code Identifier"&gt;employeeName&lt;/span&gt;, &lt;span class="Code Keyword"&gt;decimal&lt;/span&gt; &lt;span class="Code Identifier"&gt;salary&lt;/span&gt;) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Codee Identifier"&gt;Employee&lt;/span&gt;(&lt;span class="Code Keyword"&gt;decimal&lt;/span&gt; &lt;span class="Code Identifier"&gt;salary&lt;/span&gt;) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;The default constructors&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;If you haven't defined ANY constructor in a non-static or non-abstract class, the compiler creates for you a public constructor with no parameter.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Structs do not accept explicit parameterless constructors although they have one by default. All data members of the struct must be explicitly assigned to value.&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Base constructors&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Person&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Person&lt;/span&gt;() &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Person&lt;/span&gt;(&lt;span class="Code Keyword"&gt;string&lt;/span&gt; &lt;span class="Code Identifier"&gt;name&lt;/span&gt;) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Employee&lt;/span&gt; : &lt;span class="Code UserTypes"&gt;Person&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;() &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Let's talk philosophy. You cannot have a glass of water before it has been a glass first. You cannot have an employee before he/she has been a person first. &lt;br /&gt;So, in order to create an object as an Employee you have to create it first as a Person. Let's see code.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;code&gt;new Employee();&lt;/code&gt; &lt;br /&gt;When this statement get executed; if you don't explicitly specify which base constructor to use, the default constructor will get executed if exist; otherwise, you will get a compiler error.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;How to explicitly specify the base class constructor?&lt;br /&gt;Simply you use the base keyword as follows.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Employee&lt;/span&gt; : &lt;span class="Code UserTypes"&gt;Person&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;() : &lt;span class="Code Keyword"&gt;base&lt;/span&gt;(&lt;span class="Code String"&gt;&amp;quot;No name&amp;quot;&lt;/span&gt;) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Comment"&gt;// you can here write employee specific initialization&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;You can also pass parameters from the current constructor to the base constructor as follows.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Employee&lt;/span&gt; : &lt;span class="Code UserTypes"&gt;Person&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;(&lt;span class="Code Keyword"&gt;string&lt;/span&gt; &lt;span class="Code Identifier"&gt;employeeName&lt;/span&gt;) : &lt;span class="Code Keyword"&gt;base&lt;/span&gt;(&lt;span class="Code Identifier"&gt;employeeName&lt;/span&gt;) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Actually, the concept of base constructors is not bound only to base class's constructors. It extends to current class's constructors.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The concept is simply you define a constructor to construct the object before it get constructed by the current constructor being executed.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;You can make a constructor call another constructor in the same class as its base constructor using the this keyword. You can then make a chain of constructors.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Emplyee&lt;/span&gt; : &lt;span class="Code UserTypes"&gt;Person&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;() : &lt;span class="Code Keyword"&gt;this&lt;/span&gt;(&lt;span class="Code Number"&gt;0&lt;/span&gt;) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;(&lt;span class="Code Keyword"&gt;decimal&lt;/span&gt; &lt;span class="Code Identifier"&gt;salary&lt;/span&gt;) : &lt;span class="Code Keyword"&gt;base&lt;/span&gt;(&lt;span class="Code String"&gt;&amp;quot;No name&amp;quot;&lt;/span&gt;) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Static constructors&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;A static constructor gets executed directly before you first create an object of the class or access any of its static members. In fact it gets executed when loading the class definition in memory.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Static constructors increase performance with the fewest code. Another technique would be creating a &amp;quot;dirty-bit&amp;quot; to indicate whether the class has ever been used or not and keeping checking it in each constructor and static method and static property (note that this technique doesn't work with static fields of course).&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Static constructors don't accept access modifiers or parameters, thus you will have only one static constructor per class/struct (no overloading). And you cannot call static constructors directly.&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;How constructors work?&lt;/h4&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;What happens when you execute something like&lt;br /&gt;&lt;code&gt;new Employee(&amp;quot;abc&amp;quot;);&lt;/code&gt;&lt;br /&gt;You use the new keyword in constructor context so the runtime knows you want to create object of a class.&lt;/li&gt;&lt;li&gt;Then the runtime gets which type you want the object to be, here we want an Employee.&lt;/li&gt;&lt;li&gt;The runtime allocates a location in memory which will hold the data and assign them to their default values.&lt;/li&gt;&lt;li&gt;The runtime gets the constructor that matches the parameters you provide.&lt;/li&gt;&lt;li&gt;The runtime executes the base constructor of the current constructor (if not specified, the default constructor is executed).&lt;/li&gt;&lt;li&gt;Then the code of the current constructor gets executed.&lt;/li&gt;&lt;li&gt;Then you get the reference to the object you've just created.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;p&gt;Keeping in mind that static constructors get executed at the first use of the class/struct.&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Constructors and access modifiers&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;With the exception of static constructors and struct's constructors, you can define constructors with any access modifier (public, protected, internal, internal protected, private), see the Access modifiers post. &lt;br /&gt;You make a protected constructor to use it as a base constructor of other constructors in derived classes. You can not make protected constructors in structs because there is no inheritance in structs. &lt;br /&gt;You can do something like that.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Employee&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;private&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;() &lt;span class="Code Comment"&gt;// a constructor to be used only by this class&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;protected&lt;/span&gt; &lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;(&lt;span class="Code Keyword"&gt;string&lt;/span&gt; &lt;span class="Code Identifier"&gt;employeeName&lt;/span&gt;) &lt;span class="Code Comment"&gt;// to be used by derived classes&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Destructors&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;They are functions that get executed when the Garbage Collector collects objects. It's called automatically by the GC so you don't have control over when they get executed.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Destructors are used as cleanup functions you use to free up resources used by the object.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Employee&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Operator"&gt;~&lt;/span&gt;&lt;span class="Code Identifier"&gt;Employee&lt;/span&gt;() &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Comment"&gt;// here, you can free up resources that were used by the instance (Streams, Brushes, ...)&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Destructors are parameterless and have no return datatype. You can have only one destructor per class.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Another way to do this is using the IDisposable's Dispose() thus you may use the using statement. This is the recommended way to free up resources.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;See destructors at the MSDN &lt;a href="http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;A simple demo of constructors and destructors &lt;br /&gt;&lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; padding-left: 0px; border-top: #dde5e9 1px solid; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginheight="0" src="http://cid-cb7122b358a93417.skydrive.live.com/embedrowdetail.aspx/TeachUrSelfOrDieAFool%20Documents/Constructors.rar" frameborder="0" marginwidth="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-6101002301406736474?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/HEipdmNuQxo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/HEipdmNuQxo/constructors-and-destructors.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>2</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/05/constructors-and-destructors.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-5345774874203463594</guid><pubDate>Sun, 11 May 2008 14:23:00 +0000</pubDate><atom:updated>2008-05-30T00:27:50.022+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Object-Oriented</category><category domain="http://www.blogger.com/atom/ns#">Intermediate</category><category domain="http://www.blogger.com/atom/ns#">C# 2.0</category><title>Access modifiers</title><description>&lt;h4&gt;.Net assemblies&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;Before we dive into access modifiers we need first to define .Net assemblies. A .Net assembly is an IL (Intermediate Language) code library, this definition includes both process assemblies (exe) and library assemblies (dll).&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Access modifiers are keywords used to specify the declared accessibility of a member or a type. The significance of access modifiers comes from their use of defining a type's interfaces (the parts of the type exposed to other types, see the Encapsulation post). A type can be a class, a struct, an interface ... . Access modifiers keywords are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;private &lt;/li&gt;&lt;li&gt;protected &lt;/li&gt;&lt;li&gt;public &lt;/li&gt;&lt;li&gt;internal &lt;/li&gt;&lt;/ul&gt;&lt;table border="1" cellspacing="0" cellpadding="2" width="372"&gt;&lt;tr&gt;&lt;td valign="top" width="189"&gt;&lt;strong&gt;C# keyword&lt;/strong&gt;&lt;/td&gt;&lt;td valign="top" width="181"&gt;&lt;strong&gt;Meaning&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="203"&gt;private&lt;/td&gt;&lt;td valign="top" width="188"&gt;accessible only to this type&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="206"&gt;protected&lt;/td&gt;&lt;td valign="top" width="190"&gt;accessible only to this type and derived types (even outside the assembly)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="206"&gt;internal&lt;/td&gt;&lt;td valign="top" width="192"&gt;accessible to all classes within the same assembly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="205"&gt;protected internal&lt;/td&gt;&lt;td valign="top" width="193"&gt;accessible only to this type and derived types within the assembly &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="205"&gt;public&lt;/td&gt;&lt;td valign="top" width="193"&gt;unrestricted&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;The private keyword&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;A member marked with the private keyword means it's only accessible to this type. Only other members of the type can access it. &lt;br /&gt;The private keyword is used to hide both the data and the methods of the inner implementation of the type.&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;The protected keyword&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;A member marked with the protected keyword means it's accessible only to this type and derived types (inside or outside the assembly). The private keyword shapes the protected interface of the type (the parts of the type that is exposed to derived types).&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;protected&lt;/span&gt; &lt;span class="Code Keyword"&gt;int&lt;/span&gt; &lt;span class="Code Identifier"&gt;x&lt;/span&gt;; &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;B&lt;/span&gt; : &lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Comment"&gt;// x is visible here because it's a protected member of the base class. x is still protected and it became a member of this class&lt;/span&gt; &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;C&lt;/span&gt; : &lt;span class="Code UserTypes"&gt;B&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Comment"&gt;// x is visible here because it's a protected member of the base class. x here is still protected&lt;/span&gt; &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;D&lt;/span&gt; &lt;span class="Code Comment"&gt;// doesn't inherit from any any class&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;span class="Code Identifier"&gt;ref_a&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes"&gt;A&lt;/span&gt;(); &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Comment"&gt;// ref_a.x is not accessible from here because D doesn't inherits from A, B or C&lt;/span&gt; &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;The internal keyword&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;A member marked with the internal keyword means it's accessible to all other types located only in the same assembly. If you reference this assembly from somewhere else, you wont be able to access internal or protected internal members because they are public or protected at the assembly level.&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;protected internal&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;A member marked with the protected internal combination means it's both protected AND internal that is it's accessible only to derived types located only in the same assembly.&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;The public keyword&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;A member marked with the public keyword means it's accessible to any other type (inside or outside the assembly). &lt;br /&gt;The public keyword is used to define the public interface of the type (the parts of the type that is exposed to any other type).&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Nested types&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;A nested class is a class that is defined within another class. You sometimes nest classes to organize them or to expose the outer class's private and protected members to the nested class. A nested class can be thought of as a member of the containing class although it's not. What I mean is you can have a private class inside another class so this nested class is only accessible to the class containing it.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;private&lt;/span&gt; &lt;span class="Code Keyword"&gt;int&lt;/span&gt; &lt;span class="Code Identifier"&gt;x;&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ... &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;B&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Comment"&gt;// we can access A's x from here. in fact, we can access all the members of A&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ... &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;You access nested classes the way you access members. you write A.B to access the class B from out side the class A if the class B was part of the public interface or the protected interface.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;You can not make the outer class inherit from any of its inner classes. You can not do something like class A : A.B&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Remarks&lt;/h4&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;The default access level of classes and structs is private and the default access level of enums and interfaces is public. &lt;/li&gt;&lt;br /&gt;  &lt;li&gt;Once you declare a member with a specific access level you cannot change its access level in derived types. &lt;/li&gt;&lt;br /&gt;  &lt;li&gt;There are some restrictions on the use of access modifiers. For example, you can not have a private class A and a public class B that inherits A. Because the base class must be at least as accessible as the child class otherwise it wouldn't make any sense. The same rule is applied with function return types and function parameters. You can find more about Restrictions on Using Accessibility Levels &lt;a href="http://msdn.microsoft.com/en-us/library/cx03xt0t(VS.71).aspx" target="_blank"&gt;here&lt;/a&gt;. &lt;/li&gt;&lt;br /&gt;  &lt;li&gt;Members of enums and interfaces can not be marked with access modifiers. While the members of a struct can be marked only with public, internal or private. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Again this &lt;a href="http://www.codeproject.com/KB/cs/modifierkeywords.aspx" target="_blank"&gt;codeproject article by Marc Clifton&lt;/a&gt;. &lt;br /&gt;And &lt;a href="http://msdn.microsoft.com/en-us/library/wxh6fsc7(VS.71).aspx" target="_blank"&gt;access modifiers at the MSDN&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;I want to thank all my teachers and everyone who support me specially my teacher Mohamed Samy for his efforts and vision during the preparation of the previous couple posts.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-5345774874203463594?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/LYdZrJJtVFY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/LYdZrJJtVFY/access-modifiers.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>4</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/05/access-modifiers.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-7222786889569115792</guid><pubDate>Wed, 23 Apr 2008 16:10:00 +0000</pubDate><atom:updated>2008-04-23T18:21:56.598+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Object-Oriented</category><category domain="http://www.blogger.com/atom/ns#">Intermediate</category><category domain="http://www.blogger.com/atom/ns#">C# 2.0</category><title>Inheritance and Polymorphism</title><description>&lt;p&gt;Inheritance means if class B inherits from class A then B has class A's attributes and behavior. That means class B inherits class A's members (fields, properties and methods). &lt;br /&gt;We call the class A the base class or the parent class and the class B the derived class or the child class.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ... &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;B&lt;/span&gt; : &lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;span class="Code Comment"&gt;// class B inherits from class A.&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ... &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;As a result of inheritance, if class B inherits from class A then class B can substitute class A. This is because the inheritance relationship is an &amp;quot;is-a&amp;quot; relationship. The class B is an A. &lt;br /&gt;But this is one-directional relation, class B is an A but class A is not a B.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;span class="Code Identifier"&gt;ref_a&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes"&gt;A&lt;/span&gt;(); &lt;br /&gt;&lt;span class="Code UserTypes"&gt;B&lt;/span&gt; &lt;span class="Code Identifier"&gt;ref_b&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes"&gt;B&lt;/span&gt;(); &lt;br /&gt;&lt;span class="Code Identifier"&gt;ref_a&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Identifier"&gt;ref_b&lt;/span&gt;; &lt;span class="Code Comment"&gt;// because B is an A so we can put a B instead of an A&lt;/span&gt; &lt;br /&gt;&lt;span class="Code Comment"&gt;//ref_b = ref_a ; // Error. This won't compile because an A is not a B (but a B is an A)&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;So, you might have a function that returns an A but it can return a B because B is an A. The same concept is applied if you have a function that takes an A as a parameter but you can pass a B instead.&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Class hierarchy&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;A class hierarchy represents the sequence of inheritance of a class. Let's take a look at the &lt;code&gt;System.Windows.Forms.Label&lt;/code&gt;'s inheritance path in the following hierarchy.&lt;/p&gt;&lt;br /&gt;&lt;blockquote class="Hierarchy"&gt;System.Object &lt;br /&gt;&amp;#160;&amp;#160; System.MarshalByRefObject &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.ComponentModel.Component &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Windows.Forms.Control &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Windows.Forms.Label &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Windows.Forms.ButtonBase &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Windows.Forms.Button &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Windows.Forms.CheckBox &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Windows.Forms.RadioButton&lt;/blockquote&gt;&lt;br /&gt;&lt;p&gt;A &lt;code&gt;Label&lt;/code&gt; is a &lt;code&gt;Control&lt;/code&gt; and a &lt;code&gt;Control&lt;/code&gt; is a &lt;code&gt;Component&lt;/code&gt;, So a &lt;code&gt;Label&lt;/code&gt; is a &lt;code&gt;Component&lt;/code&gt;. You got the idea, a &lt;code&gt;Label&lt;/code&gt; is a &lt;code&gt;MarshalByRefObject&lt;/code&gt; and is an &lt;code&gt;Object&lt;/code&gt;. All .Net classes implicitly inherits from the &lt;code&gt;System.Object&lt;/code&gt; class&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Polymorphism and Overriding&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;Polymorphism is the ability of objects of different classes to respond to method calls of the same signature, each one according to an appropriate class-specific behavior. This is often achieved by inheritance.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Overriding allows you to alter behaviors inherited from the parent of the class.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;To override a function in the child class it must be &amp;quot;overridable&amp;quot; in the base class. It must be marked with the &lt;code&gt;virtual&lt;/code&gt; keyword in the base class, in fact the equivalent Visual Basic keyword is actually &lt;code&gt;Overridable&lt;/code&gt; which is more meaningful. Of course, an overridable function can not be private so that it's exposed to the child class. We shall discuss access modifiers later.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public virtual string&lt;/span&gt; &lt;span class="Code Identifier"&gt;Foo&lt;/span&gt;() &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;return&lt;/span&gt; &lt;span class="Code String"&gt;&amp;quot;A's foo.&amp;quot;&lt;/span&gt;; &lt;br /&gt;&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ... &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;B&lt;/span&gt; : &lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;br /&gt;{&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public override string&lt;/span&gt; &lt;span class="Code Identifier"&gt;Foo&lt;/span&gt;()&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;return&lt;/span&gt; &lt;span class="Code String"&gt;&amp;quot;B's foo.&amp;quot;&lt;/span&gt;;&amp;#160; &lt;br /&gt;&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ... &lt;br /&gt;}&lt;/div&gt;&lt;/P&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;span class="Code Identifier"&gt;ref_a&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes"&gt;A&lt;/span&gt;(); &lt;br /&gt;&lt;span class="Code UserTypes"&gt;B&lt;/span&gt; &lt;span class="Code Identifier"&gt;ref_b&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes"&gt;B&lt;/span&gt;(); &lt;br /&gt;&lt;span class="Code Identifier"&gt;ref_a&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Identifier"&gt;ref_b&lt;/span&gt;; &lt;br /&gt;&lt;span class="Code UserTypes"&gt;Console&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;WriteLine&lt;/span&gt;(&lt;span class="Code Identifier"&gt;ref_a&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;Foo&lt;/span&gt;()); &lt;span class="Code Comment"&gt;// &amp;quot;B's foo.&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;To understand better the overriding behavior, we shall discuss the &lt;em&gt;this&lt;/em&gt; keyword and &lt;em&gt;base&lt;/em&gt;.&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;this vs. base&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;The &lt;em&gt;this&lt;/em&gt; keyword represents the instance of the object being instantiated from the current class. Whether it's a base class or a derived class, it doesn't matter.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The &lt;em&gt;base&lt;/em&gt; keyword represents the direct parent of the class which the object is being instantiated from.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;So, you use the &lt;em&gt;this&lt;/em&gt; keyword to explicitly reach the members of the same class, and you use the &lt;em&gt;base&lt;/em&gt; keyword to explicitly reach the members of the parent of the class.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Let's get back to the previous code snippet and modify it a little bit to see what does it all mean.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;br /&gt;{&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public virtual string&lt;/span&gt; &lt;span class="Code Identifier"&gt;Foo&lt;/span&gt;()&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;return&lt;/span&gt; &lt;span class="Code String"&gt;&amp;quot;A's foo.&amp;quot;&lt;/span&gt;; &lt;br /&gt;&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ... &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public void&lt;/span&gt; &lt;span class="Code Identifier"&gt;Poo1&lt;/span&gt;() &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Comment"&gt;// Console.WriteLine(Foo()); // the same as the next line&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code UserTypes"&gt;Console&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;WriteLine&lt;/span&gt;(&lt;span class="Code Keyword"&gt;this&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code UserTypes"&gt;Foo&lt;/span&gt;()); &lt;span class="Code Comment"&gt;// &amp;quot;A's foo.&amp;quot;&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;B&lt;/span&gt; : &lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;br /&gt;{&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public override string&lt;/span&gt; &lt;span class="Code Identifier"&gt;Foo&lt;/span&gt;()&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;return&lt;/span&gt; &lt;span class="Code String"&gt;&amp;quot;B's foo.&amp;quot;&lt;/span&gt;; &lt;br /&gt;&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ... &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Keyword"&gt;public void&lt;/span&gt; &lt;span class="Code Identifier"&gt;Poo2&lt;/span&gt;() &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code UserTypes"&gt;Console&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;WriteLine&lt;/span&gt;(&lt;span class="Code Keyword"&gt;base&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;Foo&lt;/span&gt;()); &lt;span class="Code Comment"&gt;// &amp;quot;A's foo.&amp;quot;&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Comment"&gt;// Console.WriteLine(Foo()); // the same as the next line&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code UserTypes"&gt;Console&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;WriteLine&lt;/span&gt;(&lt;span class="Code Keyword"&gt;this&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;Foo&lt;/span&gt;()); &lt;span class="Code Comment"&gt;// &amp;quot;B's foo.&amp;quot;&lt;/span&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;From the outside.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code UserTypes"&gt;A&lt;/span&gt; &lt;span class="Code Identifier"&gt;ref_a&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes"&gt;A&lt;/span&gt;(); &lt;br /&gt;&lt;span class="Code Identifier"&gt;ref_a&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;Foo&lt;/span&gt;(); &lt;span class="Code Comment"&gt;// &amp;quot;A's foo.&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span class="Code UserTypes"&gt;B&lt;/span&gt; &lt;span class="Code Identifier"&gt;ref_b&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes"&gt;B&lt;/span&gt;(); &lt;br /&gt;&lt;span class="Code Identifier"&gt;ref_b&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;Foo&lt;/span&gt;(); &lt;span class="Code Comment"&gt;// &amp;quot;B's foo.&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span class="Code Identifier"&gt;ref_a&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Identifier"&gt;ref_b&lt;/span&gt;; &lt;br /&gt;&lt;span class="Code Identifier"&gt;ref_a&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;Foo&lt;/span&gt;(); &lt;span class="Code Comment"&gt;// &amp;quot;B's foo.&amp;quot; // it goes down the class hierarchy&lt;/span&gt; &lt;br /&gt;&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Notice that at the previous line B's Foo is called instead of A's. That's because if a function is virtual and is called from a reference to the base (it's treated as the base). It goes down the class hierarchy whenever is possible. This will become clearer when discussing type casting.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a title="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming" href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming"&gt;http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-7222786889569115792?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/hFGUclQK4no" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/hFGUclQK4no/inheritance-and-polymorphism.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>2</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/04/inheritance-and-polymorphism.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-2736034243927313768</guid><pubDate>Sun, 13 Apr 2008 21:46:00 +0000</pubDate><atom:updated>2008-04-13T23:55:42.564+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Web</category><category domain="http://www.blogger.com/atom/ns#">Beginner</category><category domain="http://www.blogger.com/atom/ns#">Design</category><title>HTML and CSS</title><description>&lt;p&gt;A while ago I was adjusting the blog's CSS to colorize code (mainly C# for now) snippets and I made a post as a test. And someone asked in a comment what CSS was. So, this is a brief introduction about HTML and CSS and some useful links at the end of the post.&lt;/p&gt;&lt;h4&gt;HTML (Hyper Text Markup Language)&lt;/h4&gt;&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Html" target="_blank"&gt;HTML&lt;/a&gt; is a markup language used for writing webpages. It consists of nested tags to describe the structure of text-based information.&lt;/p&gt;&lt;p&gt;HTML syntax is pretty simple. HTML consists of tags. A tag is some text surrounded by angle brackets like the tag &amp;lt;head&amp;gt;. Each tag has an end tag like &amp;lt;/head&amp;gt;&lt;/p&gt;&lt;p&gt;Here is a Hello World HTML page. Copy-paste the code in notepad and save the file as &amp;quot;Hello.html&amp;quot; and make sure the extension is .html or .htm but not .txt&lt;/p&gt;&lt;br /&gt;&lt;div class="Code"&gt;&amp;lt;html&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;head&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;title&amp;gt;My first HTML page&amp;lt;/title&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/head&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;body&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Hello, &amp;lt;b&amp;gt;world!&amp;lt;/b&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/body&amp;gt; &lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/div&gt;&lt;br /&gt;&lt;h4&gt;CSS (Cascading Style Sheets)&lt;/h4&gt;&lt;p&gt;In the previous Hello World example. You see the &amp;lt;b&amp;gt; and &amp;lt;/b&amp;gt; surrounding &amp;quot;world!&amp;quot;. This tag mean the text is bold. Other tags like &amp;lt;font&amp;gt; , &amp;lt;i&amp;gt; ... and attributes are used to format the text in the browser. But hard coding formatting and styles into HTML is hard and not customizable.&lt;/p&gt;&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Cascading_Style_Sheets" target="_blank"&gt;CSS&lt;/a&gt; is a stylesheet language to describe the presentation of markup language documents. Instead of hard coding embedded styles or tags in HTML you can define a styles for the page and/or link to external .css files.&lt;/p&gt;&lt;p&gt;In the previous HTML code you can set the background color of the body of the document like this.&lt;/p&gt;&lt;br /&gt;&lt;div class="Code"&gt;&amp;lt;body style=&amp;quot;background-color: Red; font-size: large&amp;quot;&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Hello, &amp;lt;p&amp;gt;world!&amp;lt;/p&amp;gt; &lt;br /&gt;&amp;lt;/body&amp;gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Or, you can define write the previous page as.&lt;/p&gt;&lt;br /&gt;&lt;div class="Code"&gt;&amp;lt;html&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;head&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;title&amp;gt;My first HTML page&amp;lt;/title&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; body { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; background-color: Red; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; font-size: large &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/style&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/head&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;body&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Hello, &amp;lt;b&amp;gt;world!&amp;lt;/b&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/body&amp;gt; &lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a title="http://www.w3schools.com/html/default.asp" href="http://www.w3schools.com/html/default.asp"&gt;http://www.w3schools.com/html/default.asp&lt;/a&gt; &lt;br /&gt;&lt;a title="http://www.w3schools.com/Css/default.asp" href="http://www.w3schools.com/Css/default.asp"&gt;http://www.w3schools.com/Css/default.asp&lt;/a&gt; &lt;br /&gt;&lt;a title="http://www.codeproject.com/KB/HTML/htmlbeginner.aspx" href="http://www.codeproject.com/KB/HTML/htmlbeginner.aspx"&gt;http://www.codeproject.com/KB/HTML/htmlbeginner.aspx&lt;/a&gt; &lt;br /&gt;&lt;a title="http://www.codeproject.com/KB/HTML/cssbeginner.aspx" href="http://www.codeproject.com/KB/HTML/cssbeginner.aspx"&gt;http://www.codeproject.com/KB/HTML/cssbeginner.aspx&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-2736034243927313768?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/Gpap1SFfgnY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/Gpap1SFfgnY/while-ago-i-was-adjusting-blogs-css-to.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>2</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/04/while-ago-i-was-adjusting-blogs-css-to.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-1773752986845077670</guid><pubDate>Wed, 09 Apr 2008 20:32:00 +0000</pubDate><atom:updated>2008-04-09T22:45:34.912+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Object-Oriented</category><category domain="http://www.blogger.com/atom/ns#">Intermediate</category><category domain="http://www.blogger.com/atom/ns#">C# 2.0</category><title>Encapsulation and Information Hiding</title><description>&lt;p&gt;Encapsulation means hiding the implementation details of the class from other objects and separating the class's implementation from it's interface.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;A class's interface is the parts of the class which is exposed to other classes. It's related to but not the same as interfaces like IEnumerable. It's the way an object of the class is exposed to other objects and which members are &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;protected&lt;/code&gt; or &lt;code&gt;private&lt;/code&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;There are two kind of object's interfaces; the parts of the object that is exposed to every other object is called &amp;quot;public interface&amp;quot;, and the parts of the object which is only exposed to derived object via inheritance is called &amp;quot;protected interface&amp;quot;.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Creating a well defined interface is an essential step in a good object oriented design.&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Why encapsulate?&lt;/h4&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Hide implementation details. &lt;/li&gt;&lt;br /&gt;&lt;li&gt;Maintain the same object's interface while the inner implementation can change without forcing the clients of the object to change the way they consume it. &lt;/li&gt;&lt;br /&gt;&lt;li&gt;Encapsulation helps abstraction so you will deal with object at a high level of details, so that you use objects like a Button or data Connection as a &amp;quot;black box&amp;quot; and build more complex software with it.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;p&gt;Example; The interface of a&amp;#160; Stack class is that it has &amp;quot;Push&amp;quot;, &amp;quot;Pop&amp;quot; and &amp;quot;Peek&amp;quot; method. Other object don't care how the Stack class is implemented, all they care is its interface.&lt;br /&gt;If the stack was implemented using an ArrayList and the developer wants to change it to use a LinkedList instead, all he has to do is to change the inner implementation and leave the interface unchanged, so that all the code outside of the class will continue to work unchanged.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Another example; you may want to write a log file, you write a class to handle logging to the file. The Log class would have a method &lt;code&gt;public void Log(string message)&lt;/code&gt; that will handle writing to the log file, take care of the file structure, add date and time to the log entry ...etc.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;One more example; You would use &lt;code&gt;Enums&lt;/code&gt; to hide the underlying datatype of a type. Let's see this code snippet.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;public enum&lt;/span&gt; &lt;span class="Code UserTypes Enums"&gt;AccountType&lt;/span&gt; : &lt;span class="Code Keyword"&gt;byte&lt;/span&gt; &lt;span class="Code Comment"&gt;// define a new type called AccountType with underlying type byte. by default, the underlying type is integer.&lt;/span&gt;&lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Identifier"&gt;Administrator&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Number"&gt;0&lt;/span&gt;,&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Identifier"&gt;Limited&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Number"&gt;1&lt;/span&gt;,&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Code Identifier"&gt;Guest&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Number"&gt;2&lt;/span&gt;&lt;br /&gt;}&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;So, you now have a new data type the AccountType type which is actually a byte. The AccountType enum has 3 constants Administrator, Limited and Guest with values 0, 1 and 2 respectively.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;div class="Code"&gt;&lt;span class="Code UserTypes Enums"&gt;AccountType&lt;/span&gt; &lt;span class="Code Identifier"&gt;type&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code UserTypes Enums"&gt;AccountType&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;Administrator&lt;/span&gt;; &lt;span class="Code Comment"&gt;// type = 0 since Administrator = 0 in the enumeration&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;&lt;span class="Code Identifier"&gt;type&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; (&lt;span class="Code UserTypes Enums"&gt;AccountType&lt;/span&gt;)&lt;span class="Code Number"&gt;2&lt;/span&gt;; &lt;span class="Code Comment"&gt;// explicit type cast from int (which will be converted implicitly to byte) to AccountType&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;&lt;span class="Code Keyword"&gt;byte&lt;/span&gt; &lt;span class="Code Identifier"&gt;b&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; (&lt;span class="Code Keyword"&gt;byte&lt;/span&gt;)&lt;span class="Code Identifier"&gt;type&lt;/span&gt;; &lt;span class="Code Comment"&gt;// explicit type cast from AccountType to byte&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a title="http://en.wikipedia.org/wiki/Information_hiding" href="http://en.wikipedia.org/wiki/Information_hiding" target="_blank"&gt;http://en.wikipedia.org/wiki/Information_hiding&lt;/a&gt; &lt;br /&gt;&lt;a href="http://en.wikipedia.org/wiki/Code_Complete" target="_blank"&gt;Code Complete&lt;/a&gt; 2nd Edition by &lt;a href="http://en.wikipedia.org/wiki/Steve_McConnell"&gt;Steve McConnell&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-1773752986845077670?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/M6vTvsvvdMU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/M6vTvsvvdMU/encapsulation-and-information-hiding.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>0</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/04/encapsulation-and-information-hiding.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-2738658881564355776</guid><pubDate>Wed, 02 Apr 2008 07:31:00 +0000</pubDate><atom:updated>2008-04-05T09:29:57.523+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">General</category><title>CSS Test</title><description>&lt;p&gt;This is just a test for the blog's CSS.&lt;/p&gt;&lt;br /&gt;&lt;div class="Code"&gt;&lt;span class="Code Comment"&gt;// comment&lt;/span&gt; &lt;br /&gt;&lt;span class="Code Keyword"&gt;static class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Program&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;[&lt;span class="Code UserTypes"&gt;STAThread&lt;/span&gt;]&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&lt;span class="Code Keyword"&gt;static void&lt;/span&gt; Main()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;{&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span class="Code UserTypes"&gt;Application&lt;/span&gt;.EnableVisualStyles();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span class="Code UserTypes"&gt;Application&lt;/span&gt;.SetCompatibleTextRenderingDefault(&lt;span class="Code Keyword"&gt;false&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span class="Code UserTypes"&gt;Application&lt;/span&gt;.Run(&lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Form1&lt;/span&gt;());&lt;br /&gt;&amp;#160;&amp;#160;}&lt;br /&gt;}&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-2738658881564355776?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/FlcAxNEUXPE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/FlcAxNEUXPE/css-test.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>2</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/04/css-test.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-5174684442174313844</guid><pubDate>Fri, 21 Mar 2008 13:21:00 +0000</pubDate><atom:updated>2008-03-21T15:21:32.068+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Beginner</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><title>SQL and some remarks</title><description>&lt;p&gt;Since there is no time now to talk in details about SQL, I'm pointing out some notes and links to articles and websites that discuss SQL in a simple manner. So, if you have questions, leave a comment.    &lt;br /&gt;&lt;a title="http://www.w3schools.com/sql/default.asp" href="http://www.w3schools.com/sql/default.asp"&gt;http://www.w3schools.com/sql/default.asp&lt;/a&gt;     &lt;br /&gt;&lt;a title="http://www.codeproject.com/KB/database/sqlintenmin.aspx" href="http://www.codeproject.com/KB/database/sqlintenmin.aspx"&gt;http://www.codeproject.com/KB/database/sqlintenmin.aspx&lt;/a&gt;     &lt;br /&gt;&lt;a title="http://www.databasedesign.co.uk/sqlselectshortsummary.htm" href="http://www.databasedesign.co.uk/sqlselectshortsummary.htm"&gt;http://www.databasedesign.co.uk/sqlselectshortsummary.htm&lt;/a&gt;     &lt;br /&gt;&lt;a title="http://msdn2.microsoft.com/en-us/library/ms189463.aspx" href="http://msdn2.microsoft.com/en-us/library/ms189463.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms189463.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;SQL Constraints&lt;/p&gt;  &lt;p&gt;&lt;a title="http://www.1keydata.com/sql/sql-constraint.html" href="http://www.1keydata.com/sql/sql-constraint.html"&gt;http://www.1keydata.com/sql/sql-constraint.html&lt;/a&gt;&lt;/p&gt; Notes:   &lt;br /&gt;  &lt;ol&gt;   &lt;li&gt;&lt;code&gt;SELECT TOP n WITH TIES&lt;/code&gt; will not select all repetition, instead it will select repetitions till the first truncation. And it is only to be used with &lt;code&gt;ORDER BY&lt;/code&gt;. &lt;/li&gt;    &lt;li&gt;To get the current date use the &lt;code&gt;GetDate()&lt;/code&gt; function. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Here is a file containing some windows applications and SQL notes thanks to Abd El-Rahman. I haven't review it's content, so, here is &lt;a href="http://www.3ainshams.com/files/2/2008/2/op/explain-programm.rar" target="_blank"&gt;the file&lt;/a&gt;[&lt;a href="http://www.3ainshams.com/files/2/2008/2/op/explain-programm.rar" target="_blank"&gt;^&lt;/a&gt;] as it is.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-5174684442174313844?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/f0sXkU2D6k4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/f0sXkU2D6k4/sql-and-some-remarks.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>1</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/03/sql-and-some-remarks.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-8381614151477236083</guid><pubDate>Fri, 21 Mar 2008 11:04:00 +0000</pubDate><atom:updated>2008-04-03T12:16:32.642+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Intermediate</category><category domain="http://www.blogger.com/atom/ns#">C# 2.0</category><title>Event-Driven Programming</title><description>&lt;h4&gt;&lt;font size="2"&gt;Hey guys, how are you?&lt;br /&gt;Here we discuss windows event-driven programming. There are some stuff must to know, I know they sound hard but in fact they are very easy.&lt;/font&gt;&lt;/h4&gt;&lt;br /&gt;&lt;h4&gt;The Message Queue&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;Unlike MS-DOS-based applications, Windows-based applications are event-driven. They do not make explicit function calls (such as C run-time library calls) to obtain input. Instead, they wait for the system to pass input to them.&lt;br /&gt;The system maintains a single system message queue and one thread-specific message queue for each graphical user interface (GUI) thread.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Whenever the user moves the mouse, clicks the mouse buttons, or types on the keyboard, the device driver for the mouse or keyboard converts the input into messages and places them in the system message queue. The system removes the messages, one at a time, from the system message queue, examines them to determine the destination window, and then posts them to the message queue of the thread that created the destination window. A thread's message queue receives all mouse and keyboard messages for the windows created by the thread. The thread removes messages from its queue and directs the system to send them to the appropriate window procedure for processing.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;With the exception of the &lt;code&gt;WM_PAINT&lt;/code&gt; message, the system always posts messages at the end of a message queue. This ensures that a window receives its input messages in the proper first in, first out (FIFO) sequence.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The &lt;code&gt;WM_PAINT&lt;/code&gt; message is a message from the system to tell the application it must draw itself. It has the highest priority in the message queue to maintain responsive GUI.&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;The Message Loop&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;The main task of the &lt;code&gt;WinMain&lt;/code&gt; function is processing the message queue. It will get a message from the queue and pass the message to the appropriate function to process it then remove it from the message queue.&lt;/p&gt;&lt;br /&gt;&lt;div class="Code"&gt;// this is a generic simplified C++ windows application's WinMain body &lt;br /&gt;while(GetMessage(&amp;amp;msg)) // retrieve a message from the application message queue (by reference) &lt;br /&gt;{ &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TranslateMessage( &amp;amp;msg ); // prepare that message in the message queue to be processed &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; DispatchMessage( &amp;amp;msg ); // determine and call the function that will handle that message &lt;br /&gt;}&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;So, the &lt;code&gt;DispatchMessage&lt;/code&gt; will know which Control will process the message and call its &lt;code&gt;WndProc&lt;/code&gt; function and from there The &lt;code&gt;OnClick&lt;/code&gt;, for example, and then raise the &lt;code&gt;Click&lt;/code&gt; event. &lt;br /&gt;&lt;h4&gt;Events&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;Events are delegates that will be invoked when specific actions happen.&lt;/p&gt;&lt;br /&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;public delegate void&lt;/span&gt; &lt;span class="Code UserTypes Delegates"&gt;SampleEventDelegate&lt;/span&gt;(&lt;span class="Code Keywrod"&gt;string &lt;/span&gt;someData); &lt;span class="Code Comment"&gt;// here we define the delegate singature&lt;/span&gt;&lt;br /&gt;&lt;span class="Code Keyword"&gt;public class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;SampleEventSource&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&lt;span class="Code Keyword"&gt;public event&lt;/span&gt; &lt;span class="Code UserTypes Delegates"&gt;SampleEventDelegate&lt;/span&gt; SampleEvent; &lt;span class="Code Comment"&gt;// here we declare that there is a public event named SampleEvent of the same signature as the delegate SampleEventDelegate&lt;/span&gt;&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;&lt;p&gt;The same rules of delegates apply to events. So, when we add an event handler to that event it must be of the same signature. The following line we create an object of the &lt;code&gt;SampleEventSource&lt;/code&gt; and add an event handler to its event.&lt;/p&gt;&lt;br /&gt;&lt;div class="Code"&gt;obj.SampleEvent += &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes Delegates"&gt;SampleEventDelegate&lt;/span&gt;(myfunction); &lt;span class="Code Comment"&gt;// that we already have a function called myfunction in the class we're declaring the obj object with the following declaration.&lt;/span&gt;&lt;br /&gt;&lt;span class="Code Keyword"&gt;private void&lt;/span&gt; myfunction(&lt;span class="Code Keyword"&gt;string&lt;/span&gt; data)&lt;br /&gt;{&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span class="Code UserTypes"&gt;MessageBox&lt;/span&gt;.Show(&lt;span class="Code String"&gt;&amp;quot;Event raised with these data\n&amp;quot;&lt;/span&gt; + data);&lt;br /&gt;}&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;You deal with event the same way you deal with delegate. So, any of following lines of code will do the job.&lt;/p&gt;&lt;br /&gt;&lt;div class="Code"&gt;obj.SampleEvent += &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes Delegates"&gt;SampleEventDelegate&lt;/span&gt;(myfunction);&lt;br /&gt;obj.SampleEvent += myfunction; &lt;span class="Code Comment"&gt;// implicit conversion to delegate&lt;/span&gt;&lt;br /&gt;obj.SampleEvent += &lt;span class="Code Keywrod"&gt;delegate&lt;/span&gt;(&lt;span class="Code Keyword"&gt;string&lt;/span&gt; data) { &lt;span class="Code UserTypes"&gt;MessageBox&lt;/span&gt;.Show(&lt;span class="Code String"&gt;&amp;quot;Event raised with these data\n&amp;quot;&lt;/span&gt; + data); }; &lt;span class="Code Comment"&gt;// anonymous method&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;As you see here, there is no &lt;code&gt;EventArgs e&lt;/code&gt; or &lt;code&gt;object sender&lt;/code&gt; in the code above and yet the code is correct. The &lt;code&gt;EventArgs e&lt;/code&gt; is to supply the data you want to the event in conventional way instead of passing your data as single parameters like the code above. So, if you want to pass data along with the event, create a new class that inherits from the &lt;code&gt;EventArgs&lt;/code&gt; class and extend it with the members and functions you want.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;What about the &lt;code&gt;object sender&lt;/code&gt;? Because of the fact that a single function can handle more than one event, these events can be events of different objects, you need something to tell you which object triggered that function. This becomes handy in situations like when you're creating a calculator application, instead of having 10 functions with almost the same code to handle the &lt;code&gt;Click&lt;/code&gt; event of the 10 numeric &lt;code&gt;Buttons&lt;/code&gt;, you make only one function to handle the 10 Buttons' &lt;code&gt;Click&lt;/code&gt; and then use a &lt;code&gt;switch&lt;/code&gt; statement to control the function behavior.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;You can handle the same event with more than one function and they will get executed in the same order they were added to the invocation list of the delegate.&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Windows Controls Events&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;Windows Controls provide events that will automatically be raised by the control itself. All you have to do is assigning event handlers.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Examples, &lt;br /&gt;The &lt;code&gt;Control.Click&lt;/code&gt; event will be raised when the control gets clicked. &lt;br /&gt;&lt;br /&gt;The &lt;code&gt;Control.MouseEnter&lt;/code&gt; will be raised when the mouse enters the control's region.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Here is a simple demo of events, I hope I covered everything. &lt;br /&gt;&lt;iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; padding-left: 0px; border-top: #dde5e9 1px solid; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginheight="0" src="http://cid-cb7122b358a93417.skydrive.live.com/embedrowdetail.aspx/TeachUrSelfOrDieAFool%20Documents/Events%20Demo.rar" frameborder="0" marginwidth="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;And some useful &lt;a href="http://www.codeproject.com/"&gt;CodeProject&lt;/a&gt; articles, you may find extra information in them but it's ok, skip the advanced parts. &lt;br /&gt;&lt;a title="http://www.codeproject.com/KB/cs/event_fundamentals.aspx" href="http://www.codeproject.com/KB/cs/event_fundamentals.aspx"&gt;http://www.codeproject.com/KB/cs/event_fundamentals.aspx&lt;/a&gt; &lt;br /&gt;&lt;a title="http://www.codeproject.com/KB/cs/events.aspx" href="http://www.codeproject.com/KB/cs/events.aspx"&gt;http://www.codeproject.com/KB/cs/events.aspx&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The information provided about the Message Queue and the message loop are from the Platform SDK for Windows XP SP2.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-8381614151477236083?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/EM8sr9JCeB4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/EM8sr9JCeB4/event-driven-programming.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>1</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/03/event-driven-programming.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-6199118080263521024</guid><pubDate>Wed, 19 Mar 2008 08:33:00 +0000</pubDate><atom:updated>2008-04-03T13:05:44.610+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Beginner</category><category domain="http://www.blogger.com/atom/ns#">C# 2.0</category><title>Static and non-static</title><description>&lt;p&gt;Hi everybody, how are you doing?&lt;br /&gt;Here we discuss some theoretical stuff for the mid-term exam. Sorry guys, I know we all know those stuff, consider it a quick revision. I hope I cover every thing. If I forgot to mention anything or said anything incorrect, please correct me. I welcome your comments.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;The &lt;em&gt;this&lt;/em&gt; keyword&lt;br /&gt;&lt;/strong&gt;We can say that &lt;em&gt;this&lt;/em&gt; refers to the object of the class which will execute the code.&lt;br /&gt;The equivalent keyword of &lt;em&gt;this&lt;/em&gt; in Visual Basic.Net is &lt;em&gt;Me&lt;/em&gt; which is a very self expressing name.&lt;br /&gt;You cannot assign a value to &lt;em&gt;this&lt;/em&gt;. A line like &lt;code&gt;this = null;&lt;/code&gt; or &lt;code&gt;this = new Foo();&lt;/code&gt; causes compilation error because &lt;em&gt;this&lt;/em&gt; is read-only.&lt;br /&gt;The &lt;em&gt;this&lt;/em&gt; keyword is also used to create indexers. It is also used in C# 3.0 to create extension methods.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;The &lt;em&gt;static&lt;/em&gt; keyword&lt;br /&gt;&lt;/strong&gt;Static members (variables or methods) don’t belong to a particular object. They belong to the class.&lt;br /&gt;You don’t need to create an object to access a static member, because it belongs to the class.&lt;br /&gt;The equivalent key word of &lt;em&gt;static&lt;/em&gt; in Visual Basic.Net is &lt;em&gt;Shared&lt;/em&gt;. This is a self expressing name because static variables are shared across all instances (objects) of the class.&lt;br /&gt;The &lt;em&gt;static&lt;/em&gt; keyword can be combined with other keywords (&lt;em&gt;private&lt;/em&gt;, &lt;em&gt;public&lt;/em&gt; …) in a meaningful context.&lt;br /&gt;Static members can address only other static members of the same class but not the non-static ones.&lt;br /&gt;Non-static members can access static members of the class.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Static variables&lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;Consider the following class.&lt;br /&gt;&lt;div class="Code"&gt;&lt;span class="Code Keyword"&gt;class&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Foo&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="Code Keyword"&gt;public static int&lt;/span&gt; &lt;span class="Code Identifier"&gt;SharedVariable&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="Code Keyword"&gt;public void&lt;/span&gt; &lt;span class="Code Identifier"&gt;AddOneToTheSharedVairable&lt;/span&gt;() &lt;span class="Code Comment"&gt;// this is an instance function. can only be called by an object&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="Code Identifier"&gt;SharedVariable&lt;/span&gt; &lt;span class="Code Operator"&gt;+=&lt;/span&gt; &lt;span class="Code Number"&gt;1&lt;/span&gt;; &lt;span class="Code Comment"&gt;// static member are accessible&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Take a look at the following code snippet.&lt;br /&gt;&lt;div class="Code"&gt;&lt;span class="Code UserTypes"&gt;Foo&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;SharedVariable&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Number"&gt;5&lt;/span&gt;; &lt;span class="Code Comment"&gt;// I don’t have to created any objects of the class before I access a static member. note that the variable is also public&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Code UserTypes"&gt;Foo&lt;/span&gt; &lt;span class="Code Identifier"&gt;f1&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Foo&lt;/span&gt;();&lt;br /&gt;&lt;span class="Code Comment"&gt;// f1.SharedVariable // error. the static member doesn’t belong to the object but it belongs to the class.&lt;/span&gt;&lt;br /&gt;&lt;span class="Code UserTypes"&gt;Foo&lt;/span&gt; &lt;span class="Code Identifier"&gt;f2&lt;/span&gt; &lt;span class="Code Operator"&gt;=&lt;/span&gt; &lt;span class="Code Keyword"&gt;new&lt;/span&gt; &lt;span class="Code UserTypes"&gt;Foo&lt;/span&gt;();&lt;br /&gt;&lt;span class="Code Comment"&gt;// now we have created 2 objects of the Foo class&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Code Identifier"&gt;f1&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;AddOneToTheSharedVariable&lt;/span&gt;(); &lt;span class="Code Comment"&gt;// the static variable is changed by an object of the class&lt;/span&gt;&lt;br /&gt;&lt;span class="Code Comment"&gt;// now lets see what happened to the static variable&lt;/span&gt;&lt;br /&gt;&lt;span class="Code UserTypes"&gt;Console&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;WriteLine&lt;/span&gt;(&lt;span class="Code UserTypes"&gt;Foo&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;SharedVariable&lt;/span&gt;); &lt;span class="Code Comment"&gt;// 6&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Code Identifier"&gt;f2&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;AddOneToTheSharedVariable&lt;/span&gt;(); &lt;span class="Code Comment"&gt;// now the object f2 is also changing the static variable&lt;/span&gt;&lt;br /&gt;&lt;span class="Code UserTypes"&gt;Console&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;WriteLine&lt;/span&gt;(&lt;span class="Code UserTypes"&gt;Foo&lt;/span&gt;&lt;span class="Code Operator"&gt;.&lt;/span&gt;&lt;span class="Code Identifier"&gt;SharedVariable&lt;/span&gt;); &lt;span class="Code Comment"&gt;// 7&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Static methods and the this keywords&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;Static functions and static properties can’t address the &lt;em&gt;this&lt;/em&gt; keyword because they belong to the class not to an object. The &lt;em&gt;this&lt;/em&gt; keyword is not available in the context of static members. &lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Notes:&lt;br /&gt;&lt;/strong&gt;• When all objects should share the same value of a variable you should use static variables. Example, a war game, each soldier object should know how many soldiers are there in the war field in order to determine how will he attack they enemy. So, instead of having a non-static variable for each soldier object, use a static variable to hold the count of the soldiers, this way saves memory and processing.&lt;br /&gt;• When you want a function to be called without creating an object first make it static.&lt;br /&gt;• If you have the choice between static and non-static function go for the static function, although it depends on the situation, this saves you the time of creating objects.&lt;br /&gt;• Remember, static members cannot access non-static members.&lt;br /&gt;• The &lt;em&gt;static&lt;/em&gt; keyword cannot be used with constants because constants are static by nature.&lt;br /&gt;• A static member cannot be marked as &lt;em&gt;override&lt;/em&gt;, &lt;em&gt;virtual&lt;/em&gt;, or &lt;em&gt;abstract&lt;/em&gt;.&lt;br /&gt;• The &lt;em&gt;static&lt;/em&gt; keyword is used in operator overloading and when defining implicit and explicit type casting. You don’t create an object to perform the ‘+’ operator.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Static classes&lt;br /&gt;&lt;/strong&gt;A static class means that all its members are static and must be static. A static class cannot have non-static members.&lt;br /&gt;We all are familiar with the &lt;code&gt;System.Math&lt;/code&gt; class. This static class has static functions (Sin, Pow…)&lt;br /&gt;A static class is sealed by default i.e. cannot be inherited.&lt;br /&gt;You cannot create objects of a static class.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Notes:&lt;br /&gt;&lt;/strong&gt;• Usually, when you want to create a utility class like the &lt;code&gt;System.Math&lt;/code&gt; you would use the &lt;em&gt;static&lt;/em&gt; keyword.&lt;/p&gt;&lt;p&gt;For further information about C# keywords, see this &lt;a href="http://www.codeproject.com/KB/cs/modifierkeywords.aspx"&gt;CodeProject article by Marc Clifton&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-6199118080263521024?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/Xi1B8WkIFB0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/Xi1B8WkIFB0/static-and-non-static.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>1</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/03/static-and-non-static.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5723263340527744122.post-2539003692081810935</guid><pubDate>Wed, 19 Mar 2008 08:19:00 +0000</pubDate><atom:updated>2008-03-19T10:20:06.351+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">General</category><title>Intro</title><description>&lt;strong&gt;Acknowledgment&lt;/strong&gt;&lt;br /&gt;First of all, I would like to express my sorrow that my professor Dr. Roshdy Aamer will no longer instruct us. It’s a big loss to college, my colleagues and me personally. And I would like to thank him for his efforts teaching us and his dedication and love to his work. Whatever I say, I still can not give him the appropriate credit.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;About this blog&lt;/strong&gt;&lt;br /&gt;1- The main topic of this blog is programming. Specifically for the .Net Framework 2.0&lt;br /&gt;2- Topics like Windows Forms Applications, Database Programming will be discussed.&lt;br /&gt;3- Other topics might also be discussed.&lt;br /&gt;4- Necessary information will be provided, technical detailed information will be provided whenever is possible.&lt;br /&gt;5- Comments are essential enriching blogs. So, feel free to share knowledge with us and to ask questions.&lt;br /&gt;6- I just want to mention that the name of the blog was one of many suggestions and it got the highest rating from my colleagues.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Reasons why I write this blog&lt;/strong&gt;&lt;br /&gt;1- I realized recently that it’s essential that I blog to help my colleagues in college, specially that our professor wont instruct us any longer due to health issues.&lt;br /&gt;2- I should pass knowledge with my colleagues, the upcoming generations and other people.&lt;br /&gt;3- Blogging will force me to learn further details, extend my knowledge and keep up-to-date as I have to provide as correct information as possible.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Resources and references&lt;/strong&gt;&lt;br /&gt;1- &lt;a href="http://www.msdn.com/"&gt;MSDN&lt;/a&gt;&lt;br /&gt;2- &lt;a href="http://www.codeproject.com/"&gt;The CodeProject&lt;/a&gt;&lt;br /&gt;3- My teachers.&lt;br /&gt;4- Friends and personal experience.&lt;br /&gt;5- Variety of books, blogs, websites and other sources of information.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Apology&lt;/strong&gt;&lt;br /&gt;I would like to apologize to all my friends that this blog is in English since it would be hard to write the content provided in Arabic or to switch frequently between these two languages. I will use simple English and explain every thing in many different ways as far as I can.&lt;br /&gt;&lt;br /&gt;I just want to mention that this is the first time I blog and I will be glad to receive comments, correction of wrong information I provide and questions.&lt;br /&gt;&lt;br /&gt;I recommend you also ask questions at &lt;a href="http://www.codeproject.com/"&gt;The CodeProject&lt;/a&gt;. Just, please, read the forum rules. Ask your question in the right forum. Don’t post programming questions in the lounge or any other non-technical message board. Ask clear and specific questions; provide details as much as possible to get your question answered. Don’t mention the word urgent or ask for a full code, no one will write your homework or do a project for you. And I want to say again, before you post a question at The CodeProject, READ THE FORUM RULES.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5723263340527744122-2539003692081810935?l=teachurselfordieafool.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Teachurselfordieafool/~4/1B4CR_ziuDM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Teachurselfordieafool/~3/1B4CR_ziuDM/intro.html</link><author>noreply@blogger.com (Eslam Afifi)</author><thr:total>6</thr:total><feedburner:origLink>http://teachurselfordieafool.blogspot.com/2008/03/intro.html</feedburner:origLink></item></channel></rss>

