Thursday, July 26, 2012

.Net Framework - A Layman’s View (Part -1)

Good morning folks !!!

For the next 2,3 posts i would like to concentrate on .net framework. I will not provide any text book material for you to prepare for interviews but i would like to give you an insight on what happens inside your computer from framework perspective.

Shall we start ?

All set ?

Suppose i asked you create your own programming language. What will you do?

I never done this task myself but when i asked this question to myself I'm getting following things in my mind.

First of all, i need to define rules for my language. Rules like how to declare variables, how to allow conditions loops, managing procedures/functions, classes, mathematical functions and more.

Good. I've defined my language rules and created. It is fine. Like any other language, our computer understand nothing from grammar or syntax of my language. It understands only 0's and 1's. So i need to create another program which translates my language into 0's and 1's. Your technical gurus call this program as "Compiler".

So what it should do ? It should check source code against the rules i have defined. After that it should convert the program to machine understandable language.

For example one rule in my language is,

Variables should be declared as

Declare myVariable Like Integer.

My Compiler should check whether variables declared in the program are in this pattern only and then should (can) compile the program to 0's and 1's.

If you declare your variable as

Dim myVariable As Integer.

My language compiler will show an error.

But...

But this same line will happily be compiled by vb.net compiler.

Why ?

Because vb.net compiler designed to understand variable declaration in this fashion like "Dim myVariable as Integer".

Making sense ?

Means, Vb.net compiler understands vb.net language syntax only. C# language understands only C# language. Java compiler understands only Java syntax and my compiler understands mylanguage syntax dot.

Got it?

Now my compiler is ready. It understand my rules and compiles programs written in my language. After program is compiled how it runs ? How each line is executed and how variables in memory created, manipulated and results are displayed ? It is obvious that i need to write another program to perform these tasks.Again, your super intelligent gurus call this program as "runtime". Again each runtime is created for it's own language.

While executing the program it does things like type checking, memory allocation and management, debugging and interacts with OS for various low level tasks.

Yalk...so many technical terms. Forgive me and do not remember them. Only thing you should remember is, each language requires it's own program to run the applications written for that language which technically called as "runtime"

Means, my runtime program customized for my programming language and your runtime program for your programming language.

So far so good. My language has been marketed and people are using it. No issues.

You know Gautama buddha ?

He says, to live happily we should reduce our wants.

We are great people, we never follow this suggestion and suffer. So one day, one more requirement came for my language.

I was asked to create a runtime program which should be able to execute any programming language, not only my language.

How can i do that ?

I can't.

How can i customize my runtime each and every time a new programming language is invented or created ? It is more complex task than understanding women.

I thought of solution for this problem and came up with a master stroke.

Instead of me customizing my runtime engine all the time, i will do one thing. I will create another new programming language and create runtime to execute that language. You all have compilers for your programs right ? What your compiler will do ? It will translate your programs into machine understandable language like 0'1 and 1's. Please don't do that. Ask your compiler to produce output of that new language which my runtime happily understands and executes. So what ever be the programming language you are using, after you compile, your compiler should generate my new programming language not 0'1 and 1's dot.

Do you follow ?

I will give you a generic example.

You went to another planet for a meeting which is organized for different representatives of the universe. You know English. They know their own planet language.

How do you all can communicate ?

You can't !

You need a translator.

But that translator job becomes hell if he has to translate all the languages to all the people of the universe. So he proposed a solution before one month of the meeting. He created a new language called "Talk" and circulated this language to all the universe representatives. Everyone should learn this language "Talk" to attend the meeting.

How this solves the problem ?

In the meeting, he translates all the speeches in the language "Talk" which every one understands irrespective of their native language.

Microsoft followed the same plan. They created a language called MSIL (Microsoft Intermediate Language) and all the language compilers should produce MSIL rather than 0's and 1's.They created a runtime which understands MSIL. They named it as CLR.

That means .net runtime called CLR understands only MSIL. It never know which original language was used to create that MSIL. So your VB.net compiler, C# compiler, JScript compiler, Fortran compiler and many more language compilers produce MSIL only after compilation. That's why it is named as Common Language Runtime.

Understanding something ?

Understanding CLR ?

We will know more about CLR in later posts.

By the way , what is .net Framework ?

Don't imagine something like old huge main frame machine.

As far Framework is concerned i liked one definition by the author Karl Moore in his book VB.net tutorials. He says

"You will hear  a lot of hype about .net framework in the programming world. Well, ignore all that rubbish. The truth is it's essentially a windows upgrade that works for .net programs. It runs .net applications, it handles program memory and it provides a common base of functionality for the programmers to use."

Fine ? It is as simple as that.

Ok boss. I think I'm done for now. In the next post we will see what happens inside CLR when it executes any program. How it compiles your MSIL code into machines readable form and also about the inside flow.

Before saying bye for now, just a tit bit.

I've created a simple windows project and added a super intelligent form called IAMFramework.frm as shown below. What we can do with this ?


Nothing. I just wanted to show you contents of the "bin" folder of this project. What it is showing?



It is showing nothing except for few things which are created by visual studio for its own use. At this point of time what our vb.net compiler is doing ?

It is doing nothing. It is also saying “you did’t say anything to me to do something.”

Oh..sorry compiler. I need to click on “Build” then only you will have permission to do something. I forgot.
I clicked on “Build”.

Shall we see the bin folder again?




Ooook. Now I can see .exe file which is created by the VB.net compiler.

What does this .exe file consists of?

Machine readable language!!

Correct?

Wrong!!

It consists of MSIL code.

Clear?

I’m singing off now.

Saturday, July 14, 2012

Base Class, Derived Class and Headache

Good morning. 2nd post after a long time.

I’m trying to be regular but… anyway from now onwards…..

In the early days of my career I used to confuse when assigning object of one type to variable of another type. For example consider these 3 classes.

1. Grand Father 2. Father 3. Finally Me

I think by seeing the class names you must have visualized that this fellow talking about inheritance. Of course for this post I’ve taken these 3 inherited classes but I’m not going to talk about inheritance. I know you already know that theoretical stuff.



In this example, we have grandparent class, father class inherited from grandparent class and FinallyMe class inherited from the father class.

My confusion was like this.Look at the below code block.



No issues. We have defined a varable of type FinallyMe and assigned FinallyMe object to it.

Now, look at the below one



1,2 butterflies flying in the stomach.

Ofcourse, for few of you it may not look that complex. Derived class object can be referred using base class variable. What is so great about it?

If you are feeling like that, this post is not for you. If you are little discomfort or frequently forgetting which type can be referred by which type (derived with base or base with derived) then read on.

One way I remember this stuff is,

Left side variable should always be on top or equal to the right side object. Look at the below given images.



Valid case. Object variable and real object both are of same of type. Both are at the same level.



Valid case. Object variable on top level compared to the real object.



Not a valid case. Object variable on bottom level compared to the real object.

Clear on this ?

Now one more thing should be cleared. In case of second scenario where top level object variable referring to it’s derived object, what are the methods can be called ? Let me explain this scenario in detail.

Take this example again



Now confusion may come. Variable is defined of type GrandParent. It is having a method called ”GrandParentProperty”. We know we can call this method using this variable but it is assigned to an object called “FinallyMe” which is having two more methods “FatherProperty” and “MyProperty”.

Can you call these two methods also like below ?

myGrandParentObject.FatherProperty()
myGrandParentObject.MyProperty()

You cannot. Your program will not get compiled at all. Your compiler may show error like “FatherProperty” is not a member of myGrandParentObject.

Your compiler argument is this.

Variable myGrandParentObject is of type GrandParent. It is having only one method called “GrandParentProperty” How can I call “FatherProperty” or "MyProperty" ?

Your argument may be this.

I have assigned Variable myGrandParentObject to object “FinallyMe”. This object is having methods called “FatherProperty” and “MyProperty”. What is your problem in calling them then?

After some silence, your compiler may say this.

Ok. You are correct and I’m also correct. Let us come to a compromise. You just clearly tell me the original object name which actually holding the required method. Please use the word CType like below so that I can understand what I have to do.

CType(myGrandParentObject, FinallyMe).FatherProperty()
CType(myGrandParentObject, FinallyMe).MyProperty()

Now your compiler is happy and you are happy. Your program runs.

Fine. Leave all this non sense talk. I hope message is clear to you. If you want to call methods of your object from a parent variable type, just use CType. It will do the trick.

Any more questions on this concept ? Please post them in comments section.

I’m signing off here. Let us meet in another post.

Note : If any image is not clear, just click on it.