March 20, 2010

Generate Odd Numbers within a Range using LINQ

0 comments

The Enumerable.Range method generates a sequence of integral numbers within a specified range. Here’s how to use it to generate a sequence of odd numbers within a given range say 20 to 40:

C#

static void Main(string[] args)
{
IEnumerable<int> oddNums =
Enumerable.Range(20, 20).Where(x => x % 2 != 0);

foreach (int n in oddNums)
{
Console.WriteLine(n);
}
Console.ReadLine();

}

VB.NET

Sub Main(ByVal args() As String)
Dim oddNums As IEnumerable(Of Integer) = _
Enumerable.Range(20, 20).Where(Function(x) x Mod 2 <> 0)

For Each n As Integer In oddNums
Console.WriteLine(n)
Next n
Console.ReadLine()

End Sub

Note: Just remember that the Enumerable.Range accepts two parameters: Start and Count. So if you want to generate numbers from 25 to 50 (inclusive of both), you would say Enumerable.Range(25,26) since the second parameter is the number of sequential integers to generate.

OUTPUT

Generate Odd Numbers LINQ

March 19, 2010

Learn Silverlight using this Free Online Training Program

0 comments

Microsoft recently announced the launch of .toolbox! .toolbox is a free online training program where designers and developers can learn to create Silverlight applications using Expression Studio and to apply basic UX concepts to their solutions.

.toolbox

Once you visit .toolbox, Sign-In using your LiveID and create a profile

.toolbox

and then your avatar.

.toolbox

You are all set to now attend the Silverlight School! Select a module, watch the videos, and follow along using the provided guide and assets. Take the evaluation for a completed level to achieve a badge. Broadcast your achievements by posting to Twitter and Facebook! Watch a .toolbox YouTube video to learn more.

Happy Learning!

March 18, 2010

Join Two String Arrays with Distinct values using LINQ

0 comments

One of my colleagues was looking out for a simple way to join two string arrays and avoid duplicates (if any) in those arrays. I asked him to use the Union method which excludes duplicates from the return set.

C#

static void Main(string[] args)
{
string[] arr1 = { "One", "Two", "Four", "Six" };
string[] arr2 = { "Three", "Two", "Six", "Five" };

var arr3 = arr1.Union(arr2);

foreach (string n in arr3)
{
Console.WriteLine(n);
}
Console.ReadLine();

}

VB.NET (option infer on)

Sub Main(ByVal args() As String)
Dim arr1() As String = { "One", "Two", "Four", "Six" }
Dim arr2() As String = { "Three", "Two", "Six", "Five" }

Dim arr3 = arr1.Union(arr2)

For Each n As String In arr3
Console.WriteLine(n)
Next n
Console.ReadLine()

End Sub

OUTPUT

Merge String Arrays LINQ

March 17, 2010

Silverlight 4 Release Candidate

0 comments

During the MIX10 Keynote on March 15, 2010, Scott Guthrie announced the release of Silverlight 4 Release Candidate (RC). The final release is expected to be sometime in April this year. One of the other related announcements that got me all excited was that we can now create apps for Windows Phone 7 using Silverlight 4.

Here are some important links related to this release:

Getting Started with Silverlight 4

What’s New in Silverlight 4?

Download Silverlight 4 Tools

March 16, 2010

Add a Twitter Button into Blogger next to the Post Title

0 comments

You must have observed the new Twitter Button on each post. Within minutes of adding it, a devcurry reader mailed me asking how I implemented the code. It’s quite easy. I am using the tweetmeme service to add a Twitter Button into Blogger with the help of this post Integrating the button into Blogger

Step 1: Sign Into your Blogger account > Layout > Edit HTML. Make sure you save a copy by clicking the link ‘Download Full Template’. Check the box ‘Expand Widget Templates’

Step 2: Copy the code shared in the link I shared earlier. Now this code adds the twitter button at the end of each post. However what we want is to add a Twitter Button next to the Post Title.

Making sure you have checked the box ‘Expand Widget Templates’, search for the text ‘post-header-line-1’

Blogger Twitter Button

Step 3: Just before the closing div tag, add the following code as shown below:

<b:if cond='data:blog.homepageUrl != data:blog.url'>
<script type='text/javascript'>
tweetmeme_url = &#39;<data:post.url/>&#39;;
</script>
<script src='http://tweetmeme.com/i/scripts/button.js type='text/javascript'> </script>
</b:if>

Blogger Twitter Button

The piece of code 'data:blog.homepageUrl != data:blog.url' checks if the page is not the homepage url and displays the button only on individual posts.

Step 4: Preview the template and then save it.

Now every post of your should have the Tweet Button displayed next to the Post Title

Blogger Twitter Button

March 15, 2010

Replace Extensions of Images from .jpg to .png using jQuery

1 comments

A recent requirement demanded that all the .jpg images on a page be replaced with .png’s. Here’s how to solve this requirement using jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title>Change Image Extensions</title>
<
script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js">
</
script>
<script type="text/javascript">
$(function() {
$("#replAll").click(function() {
$('img').each(function() {
var src = $(this).attr("src");
$(this).attr("src", src.replace(/\.jpg$/i, ".png"));
});
});
});
</script>
</
head>
<
body>
<
input id="replAll" type="button" value="Replace" /><br />
<
img src="images/abc.jpg" />
<
img src="images/emo.jpg" />
</
body>
</
html>

OUTPUT

jQuery change extensions

Clicking on the ‘Replace’ button changes all .jpg’s to their .png’s counterpart. I have used different images to be able to demonstrate that the images did get changed.

jQuery change extensions

March 14, 2010

Smarter Intellisense in Visual Studio 2010

0 comments

Intellisense in Visual Studio 2010 just got smarter! VS 2010 now not only does a search for the text based on the starting letters, it also does a search for “sub-string” occurrences for the text and prompts a list of possible occurrences. Here’s an example. I have declared a textbox control called ‘txtIAmATextboxWithALongName’

Now observe as I type a substring ‘lon’, the intellisense brings up the control name

Intellisense VS 2010

Cool!

 

Copyright 2010 All Rights Reserved DevCurry.com by Suprotim Agarwal