Presented on: “Discover Best in You “ and Windows Azure

This weekend was exciting I presented two different sessions on entirely two different topics in two different cities.

C-SharpCorner Delhi Chapter Meet: 21st September 2013

Read C# Corner Delhi Chapter Meet, 21 September, 2013: Official Recap

I tried new thing in this chapter meet and presented on a non-technical topic. I presented on “Discover Best in you”.

image

In my session I conducted two exercise for the audience and they loved that. Find slides from my presentation below,

 

I hope attendees liked this session and I am motivated to conduct more of this kind presentation.

Join C-Sharpcorner Delhi Chapter here to attend future events

 

Lovely Professional University, Jalandhar- 22nd September 2013

Yes first time I visited Punjab and I must say I had great time presenting there. There was an event centred on Windows Azure and I had an opportunity to introduce Windows Azure and Cloud Computing to young students. It was introductory kind of session and I enjoyed it most. I always look forward to talk to students.

All together it was exciting week for me. Looking forward for next month community engagement.

Variable Scope Chain in JavaScript

Before you start reading this post I recommend you to read Variable Scoping and Hoisting in JavaScript . In this post we will take a look on Scope Chain in JavaScript.

Let us consider you have declare a variable globally as below,


var var1 = "I am Global";
 var2 = "I am also global";
 this.var3= "I am also global"

In above code variables are declared globally, in other words they are not declared inside any function. There are few important points about above variable declaration,

  • Whenever you declare a variable globally, you create property of global object. So we can say var1 is property of global object.
  • Since var1 is property of global object so it is not configurable. It cannot be deleted.
  • We are creating var2 without var keyword. It is possible to declare variable without var, if we are not in strict mode.
  • In JavaScript global object can be accessed via this keyword.

Any property of global object cannot be deleted. Others can be deleted. So let us consider below code,


var var1 = "I am Global";
 var2 = "I am also global";
 this.var3= "I am also global"
 console.log(delete var1);
 console.log(delete var2);
 console.log(delete var3);

As we disused earlier var1 is not configurable so it cannot be deleted whereas other variables can be deleted. You will get output as below,

image

Above concept was very important in understating Scope Chain in JavaScript. JavaScript variables has scope

  • In function they are defined
  • If defined globally then throughout the program.

Let us consider following code snippet,

var var1 = "hello";
 function funct1() {

var var2 = "hello function";

 function funct2() {

var var3 = "hello nested function";
 }
 }

In JavaScript each function or chunk of code (for example if-else) has a scope chain. Scope chain can be seen as list of objects or objects connected to each other as chain and any variable defined inside that has a scope in that particular chain.

image

Objects in above code snippet can be viewed as following. Consider variable var2. JavaScript will first search var2 as property of funct2. If it does not find that then it will search var2 as property of object of funct2 and so on it keep searching unless it reach global object. If it does not find variable in global object then JavaScript throw ReferenceError.

Concept of Scope Chain is very useful in understating closure in JavaScript. I hope you find this post useful. Thanks for reading.

Get it right: null and undefined in JavaScript

Well, whenever I talk to developers, I find them in confusion about null and undefined in JavaScript. In this post I try to simplify understanding of null and undefined in JavaScript.

To start with null is language keyword and undefined in value of a variable. Both evaluates to value that indicates absence of a value.

To understand it better we can say null indicates an object with no value. Let us consider following code on null,


var result = typeof (null);
 console.log(result);

This will return you output as object. So to conclude we can say null is an object which contains no value or represents no object.

On the other hand undefined represents absence of value in a variable. Undefined is value of a variable. You get undefined value for a variable in following scenario

  • When variable is declared but not initialized
  • When function does not return a value
  • As value of function parameter which is not being passed
  • On accessing Array with index which does not exist
  • When accessing object property which does not exist

Let us consider following code on undefined,


var result = typeof (undefined);
 console.log(result);

This will return you output as undefined.

Other important difference is you cannot assigned undefined to a variable, whereas you can assign null to a variable. Undefined is read only.

Let us consider following code. It indicates that null and undefined represents same value that is absence of value but both are not same.


if (null == undefined) {
 console.log("both indicates absense of value");
 }

if (null === undefined) {
 console.log("both are same");
 }
 else {

console.log("both are not same");
 }

In last I will conclude that both undefined and null are falsy value in JavaScript. Both does not contain any property or methods.

I hope now you have better understanding on undefined and null in JavaScript. Thanks for reading.

Capturing Photos in Cordova or PhoneGap

In this post we will take a look on capturing photos using PhoneGap or CorDova. To start with make sure to add reference of coredova .


<script src="cordova.js"></script>

I have put one button and an image control. On click of button camera will be launched and then we will bind (display) captured photo in image control.


<button onclick="takePhoto();">Take Photo</button> <br>
<img style="display:none;width:60px;height:60px;" id="img" src="" />

takePhoto() function will be called on click event of the button . You can capture photo as following

function takePhoto() {

navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
 quality: 50,
 destinationType: destinationType.DATA_URL
 }
 );

We are using navigation.camera.getPicture function to capture photo. There are many optional parameters can be passed to this function. Those parameters are as follows,

Quality of image: quality

This is a number in range of 0 to 100 to define quality of image

Destination type: destinationType

This chooses the format of the return value. It could return image as base64 encoded string or return image file URI.

image

Source Type: sourceType

It sets the source of the picture. Source type could be PhotoLibrary , Camera , SavedPhotoAlbum. You can choose either of them.

image

Other options are as follows,

  • targetWidth a number value
  • targetHeight a number value
  • EncodingType. This could be jpeg(0) or png(1)
  • allowEdit : Boolean value to allow edit before image selection

In above scenario we have set parameters like quality and destination type. After successful selection of image onPhotoDataSuccess function will be called. In this function we will bind selected image to image control.


function onPhotoDataSuccess(imageData) {
 var imgControl = document.getElementById('img');
 imgControl.style.display = 'block';
 imgControl.src = "data:image/jpeg;base64," + imageData;
 }

If there is error selecting photo then onFail() function will be called. We can simply display an error message in this function.


function onFail(message) {
 alert('Failed because: ' + message);
 }

In this way you can work with pictures in CorDova. I hope you find this post useful. Thanks for reading.

Single Responsibility Principle in C-Sharp: Simplified

Let us start with understanding what is SRP?

Single Responsibility Principle says every class should have single responsibility and all the responsibility should be encapsulated in the class. To explain it further assume you are creating a creating class to Calculate Salary.

Now if you have two methods in class like below. Usually we can think of these two methods in a Salary class.

  1. CalculateSalaray
  2. PrintSalary

So you can say there are two responsibilities to class. With two responsibilities Salary class can be defined as below,

 class Salary

{
 public double wageperday { get; set; }
 public int attendence { get; set; }
 public double CalculateSalaray()
 {
 return this.wageperday * this.attendence;

}

public void PrintSalaray()
 {
 Console.WriteLine(this.wageperday * this.attendence);

}

}


As of Martin SRP means, “Class should have a single reason to change”. Reason to change can be seen as a Responsibility. On seeing closely you will find above class has two reasons to change,

  1. When formula for calculate salary is changing
  2. When print logic is changing

Since Salary class has two responsibility so there are two reasons to change it. We can say Salary class is not following Single Repository Principle.

We can summarize that

image

Next we need to find that how could we divide two responsibility in two separate class. So above Salary class can be divided into two separate classes. One class to CalculateSalary and another to PrintSalary.

Below is the class CalculateSalary. This class got only one responsibility to calculate salary.

class CalculateSalary
 {
 public double wageperday { get; set; }
 public int attendence { get; set; }
 public double CalculateSalaray()
 {
 return this.wageperday * this.attendence;

}
 }

&nbsp;

Another class is PrintSalary. This class also got only one responsibility to print salary.

 class PrintSalary
 {

public void PrintSalaray(double salary)
 {
 Console.WriteLine(salary);

}
 }

Let us understand what we done now is divided Salary class in two separate classes. Salary class had two responsibilities and two reasons to change. Now we have created two classes respectively for CalculateSalary and PrintSalary.

image

Now at client these two classes can be used as below,

static void Main(string[] args)
 {
 CalculateSalary clcObject = new CalculateSalary();
 clcObject.wageperday = 300;
 clcObject.attendence = 20;
 var salary = clcObject.CalculateSalaray();
 PrintSalary prntObject = new PrintSalary();
 prntObject.PrintSalaray(salary);
 Console.ReadKey(true);

}

As of now you should have idea about Single Responsibility Principle. To summarize Single Responsibility Principle says a class should have single responsibility and only one reason to change. I hope you find this post useful. Thanks for reading.