.NET Tip of The Day
Learn one new .NET trick every day
Быстрое пополнение счета телефона      Login or Join

.NET Tips & Tricks Community (ALPHA)

First 1 2 3 4 5  ... Last  RSS

Format the current document in Visual Studio

Press ctrl +E, D, It will apply the format(like indent, spacing etc)  to whole current document.

submitted by Yaseen
11/26/2008

WPF Equivalent for ListView.GetItemAt(e.X,e.Y)

FrameworkElement framework = e.OriginalSource as FrameworkElement;

ListViewItem lvi = Lstview.ItemContainerGenerator.ContainerFromItem(fe.DataContext) as ListViewItem;

submitted by Renuka
9/16/2008

Generating artificial phone numbers (in North America)

Having worked for a company that developed phone fraud detection software in a previous life, I was familiar with the North American Numbering Plan described in Wikipedia. Several times I’ve had a need to create artificial phone numbers.

Below is an algorithm that will handle most if not all of these numbers.

private string GetRandomPhone()

{

    // Generate a NANP compliant phone number (look in wikipedia under North American Numbering Plan)

    var rnd = new Random();

 

    // A number consists of 3 parts: NPA Nxx Station

    // NPA (Numbering Plan Area code) = [2-9][0-8][0-9]

    // Exclude common toll and toll-free numbers

    int npa = 0;

    while (npa == 0 || npa == 800 || npa == 877 || npa == 888 || npa == 900)

    {

        npa = rnd.Next(2, 9) * 100 + rnd.Next(0, 8) * 10 + rnd.Next(9);

    }

 

    // NXX (Central Office or Exchange code) = [2-9][0-9][0-9]

    // Exception the second digits may not be 11

    int nxx = 0;

    while (nxx == 0 || nxx % 100 == 11)

    {

        nxx = rnd.Next(2, 9)*100 + rnd.Next(99);

    }

 

    // Station code = [0-9][0-9][0-9][0-9]

    // When 555 is the NXX the number 100-199 are reserved or fictional numbers)

    int station = 0;

    while (station == 0 || (nxx == 555 && station > 99 && station < 200))

    {

        station = rnd.Next(9999);

    }

 

    const string format = "({0:000}) {1:000}-{2:0000}";

    return string.Format(format, npa, nxx, station);

}

 

submitted by jtheisen
8/28/2008

LINQ to XML method chaining

First, I have to say that I really love using LINQ to SQL and LINQ to Objects. I wasn’t able to say the same thing about LINQ to XML until recently.

Here’s an XML document that I’ll use for this example:

<Zoo>

  <Animals>

    <Animal species="monkey">George</Animal>

    <Animal species="monkey">Zanzi</Animal>

    <Animal species="panda">Long-Long</Animal>

  </Animals>

</Zoo>

I’m very familiar with XPath expressions and if I wanted to find all of the panda’s in the Zoo I could write an expression like:

/Zoo/Animals/Animal[@species=’panda’]

So that works great and it would return to me the element containing ‘Long-Long’. Now for the LINQ to XML equivalant without using the XPath Extensions Method.

var panda = doc.Element("Zoo").Element("Animals").Descendants("Animal")

    .Where(e => e.Attribute("species").Value == "panda");

The problem is that all of the italic, underlined and in red text (i.e. the .Element() and .Attribute()) methods can return null. So, I went from a clear and consise XPath expression to a less readable LINQ call that I should be checking for null for each of the Element and Attribute calls. Hmmm, not good.

Fortunately, .NET 3.5 introduces Extension Methods that allow us to extend existing classes with new Methods. Here’s the class that I came up with:

public static class XmlExtensions

{

    public static XElement SafeElement(this XContainer container, string name)

    {

        return container.Element(name) ?? new XElement(name);

    }

 

    public static XAttribute SafeAttribute(this XElement element, string name)

    {

        return element.Attribute(name) ?? new XAttribute(name, "");

    }

}

For an introduction on Extension Methods, read this blog entry from ScottGu here. Note that there are three indicators that this is an extension method:

  1. The class has been declared static.
  2. Each of the extension methods is also static.
  3. There is a “this” keyword before the first parameter argument.

The ‘SafeElement’ method is extending the XContainer class and the ‘SafeAttribute’ method is extending the XElement class.

We these are used in our example we get:

var panda = doc.SafeElement("Zoo").SafeElement("Animals").Descendants("Animal")

    .Where(e => e.SafeAttribute("species").Value == "panda");

If you are using Resharper you will see the squiggles disappear from the expression now. So, how and why does this work and allow us to use expression chaining? Since we are guaranteed to get an either an XElement or XAttribute back from the respective calls, we won’t blow up with a NullReferenceException. That’s good. In addition, since we return an empty Element or Attribute, our other chained calls will fail cleanly, thus the Lamba Expression will not match any items if we had loaded a different XML document.

 

submitted by jtheisen
8/27/2008
First 1 2 3 4 5  ... Last  RSS
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.