Select the index of a specific object via LINQ

This will search for the index of an object in a collection depending on a specific criteria.
When the object is not found in the collection, it will return an index with value -1.


string searchValue = "d";
List<string> items = new List<string>() { "a", "b", "c", "d", "e" };

int foundIndex = items.Select((x, i) => new { x, i })
.Where(x => x.x == searchValue)
.Select(x => x.i)
.DefaultIfEmpty(-1)
.First();

// specific code for LINQPad
foundIndex.Dump();

The result will be 3, because “d” has index 3 in the collection.

HTML5 support for Visual Studio 2010 Editor

Today, a faction of the Web Platform and Tools team, spearheaded by Mads Kristensen, is pleased to announce the Visual Studio Web Standards Update. This adds better support for HTML5, CSS3 and new JavaScript features to ALL versions of Visual Studio.

More info and the download link here.

[ source ]

Debugger Canvas for Visual Studio

Microsoft and Brown University have collaborated to create Debugger Canvas, a free Power Tool that adds Code Bubbles™ to Visual Studio for a new way to debug.

Do you get lost in the document tabs? Are you tired of the debugger jumping around from file to file? Debugger Canvas pulls together the code you’re exploring onto a single pan-and-zoom display. As you hit breakpoints or step into code, Debugger Canvas shows just the methods that you’re debugging, with call lines and local variables, to help you see the bigger picture.

Release info:

We are currently in the last phase of shipping! The first alpha release will be available in early June 2011. (download link)

Something I will check out for sure! ;)
Update: works only with Visual Studio 2010 Ultimate :(

More info on the Microsoft Research page.

[ source ]

White screen of Darn

Ever had a White screen of Darn (WSOD) in Visual Studio or an error like this: “exception of type system.componentmodel.design.exceptioncollection was thrown” ?

If the [Clean Solution] and [Rebuild Solution] didn’t work.
Here is how to find the culprit:

  1. Open Visual Studio 2010 and open/load your solution containing the problem
  2. Open a second Visual Studio 2010 instance

In the second VS2010 instance do the following things:

  1. Go to [Tools] > [Attach to Process...]
  2. Select devenv.exe with which contains the solution with the problem from the Available Processes and click OK
  3. Next go to [Debug] > [Exceptions...]
  4. Check [Common Language Runtime Exceptions] in the [Thrown] column

Now go to your first Visual Studio 2010 instance with your project and open the designer file which causes you the PITA (Pain In The Ass).

Your second VS2010 instance should break on the exception, showing you where it goes wrong in your code.

Enjoy ;)

Visual Studio Styles

I’ve been using a new style in Visual Studio recently, because my eyes got a bit too tired of looking constantly at the white background of the editor in Visual Studio 2010.

Today I’ve installed the style also at work and I must say that my eyes like it ^^.

I used “Son of Obsidian” from StudioStyl.es.

Installing this style is simple, if you have problems read the StudioStyl.es FAQ.

Happy coding ;)

[ source ]

 

Variable-length parameters in C#

Sometimes you need to call a method with one default parameter and one extra parameter. The next moment you want to call that same method with its default parameter and two or more extra parameters. In other words, you want variable-length parameters.

In Java this can be done by using … inside the parameters(/arguments) list of a method.
In C# you have several options, depending on your logic.

One choice, which will in most cases not work because of your logic, is to use an optional parameter which can be set to null (or a default value). It requires you to check if the number2 variable is null or empty every time:

public void MyMethod(int number1, int? number2 = null)
 {
 //...
 }

 

You can also choose to use method overloading but instead of writing extra code you can use the params keyword (if it fits your logic).

This code below from this simple Console Application shows some basic usage of the params keyword. One main class to call the methods and one class where the methods were implemented:

using System;

namespace VariableParamsTest
{
 class Program
 {
 static void Main(string[] args)
 {
 MyClass test = new MyClass();

 test.DoSomething(1, 2, 3);
 test.DoSomething(9, 8, 7, 6, 5, 4, 3, 2, 1);

 Console.WriteLine();

 test.DoSomethingElse(new int[] { 1, 3, 5 }, new string[] { "hello", "hi", "bonjour", "hallo" });

 Console.WriteLine();

 test.DoSomethingDifferent("monkey", 42, new double[] { 1.0, 1.1, 1.5, 1.6 });

 Console.ReadLine();
 }
 }
}
using System;
using System.Reflection;

namespace VariableParamsTest
{
 public class MyClass
 {
 public void DoSomething(params int[] numbers)
 {
 foreach (int i in numbers)
 Console.WriteLine("parameter: " + i);
 }

 public void DoSomethingElse(params object[] things)
 {
 foreach (object thing in things)
 if (thing is Array)
 foreach (object o in (Array)thing)
 Console.WriteLine("parameter: " + o.ToString());
 }

 public void DoSomethingDifferent(string word, int number, params double[] numbers)
 {
 foreach (object par in MethodBase.GetCurrentMethod().GetParameters())
 Console.WriteLine("parameter type: " + par.ToString());
 }
 }
}

 

Pretty easy, have fun testing it out. ;)

More info about params can be found on MSDN.

 

Visual Studio LightSwitch (beta)

Microsoft Visual Studio LightSwitch gives you a simpler and faster way to create professional-quality business applications for the desktop, the web, and the cloud. LightSwitch is a new addition to the Visual Studio family.

They just released a Beta. ;)

[ source ] [ twitter ]