21 October 2002 - Current month previous updates: - 07 | 14 | 21 | |

1 - C# - Tutorial #01

Instants (#1) - computer your age, in realtime, with 1 second precision... scary!

Instants (#2) - in the next tutorial, we'll add the graphical interface, but you can already download the final version.

C# - Tutorial #01

C# (it reads "c sharp") is a new object oriented language, from Microsoft. C# was born from the .NET initiative, but while other existing languages suffered changes, so they could adapt to the .NET huge family of classes (classes are data types that join properties and methods, the former being what you would call procedures or functions under procedural languages), C# was developed from scratch, so it is probably the best tool to develop .NET applications.

In this tutorial, we'll code an application that computes the exact difference between any two instants of the Gregorian Calendar, and displays that difference with 1 second precision, in realtime. The display will be a string with the format "year(s) / month(s) / day(s) / hour(s) / minute(s) / second(s)".

The application will have a graphical interface, but today we shall focus in problem's core.

First, if we need to work with entities called "instants", we would be grateful for a corresponding data type - we will build such data type, as a class. When building classes you need to ask yourself two questions: #1) what characterizes each object (instance) of the class?, and #2) what commands should objects of the class know to obey to?

Second, once you have the high level answer to those questions, implement them, following whatever approach you feel more comfortable with: #1) do a top down observation of the problem; and #2) implement either postponing details (ie, top down all the way), or building up, from details (ie, bottom up, after understanding the problem).

My answers to the aforementioned questions, for this specific problem, are #1) a six-tuple (year, month, day, hour, minute, second), defines (characterizes) any instant; and #2) any instant should know the answers to the questions: "is my year a leap year?", "how many days has my month?", "what is my difference, measured in instants, to any other instant?".

In the next tutorial, we'll add the graphical interface, but you can already download the executable, and try to design the "looks" yourself, based on what follows, which is the core of the application.

Download the "Instants" application (.EXE) [13 KB]

Download Microsoft .NET. [various KB, depending on your options]

NOTE: in order to run the application, your computer *MUST* have, at least, a minimal .NET installation.

So, lets build the class core:

class Instant{

int year, month, day, hour, minute, second; // these are properties

// next are the constant properties, which are static (shared) by all objects

const int SECONDS_IN_ONE_MINUTE=60;

const int MINUTES_IN_ONE_HOUR=60;

const int HOURS_IN_ONE_DAY=24;

const int MONTHS_IN_ONE_YEAR=12;

// an instant object answers true or false to leap_year

bool leap_year(){

if (this.year%4==0)

if (this.year%100==0)

if (this.year%400==0) return true; // multiple of 4 and 400 is a leap

else return false; // year multiple of 4, but not of 400 is NOT a leap

else return true; // year mutiple of 4 and NOT multiple of 100 is a leap

else return false; // year NOT multiple of 4 is not a leap

} // method leap_year ends

// an instant object answers the number of days of its month, year properties

int days_in_month(){

switch (this.month){

case 1:case 3:case 5:case 7:case 8:case 10:case 12: return 31;

case 4:case 6:case 9:case 11: return 30;

case 2:if (this.leap_year()) return 29; else return 28;

}

} // method days_in_month ends

// an instant object measures its distance to a reference instant

Instant difference(Instant f){ // f is assumed to be "bigger" (older) than "this"

Instant d=new Instant(); bool subtract=false;

d.second=f.second-this.second; // difference in seconds

// if subtracting brings to a negative value, do like a watch:

// compensate, but make sure you discount one unit on the next subtraction

if (d.second<0){d.second+=SECONDS_IN_ONE_MINUTE; subtract=true;}

d.minute=f.minute-this.minute;

if (subtract) d.minute--;

if (d.minute<0){d.minute+=MINUTES_IN_ONE_HOUR; subtract=true;}

else subtract=false;

d.hour=f.hour-this.hour;

if (subtract) d.hour--;

if (d.hour<0){d.hour+=HOURS_IN_ONE_DAY; subtract=true;}

else subtract=false;

d.day=f.day-this.day;

if (subtract) d.day--;

if (d.day<0){d.day+=f.days_in_month(); subtract=true;}

else subtract=false;

d.month=f.month-this.month;

if (subtract) d.month--;

if (d.month<0){d.month+=MONTHS_IN_ONE_YEAR; subtract=true;}

else subtract=false;

d.year=f.year-this.year;

if (subtract) d.year--;

return d; // return the computed instant

} // method difference ends

} // class Instant ends

Instants (#3) - for example, just input your birth year, month, day, hour, minute and second (!) and et voila...

Instants (#4) - to put an end to the drama, just press stop.