Intro Week 10
Constructors
A classes constructor is the method that is run when an instance of the class is created. If you do not provide a constructor the compiler provides one for you. The constructor is used to initiate all member properties. Unlike intrinsic types variables that are not initialized will be set to 0 or "" or null by the compiler in the default constructor.
The constructor allows you to set default values for the properties of the class. If you do not provide a constructor then a default constructor will be provided. The default constructor will initialize all of the properties in the class to empty/zero/null values.
constructor example
<syntaxhighlight lang="csharp" line="1" >public class Dog { public Dog() { //constructor code } }</csharp>
Simple Dog class with constructor
<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition public class Dog {
public string Name; // the dog's name
public int Age; // the dog's age public int Weight; // the dog's weight public string BarkSound; // the sound of the dog's bark
public Dog() { BarkSound = "Woof!!!"; }
public void Bark() { //put bark code here } public void Eat() { //put eat code here } }</csharp>
Console Example Dog.cs
OverLoading Constructors
Overloading a constructor allows object creation to happen in different ways with different parameters. The class can be overloaded to accept different parameters but each constructor method must have a unique parameter signature. Dog class with overloaded constructor
<syntaxhighlight lang="csharp" line="1" >//Dog overloaded class definition public class Dog {
public string Name; // the dog's name
public int Age; // the dog's age public int Weight; // the dog's weight public string BarkSound; // the sound of the dog's bark
public Dog() { BarkSound = "Woof!!!"; } public Dog(string newName) { Name = newName; BarkSound = "Woof!!!"; } public Dog(string newName, string newBarkSound ) { Name = newName; BarkSound = newBarkSound; }
public void Bark() { //put bark code here } public void Eat() { //put eat code here }
public string About() { string strAbout = ""; strAbout += this.Name; strAbout += " " + this.Age; strAbout += " " + this.Weight; strAbout += " " + this.BarkSound; return strAbout; }
}
}</csharp>
Console Example dogOverload.cs
Art as a windows app
Art history flash cards
Intro To Programming Art Flash Cards
As a windows app
http://iam.colum.edu/introtoprogramming/jeff/sp10/WindowsFormsApplicationArts.zip
Homework
Make the Art as a windows app into a testing app.