Friday, March 21, 2008

SQL and some remarks

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.
http://www.w3schools.com/sql/default.asp
http://www.codeproject.com/KB/database/sqlintenmin.aspx
http://www.databasedesign.co.uk/sqlselectshortsummary.htm
http://msdn2.microsoft.com/en-us/library/ms189463.aspx

SQL Constraints

http://www.1keydata.com/sql/sql-constraint.html

Notes:
  1. SELECT TOP n WITH TIES will not select all repetition, instead it will select repetitions till the first truncation. And it is only to be used with ORDER BY.
  2. To get the current date use the GetDate() function.

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 the file[^] as it is.

Event-Driven Programming

Hey guys, how are you?
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.


The Message Queue


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.
The system maintains a single system message queue and one thread-specific message queue for each graphical user interface (GUI) thread.


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.


With the exception of the WM_PAINT 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.


The WM_PAINT 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.


The Message Loop


The main task of the WinMain 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.


// this is a generic simplified C++ windows application's WinMain body
while(GetMessage(&msg)) // retrieve a message from the application message queue (by reference)
{
    TranslateMessage( &msg ); // prepare that message in the message queue to be processed
    DispatchMessage( &msg ); // determine and call the function that will handle that message
}

So, the DispatchMessage will know which Control will process the message and call its WndProc function and from there The OnClick, for example, and then raise the Click event.

Events


Events are delegates that will be invoked when specific actions happen.


public delegate void SampleEventDelegate(string someData); // here we define the delegate singature
public class SampleEventSource
{
   public event SampleEventDelegate SampleEvent; // here we declare that there is a public event named SampleEvent of the same signature as the delegate SampleEventDelegate
}

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 SampleEventSource and add an event handler to its event.


obj.SampleEvent += new SampleEventDelegate(myfunction); // that we already have a function called myfunction in the class we're declaring the obj object with the following declaration.
private void myfunction(string data)
{
    MessageBox.Show("Event raised with these data\n" + data);
}

You deal with event the same way you deal with delegate. So, any of following lines of code will do the job.


obj.SampleEvent += new SampleEventDelegate(myfunction);
obj.SampleEvent += myfunction; // implicit conversion to delegate
obj.SampleEvent += delegate(string data) { MessageBox.Show("Event raised with these data\n" + data); }; // anonymous method

As you see here, there is no EventArgs e or object sender in the code above and yet the code is correct. The EventArgs e 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 EventArgs class and extend it with the members and functions you want.


What about the object sender? 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 Click event of the 10 numeric Buttons, you make only one function to handle the 10 Buttons' Click and then use a switch statement to control the function behavior.


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.


Windows Controls Events


Windows Controls provide events that will automatically be raised by the control itself. All you have to do is assigning event handlers.


Examples,
The Control.Click event will be raised when the control gets clicked.

The Control.MouseEnter will be raised when the mouse enters the control's region.


Here is a simple demo of events, I hope I covered everything.


And some useful CodeProject articles, you may find extra information in them but it's ok, skip the advanced parts.
http://www.codeproject.com/KB/cs/event_fundamentals.aspx
http://www.codeproject.com/KB/cs/events.aspx


The information provided about the Message Queue and the message loop are from the Platform SDK for Windows XP SP2.

Wednesday, March 19, 2008

Static and non-static

Hi everybody, how are you doing?
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.

The this keyword
We can say that this refers to the object of the class which will execute the code.
The equivalent keyword of this in Visual Basic.Net is Me which is a very self expressing name.
You cannot assign a value to this. A line like this = null; or this = new Foo(); causes compilation error because this is read-only.
The this keyword is also used to create indexers. It is also used in C# 3.0 to create extension methods.

The static keyword
Static members (variables or methods) don’t belong to a particular object. They belong to the class.
You don’t need to create an object to access a static member, because it belongs to the class.
The equivalent key word of static in Visual Basic.Net is Shared. This is a self expressing name because static variables are shared across all instances (objects) of the class.
The static keyword can be combined with other keywords (private, public …) in a meaningful context.
Static members can address only other static members of the same class but not the non-static ones.
Non-static members can access static members of the class.

Static variables
Consider the following class.

class Foo
{
    public static int SharedVariable;

    public void AddOneToTheSharedVairable() // this is an instance function. can only be called by an object
    {
        SharedVariable += 1; // static member are accessible
    }
}

Take a look at the following code snippet.

Foo.SharedVariable = 5; // I don’t have to created any objects of the class before I access a static member. note that the variable is also public

Foo f1 = new Foo();
// f1.SharedVariable // error. the static member doesn’t belong to the object but it belongs to the class.
Foo f2 = new Foo();
// now we have created 2 objects of the Foo class

f1.AddOneToTheSharedVariable(); // the static variable is changed by an object of the class
// now lets see what happened to the static variable
Console.WriteLine(Foo.SharedVariable); // 6

f2.AddOneToTheSharedVariable(); // now the object f2 is also changing the static variable
Console.WriteLine(Foo.SharedVariable); // 7

Static methods and the this keywords
Static functions and static properties can’t address the this keyword because they belong to the class not to an object. The this keyword is not available in the context of static members.

Notes:
• 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.
• When you want a function to be called without creating an object first make it static.
• 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.
• Remember, static members cannot access non-static members.
• The static keyword cannot be used with constants because constants are static by nature.
• A static member cannot be marked as override, virtual, or abstract.
• The static keyword is used in operator overloading and when defining implicit and explicit type casting. You don’t create an object to perform the ‘+’ operator.

Static classes
A static class means that all its members are static and must be static. A static class cannot have non-static members.
We all are familiar with the System.Math class. This static class has static functions (Sin, Pow…)
A static class is sealed by default i.e. cannot be inherited.
You cannot create objects of a static class.

Notes:
• Usually, when you want to create a utility class like the System.Math you would use the static keyword.

For further information about C# keywords, see this CodeProject article by Marc Clifton.

Intro

Acknowledgment
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.

About this blog
1- The main topic of this blog is programming. Specifically for the .Net Framework 2.0
2- Topics like Windows Forms Applications, Database Programming will be discussed.
3- Other topics might also be discussed.
4- Necessary information will be provided, technical detailed information will be provided whenever is possible.
5- Comments are essential enriching blogs. So, feel free to share knowledge with us and to ask questions.
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.

Reasons why I write this blog
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.
2- I should pass knowledge with my colleagues, the upcoming generations and other people.
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.

Resources and references
1- MSDN
2- The CodeProject
3- My teachers.
4- Friends and personal experience.
5- Variety of books, blogs, websites and other sources of information.

Apology
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.

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.

I recommend you also ask questions at The CodeProject. 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.