Wednesday, June 3, 2009

Incomplete Entry : Exam - Tips for Programming with Classes

Hi guys

I'm compiling some tips, or a 'cheat-sheet', that could help all of us to get great marks for the C# exam due on Thursday 11th June.

You might want to check it from time to time as I will update this entry frequently. Please do not hesitate to ask questions or suggestions, this place is also a discussion area :)

A step-by-step tutorial on how to get 70%+ for the exam!

Contents
1. Getting started - writing the class!
2. Creating the Graphical User Interface - Form Level
3. Creating your array of objects writing your methods for add, delete, update, display and search!

Checklist


1. Writing the Class
After creating a class file with the appropriate name from the Solution Explorer, immediately change the class MODIFIER. You want the MODIFIER to be PUBLIC.


public class MyProperty


1.1. Separate your class file into following sections

1.1.1. Fields
This contains all the fields or elements that are innate to the object, i.e. required to define this object.
As a matter of fact, you ALWAYS want them to be PRIVATE, so that no other objects from other classes can 'see' the fields. That's what we call encapsulation.


private string suburb;
private int bedrooms;
private int price;


1.1.2. Properties
Properties are the 'getters' and 'setters' of the fields described in 2.1. They access those fields and modify their value.
You might want to use the 're-factor' function in Visual Studio to do this quickly.

Right-click on the private field (e.g. suburb), hover on Refactor, and click on Encapsulate field. Click OK on the following message boxes to use the default properties name.


public string Suburb
{
get { return suburb; }
set { suburb = value; }
}


1.1.3. Constructors
Usually, you will require to implement at least 2 constructors, the first one being the null constructor. In the exam, the other constructor will be stated, for example, asking you to use 3 fields to pass in the constructor.
Writing the null constructor is a good practice, even if not explicitly asked during examination. Your constructors will ALWAYS be PUBLIC.


public MyProperty()
{
}

public MyProperty(string _suburb, int _bedrooms, int _price)
{
this.suburb = _suburb;
this.bedrooms = _bedrooms;
this.price = _price;
}


1.1.4. Methods
This section will implement the functions, or methods, that will be required to accomplish a certain task.
You might be ask to override the ToString() method, which means that you will have to re-define the passing parameters of the ToString(), and change its code to adapt to a situation.
Note: You ALWAYS write your methods with PUBLIC modifiers.


//override ToString() method
public override string ToString()
{
string msg = "";
msg = suburb.ToString() + ", " + bedrooms.ToString() + ", " + price.ToString() + "\n";
return msg;
}


Done with classes!

Back to the Table of Contents

2. Graphical User Interface - Form
Do as you are told in the exam, not too fancy design, but you will need those functions:
Add
Delete
Update
Display
Search

Note: Additional functions might be required, but you essentially need those.

Back to the Table of Contents

3. Creating your array of objects writing your methods for add, delete, update, display and search!
This is my favourite part!
You might want to know that we are done working with the class file, so everything that is being explained as from this section, is done in the FORM file or class.

3.1. Declaring your array
The first question would be to create an array of objects. Some students might be confused when declaring an array of objects. Just follow this rule of thumb:


ClassName[] arrayofObjects = new ClassName[10];


Of course, you might want to modify the length of the array to whatever figure required for the exam. I used 10. For those who didn't notice. :)

3.2. Add method (PUBLIC)
Return type: boolean
Passing parameters: the object to be added
Method signature: public bool addMethod(ClassName objectName)


public bool addProperty(MyProperty propObj)
{
for (int i = 0; i < propertyArray.Length; i++)
{
if (propertyArray[i] == null)
{
propertyArray[i] = propObj;
return true;
}
}
return false;
}


How it works:
Before you can add an object to your array, you need to check if there is a free space or slot for the object to be added.
You loop through ALL items in the array.
If null (meaning that this space is unoccupied), you assign this array position to the object to be added.
You need to return a boolean to tell the caller that the method has been successful.

Combining the Add method with the form:
i. Create an instance of the class, also known as the object of the class.
ii. Assign all the values that have been entered by the user to the object's properties.
iii. Call the method and store it in a boolean variable.
iv. Do the necessary validation if addition has been successful or not.



MyProperty propObj = new MyProperty(); //create an object

//assign fields to user entered values
propObj.Suburb = txtSuburb.Text;
propObj.Bedrooms = int.Parse(txtBedrooms.Text);
propObj.Price = int.Parse(txtPrice.Text);

//call add function and store result in a boolean variable
bool bool_add = addProperty(propObj);

//do the validations
if (bool_add == true)
{
MessageBox.Show("Property added!");
clearAllFields(); //clears fields
}
else
{
MessageBox.Show("No more property can be stored in the array");
}



Back to the Table of Contents


Checklist
1) Only the fields defined in your class are PRIVATE. All other entries are PUBLIC.

No comments:

Post a Comment