Archive for the 'code' Category

Recursively delete all .svn directories

Sometimes those .svn files just get in the way, especially when you want to ship your code off to someone else. Here’s a quick way to delete all of them from the command line. We’ll use the find command to locate all the .svn directories and then pass them to xargs, which will execute the “rm -rf” command for every line of output from the find results.


find . -name *.svn | xargs rm -rf

Adding a blank / default item to an ASP.Net dropdown list and making it required

Here’s the declarative way to add a default first item to an ASP.Net drop down list:



Notice the important attribute: AppendDataBoundItems. This just tells ASP.Net to add your dynamically data-driven list items after any declarative markup items.

Now to make it required, you can just add a Required Field Validator:


ControlToValidate="ddCategory1" Text="* required">

Note, you just have to set your initial value to the value of your declarative first list item in the drop down. This tells the required field validator that this value doesn’t count and that the user haven’t selected an item yet.

Declarative markup is easy!

Getting the selected (or de-selected) ListBoxItems in WPF

Getting the ListBoxItem during a selection event used to be much easier in standard winforms. In the new SelectionChanged event in WPF, you don’t get the ListBoxItem that generated the selection event, you get back the data item that was used to bind to that ListBoxItem. So how do you get the ListBoxItem? It’s actually not so hard, you just have to ask the ItemContainerGenerator to give it to you. For example:


void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox = (ListBox) sender;

// handle selected items
foreach (MyClass datum in e.AddedItems)
{
ListBoxItem listBoxItem = (ListBoxItem) listBox.ItemContainerGenerator.ContainerFromItem(datum);
// do something with the listBoxItem
}

// handle deselected items
foreach (MyClass datum in e.RemovedItems)
{
ListBoxItem listBoxItem = (ListBoxItem) listBox.ItemContainerGenerator.ContainerFromItem(datum);
// sometimes this can be null
if (listBoxItem == null)
break;

// do something with the listBoxItem
}
}

Starting and stopping an iterator

Sometimes you want to stop and start an iterator and continue from where you last stopped. For example, in the code below, we ask for 2 names, stop and then ask for 3 more names.


static void Main(string[] args)
{
NameSource nameSource = new NameSource();

// get 2 names
foreach(string s in nameSource.GetNames(2))
{
Console.Out.WriteLine(”{0}”, s);
}

// get 3 more names
foreach (string s in nameSource.GetNames(3))
{
Console.Out.WriteLine(”{0}”, s);
}

Console.ReadLine();
}

To have the iterator know where it left off, we simple store the current place in the iteration in state variable. For example, in the code below we store the index of the current item being iterated over in the state variable ‘current’:


public class NameSource
{
///


/// Keeps track of the names already returned and starts off where it left off.
///

public IEnumerable GetNames(int amount)
{
for(int i = 0; i < amount; i++)
{
yield return strings[current++];
}
}

///


/// initialize state variable ‘current’
///

private int current = 0;
private static readonly string[] strings =
new string[] { “ankur”, “buster”, “ricardo”, “jerome”, “constantine”};
}

Actually, the article title is a misnomer. We’re not actually starting and stopping the same iterator, we’re creating two iterators that are referencing the same state variable. This makes it appear as one continuous iteration.