Dynamic VS Var Data types

Posted: October 14, 2010 in .NET

Salam Alikom ,

I think that most of developers are using “var” and “dynamic” data types and they think that the two data types has the same function !? , maybe that are right ! but when we study each one .. we will find that each one has its own role ! HOW?

if we wrote on Visual Studio:

var x = “Weldo”;

Static Type of x is String & the Runtime Type of x will be String also ! what’s Mean ?

Means that “var” says : Let the Compiler Figure out the Type .

the compiler will assign the variable x string data type before compiling & of course the runtime type will be also string because the compiler decided the type before compiling the source code.

and if we wrote:

dynamic y = “Weldo”;

the Static Type of y will be dynamic &  the runtime type of y will be string !

so, “Dynamic” says : Let the Runtime Figure out the Type .

the compiler won’t care about variable y assignment ….

……….. if you understood the idea of “var” and “dynamic” … what are the Error types of the following two lines code ?

int n = x; // x is a var string

int w = y; // y is a dynamic string

-

-

-

-

-

-

-

1. Compile-time Error

2. Runtime Error

Good Luck :)
Salam Alikom

Static Constructors

Posted: September 27, 2010 in Tips
Tags: ,

Salam Alikom,

A static constructor executes once per type, rather than once per instance (Like Instance Constructors). A type
can define only one static constructor, and it must be parameterless and have the
same name as the type:
class Test
{
static Test() { Console.WriteLine (“Type Initialized”); }
}
The runtime automatically invokes a static constructor just prior to the type being
used. Two things trigger this:
• Instantiating the type
• Accessing a static member in the type
The only modifiers allowed by static constructors are unsafe and extern.

Note: If a static constructor throws an unhandled exception that type becomes unusable for the life of the application.

Salam Alikom

Optional and Named Paramters

Posted: September 27, 2010 in C# 4.0
Tags: , ,

Salam Alikom,

Optional Paramters:

A parameter is optional if it specifies a default value in its declaration:
void Foo (int x = 23) { Console.WriteLine (x); }
Optional parameters may be omitted when calling the method:
Foo(); // 23
The default argument of 23 is actually passed to the optional parameter x—the compiler
bakes the value 23 into the compiled code at the calling side. The preceding
call to Foo is semantically identical to:
Foo (23);
because the compiler simply substitutes the default value of an optional parameter
wherever it is used.

The default value of an optional parameter must be specified by a constant expression,
or a parameterless constructor of a value type. Optional parameters cannot be
marked with ref or out.
Mandatory parameters must occur before optional parameters in both the method
declaration and the method call (the exception is with params arguments, which still
always come last). In the following example, the explicit value of 1 is passed to x,
and the default value of 0 is passed to y:
void Foo (int x = 0, int y = 0) { Console.WriteLine (x + “, ” + y); }
void Test()
{
Foo(1); // 1, 0
}

Named Paramters:

 Rather than identifying an argument by position, you can identify an argument by
name. For example:
void Foo (int x, int y) { Console.WriteLine (x + “, ” + y); }
void Test()
{
Foo (x:1, y:2); // 1, 2
}
Named arguments can occur in any order. The following calls to Foo are semantically
identical:
Foo (x:1, y:2);
Foo (y:2, x:1);

You can mix named and positional parameters:
Foo (1, y:2);
However, there is a restriction: positional parameters must come before named arguments.
So we couldn’t call Foo like this:
Foo (x:1, 2); // Compile-time error
Named arguments are particularly useful in conjunction with optional parameters.
For instance, consider the following method:
void Bar (int a = 0, int b = 0, int c = 0, int d = 0) { … }
We can call this supplying only a value for d as follows:
Bar (d:3);

Salam Alikom

Stack VS Heap

Posted: September 26, 2010 in Tips
Tags: , ,

Salam Alikom ,

The stack and the heap are the places where variables and constants reside. Each
has very different lifetime semantics.

Stack
The stack is a block of memory for storing local variables and parameters. The stack
logically grows and shrinks as a function is entered and exited. Consider the following
method (to avoid distraction, input argument checking is ignored):
static int Factorial (int x)
{
if (x == 0) return 1;
return x * Factorial (x-1);
}
This method is recursive, meaning that it calls itself. Each time the method is entered,
a new int is allocated on the stack, and each time the method exits, the int is
deallocated.

Heap
The heap is a block of memory in which objects (i.e., reference-type instances) reside.
Whenever a new object is created, it is allocated on the heap, and a reference to that
object is returned. During a program’s execution, the heap starts filling up as new
objects are created. The runtime has a garbage collector that periodically deallocates
objects from the heap, so your computer does not run out of memory. An object is
eligible for deallocation as soon as nothing references it.
In the following example, we start by creating a StringBuilder object referenced by
the variable ref1, and then write out its content. That StringBuilder object is then
immediately eligible for garbage collection, because nothing subsequently uses it.
Then, we create another StringBuilder referenced by variable ref2, and copy that
reference to ref3. Even though ref2 is not used after that point, ref3 keeps the same
StringBuilder object alive—ensuring that it doesn’t become eligible for collection
until we’ve finished using ref3.

——————————-
using System;
using System.Text;
class Test
{
static void Main()
{
StringBuilder ref1 = new StringBuilder (“object1″);
Console.WriteLine (ref1);
// The StringBuilder referenced by ref1 is now eligible for GC.
StringBuilder ref2 = new StringBuilder (“object2″);
StringBuilder ref3 = ref2;
// The StringBuilder referenced by ref2 is NOT yet eligible for GC.
Console.WriteLine (ref3); // object2
}
}

——————————-
Value-type instances (and object references) live wherever the variable was declared.
If the instance was declared as a field within an object, or as an array element, that
instance lives on the heap.
The heap also stores static fields and constants. Unlike objects allocated on the heap
(which can get garbage-collected), these live until the application domain is torn
down.

Salam Alikom

ADO.NET VS ADO

Posted: September 22, 2010 in My Graduation Project
Tags: , , ,

Salam Alikom ,

there are differences between ADO.NET and ADO Centered in Data Representation in memory, Number of tables , data navigation & cursors , Data Connections , Data sharing between applications.

For further information you can take a look on this Link .

You can visit this page to know the benefits of ADO.NET Topic.

Salam Alikom

Salam Alikom,

Access.NET has a VBA to VB.NET Compiler to convert VBA Codes to be running on .NET Framework because VBA can’t be ran on .NET Framework, I Used this Article to convert VBA to VB.NET and you should to be aware about these issues:

Data Types , Enumerations , Arrays , Dates , Scoping , Parentheses in Procedure Calls , Parameter Passing , Assignments , TypeOf and TypeName Functions , Control Structures , Exception Handling , Late Binding and Early Binding , Multiple Variable Declarations , Variant Arrays , Windows APIs , Clipboard , UserForms , Controls Properties .

and if you want to convert your VB.NET codes to C# you can use this article .

Salam Alikom

Access.NET Tool

Posted: September 21, 2010 in My Graduation Project

Salam Alikom,

this is the time to talk about my Graduation Project , it is Access.NET Tool , Actually it’s a one tool from 2 tools … i will talk about the 1st one “Access.NET” :

What is Access.NET ?

A tool to convert MS Access Project for a given access file into .NET Solution (C#) can be running under .NET Framework and you have an ability to develop these projects using all .NET technologies ex: WPF,WCF,Compact Frawework , microsoft services and Linq ,about Access DB will be converted to SQL Server DB and about VBA codes will be converted to VB.NET codes.

Why Access.NET Tool ?

MS Access has a limitations :

1.Database capacity is very limited (max 2 GB only).
2.Limited number of connections on Access DB.
3.Can’t handle large data requests (When the Number of returned Records more than 10,000).
4. No more than 12 concurrent users can successfully use a networked access application to be in ideal state with good response time for Data Requests.
 

 

Access.NET High Level Design:

Access.NET RunTime Video:

Supported Access Controls:

1.SubForm
2.ToggleButton
3.OptionGroup
4.OptionButton
5.CommandButton
6.TextBox
7.TabControl
8.CheckBox
9.ComboBox
10.Label
11.Image

 for Further Information you can visit : Access.NET WebSite

Our Page in EED’10 website : Link

I want to mention to something .. We are Participated to the EED ’10 (Egyptian Engineering Day) in Software Engineering Competition & Access.NET From the top 6 projects From 52 Participated Projects and we are proud.

 Salam Alikom

Queries Processing

Posted: September 21, 2010 in Tips
Tags: , , , ,

Salam Alikom ,

I will Explain in this post how Query  processing Works .. so i will start with Example:

if we have a Database with 2 tables :

we will consider that you have a parser to build a tree  From Given Query.. each node in the tree will contains 3 values :

1.Node value: columns of the Query

2.Left Node: Conditions of the Query

3.Right Node: Cartesian Product between el Tables in the Query

*Cartesian Product : It is a table has All Possible Combination between tables in Query

* # Columns of Cartesian Product Table = # Tables in DB +1 .

Are You got the Idea … i think not so :) …., so i will give you an example :

if i want to process this Query:

“ Select ID,Name From Table1 Where Table1.ID=1 “

Parser will Generate a node with these values:

 1.Node Value: ID,Name

2.Left Node: Table1.ID=1

3.Right Node: Cartesian Protuct Table

What about Cartesian Product Table (CPT) ?

CPT of the above Database that contains 2 tables T1 and T2 should be :

CPT contains columns will All Tables in DB with new column Titled ”Selected” contains the value “True” or “False” of the condition of specific Query …..

 what is 1′s ,2′s,3′s and 4′s in CPT ?

it is the indexes of rows in T1 and T2 , we can conclude From CPT that T1 has 2 rows only because Column T1 in CPT has 2 values only 1 and 2 & T2 contains 4 rows.

Ex: Select T1.ID, T2.BookID from T1,T2 where T1.SiteName = T2.DownloadedFrom

i will explain why the 2nd row has a False “Selected” Value:

it’s a false Value because when we apply this condition ”T1.SiteName = T2.DownloadedFrom” on the 1st row in T1 and the 2nd row in T2 won’t fire the condition .

when we will create Table will holds the filtered data in Rows in CPT with True Value and we will call it “Result Set Table” , this table will contains 4 rows.

so the result of the query will be :

i hope that the ideas of Query Processing delivered to the reader

Salam Alikom

Salam Alikom ,

What is NFS Under Construction?
NFS is cars racing game, not only Car Racing but also Car Tuning, my game is a simple NFS game  & it’s my First developed Game; it doesn’t have all features that included in Real NFS. It has a Loading mode, Garage Room, Car
show Room, Racing Mode. At racing mode we have a collision detector that helps us to avoid collision; collision is installed in our game with sound effect, we can use Horn . and drifting is installed in our game also. At Car Show Room we can test Hydraulics (hydraulics is Suspensions Allow Car to move Up, Down and sometimes from side to side); in otherwise we can use it in racing mode.

Developed by : Waleed Al-Zoghby , 2009

Take a look to the screen shots of the Game:

Game Logo

Loading mode

Garage Mode(Car Without Any thing)

Car Ready Now

Car show Room

Collision Detector Works Good :)

Drifting when you Press “Space” button

To download NFS UnderConstruction Game From Here , Documention.

Salam Alikom

Salam Alikom

Why we use Serialization ? To persist object data .

Steps to serialize Or deserialize :

1. adding these namespaces :

using System.IO;
using System.Xml.Serialization;

2. Writing [Serializable] before any class that you want to serialize.

Ex.:

[Serializable]

public class ClassName

{

//class members
}

*your class should be public

*don’t put it inside program class
3.Create an object from StreamWriter class

* StreamReader if you will deserialize.
4.Create an object from XmlSerializer class

5.Invoking Serialize() or Deserialize() method.

6. Closing stream object.

Salam Alikom