Nexus S Spotted in Germany Running Android 2.4 | Android Phone Fans
Thursday, January 20, 2011
Friday, January 14, 2011
C# Lambda Expression
Lambda Expressions have been part of .NET since version 3.5 (C# 3.0); so, I decided to add a quick tutorial to the blog for programmers new to C# or C# 3.0+.
Quick Definition
“A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.”
From MSDN Site
In sum, a lambda expression is a way we can implement an anonymous method in less code. Besides the syntactic benefits added by Lambda expression; they also bring elements of functional programming into C# (a traditional object oriented language).
To the point
A lambda expressions must being with a parameter or set of parameters followed by the lambda symbol (=>) then the expression to be executed.
Syntax Form:
Parameter(s) => Expressions
Example 1:
X => X * 2
The example shows a function where X is the parameter following by the return expression of (X * 2).
Example 2:
Let’s declare a delegate.
public delegate int DoubleIt(int v);
Back in C# 2.0 we would have to use an anonymous method (see example below):
DoubleIt d = new DoubleIt(delegate(int x) {
return x * 2;
});
Console.WriteLine(d(3));
We can still use an anonymous methods; however, we can also use lambda expressions to accomplish the same thing (see example below):
DoubleIt d = x => x * 2;
Console.WriteLine(d(3));
Example 3:
Lambda expression with multiple arguments
Let’s declare a delegate.
public delegate int Multiply(int a, int b);
…
Multiply multi = (a, b) => a * b;
Console.WriteLine(multi(2, 3));
Lambda Statements
Lambda statements follow the same principle as expression; but, it encloses a full statement within brackets.
Syntax Form:
Parameter(s) => { Statement }
Example 4:
X => { Return X * 2; }
Example 5:
Lambda statement that returns void
Let’s declare a delegate.
delegate void Greeter(string name);
...
Greeter g = new Greeter((string name) => {
Console.WriteLine("Hello " + name);
});
g("Walter");
Or
Greeter g = name => { string r = "Hello " + name; Console.WriteLine(r); };
g("Walter");
Output:
Hello Walter
Summary
The purpose of this post was to show you a basic implementation of lambda expressions in C# via examples without touching the functional programming subject.
Tuesday, January 11, 2011
HTML Radio Buttons and JavaScript
Here is a quick tutorial that shows how to read the selected value or selected index from a group of radio buttons using JavaScript.
I present the sample within the DeveloperCaster namespace (like all the samples in this blog); however, I decided to create a sub namespace for Forms so we will access these functions by calling DeveloperCaster.Forms.
var DeveloperCaster = {
Forms: {
}
}
Finding the selected index
The function goes straight to the point. We check if it is a valid object otherwise we return -1; then, we loop through every object in the collection until we find the checked item to return the index (-1 will be returned if no item is checked).
RadioSelectedIndex: function (id) {
if (typeof ($(id).length) != 'undefined') {
var index = -1;
var _obj = $(id);
for (var i = 0; i < _obj.length; i++) {
if (_obj[i].checked) {
index = i;
break;
}
}
return index;
}
return -1;
}
Finding the selected value
This function simply utilizes our previous function; so, we check if the selected index is not equal to -1 and then return the selected index value.
RadioSelectedValue: function (id) {
var index = DeveloperCaster.Forms.RadioSelectedIndex(id);
if (index != -1) {
return $(id)[index].value;
}
return "";
}
Here is the complete set of functions:
var DeveloperCaster = {
Forms: {
RadioSelectedIndex: function (id) {
if (typeof ($(id).length) != 'undefined') {
var index = -1;
var _obj = $(id);
for (var i = 0; i < _obj.length; i++) {
if (_obj[i].checked) {
index = i;
break;
}
}
return index;
}
return -1;
},
RadioSelectedValue: function (id) {
var index = DeveloperCaster.Forms.RadioSelectedIndex(id);
if (index != -1) {
return $(id)[index].value;
}
return "";
}
}
}
Usage
Note: Tutorial for the AddEvent function can be found here.
Compress this code here: http://www.jscompressor.com/
Usage
Note: Tutorial for the AddEvent function can be found here.
<html>
<head>
<script language=”javascript”>
window.onload = function () {
DeveloperCaster.AddEvent('btnValue', 'click', function () {
alert(DeveloperCaster.Forms.RadioSelectedValue('group1'));
});
DeveloperCaster.AddEvent('btnIndex', 'click', function () {
alert(DeveloperCaster.Forms.RadioSelectedIndex('group1'));
});
}
</script>
</head>
<body>
<input type="radio" name="group1" value="One"> One<br>
<input type="radio" name="group1" value="Two" checked>Two<br>
<input type="radio" name="group1" value="Three"> Three
<br />
<input type="button" name="btnValue" value="Selected Value" />
<input type="button" name="btnIndex" value="Selected Index" />
</body>
</html>
Compress this code here: http://www.jscompressor.com/
Motorola Xoom Table
This year's CES was about Tablet's and the Motorola Xoom running Android 3.0 was one of the better ones shown at the show.
What they are saying around the Web:
Subscribe to:
Posts (Atom)
