Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, November 24, 2011

Short Circuit Evaluation

Short circuit evaluation (also called "lazy" evaluation or McCarthy Evaluation) is a commonly used method for avoiding the execution of a second expression contained in a conditional clause.

Speaking plainly, it means that when you use an if instruction that has 2 or more clauses, if the first condition is not met (when the conditions are connected by an AND operator) or if the first condition is met (when the conditions are connected by an OR operator), the program will not evaluate the second expression.

The short circuit evaluation is available for C, C++, Java, C# and VB (and most of the other programming languages. See this for the full list). 

It is commonly used for:
  • Checking before a division that the denominator is not equal to 0
  • Checking if a pointer is allocated before doing some operation with the data that he is pointing to.
Also, short circuit evaluation helps optimizing the code since an if statement that contains 5 clauses will end very quickly if one condition is not respected (without continuing to evaluate the other clauses), so your program will run more quickly.

Division check example for C\C++, Java and C#:
int a = 0, b = 32;
if ( (a != 0) && (b / a > 10) )
   //Will not go here because a=0
else
   //Will go here without triggering an divide by zero error/exception
Division check example for VB.NET
Dim x, y As Integer
x = 0
y = 32
If ( (x <> 0) AndAlso (x / y > 10) ) Then
   'Will not go here because x=0
Else
   'Will go here without triggering an division by zero exception
End If
Pointer example for C\C++
//We shall assume that you have a structure/class with a field
//(a public field in//case of C++) data.
MyStruct *myStructPointer = NULL;
if ( (myStructPointer!=null) && (myStructPointer.data == 5) )
   //Will not go here because myStructPointer = null
else
   //Will go here without triggering a null error/exception
Pointer (Reference) example for Java and C#
//We shall assume that you have a class containing
//a public field called data.
Xobject myObject = null;
if ( (myObject!=null) && (myObject.data == 5) )
   //Will not go here because myObject = null
else
   //Will go here without triggering a null error/exception
Pointer (Reference) example for VB. NET
If ( (str <> Nothing) AndAlso (str.Equals("Bird") ) Then
   'Will not go here because x=0
Else
   'Will go here without triggering a null exception
End If
In VB.NET the short circuit OR operator is OrAlso.

In some cases you may not want to use short circuit evaluation, but rather let the program evaluate all clauses. In this case you will need to use "eager" operators like (in contrast to the "lazy" operators used in short circuit evaluation):
  • C++\C#\Java : & instead of && and | instead of ||
  • Java has also : and instead of && and or instead of ||
  • VB.NET : and instead of andAlso and or instead of orAlso 
  • C doesn't have "eager" operators and all evaluation is done short-circuit only.
"Eager" operators are mostly used when your second condition consists of a function that modifies some of your other variables (but this corresponds in most cases to a bad programming style).

Tuesday, October 18, 2011

The Conditional Operator

The conditional operator ?: allows writing more simple conditional expressions in  which you can select one of two statements according to a logical condition.

Syntax
(condition)?(condition_true_statement):(condition_false_statement)

Example:
int i=3,j;
(i==3)?(j=5):(j=3);
//j will become equal to 5
Also, the conditional operator can be used for assigning a value to a variable according to a condtion.

Syntax
variable = (condition)?(condition_true_value):(condition_false_value)

Example:
int i=3,j;
j=(i==3)?(5):(3);
//j will become equal to 5
The conditional operator is mostly used in the C language, but is accepted also in C++, C# and Java (still, some people consider it not fit for object-oriented programming).

Saturday, August 20, 2011

Creating a LINQ TO SQL Model for a SQL CE Database

Right now, Visual Studio 2010 cannot create directly a .dbml model (LINQ to SQL) from .sdf file (SQL Server CE). If you try to create a model and drag & drop a table, Visual Studio will give you an error and say that this feature isn't supported.

By default, you cannot use a dbml model for a SQL Server CE database.

Still, the model can be created by using the application SqlMetal.

The application can be found in the directory (supposing that you have Windows installed on the C: partition).

[Windows x64] C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
[Windows x86] C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin

1)Open Command Prompt (Start->Run->enter cmd in textbox).
2)Once the console (Command Prompt) is opened, write:

SqlMetal /dbml:Database.dbml Database.sdf
Database.dbml must be replaced with the path to the location where you want to generate the dbml model.
Database.sdf must be replaced with the path to the location of the database (the sdf file).

3)The generated file will contain the database map (the database model). To use the model in Visual Studio, you must attach it to your current project.
Related Posts Plugin for WordPress, Blogger...