Adsense

Showing posts with label Tip. Show all posts
Showing posts with label Tip. Show all posts

Monday, August 11, 2008

How to Replace/Remove Blank/Empty Lines in Visual Studio using Regular Expressions

I'm mainly just posting this so that I don't forget it. I find myself needing this every now and again and need an easy place to find it. Hope it helps someone else. =)
If you want to remove or replace the Empty Lines in a Visual Studio file, you can perform the following.
  • Press Control + H on your keyboard to bring up the replace window.
  • In the Find What: field, enter ^$\n
  • In the Replace With: field, enter nothing
  • Check the Use: checkbox and then select Regular Expressions from the drop-down
  • Click Replace All
Note: If you want to remove the blank lines from just a part of your file, select/highlight it prior to performing the step above and make sure Selection is selected in the Look in: field.
Hope this helped. =)

Tuesday, May 27, 2008

How to Sort a generic IList or List

A while back I wrote a post on How to sort a Generic IList. Here is another approach.
To sort a generic List<T>, you can create a class that implements IComparer<T> and tells the list how to sort itself. Its fairly straight forward and the example below shows how to sort Widgets.
A Widget is a simple class that has a public string Name and a public int Size. I want to be able to sort generic lists of widgets (List<Widget>) by name and by size. You can see my code below.
The widget and sorter implementations
public class Widget {
    public string Name = string.Empty;
    public int Size = 0;

    public Widget(string name, int size) {
this.Name = name;
this.Size = size;
}
}

public class WidgetNameSorter : IComparer<Widget> {
    public int Compare(Widget x, Widget y) {
        return x.Name.CompareTo(y.Name);
}
}

public class WidgetSizeSorter : IComparer<Widget> {
    public int Compare(Widget x, Widget y) {
return x.Size.CompareTo(y.Size);
}
}
And application to create the list and sort it
class Program {
static void Main(string[] args) {
List<Widget> widgets = new List<Widget>();
widgets.Add(new Widget("Zeta", 6));
widgets.Add(new Widget("Beta", 3));
widgets.Add(new Widget("Alpha", 9));

OutputWidgets(widgets);
widgets.Sort(new WidgetNameSorter());
OutputWidgets(widgets);
widgets.Sort(new WidgetSizeSorter());
OutputWidgets(widgets);

Console.ReadLine();
}

public static void OutputWidgets(List<Widget> widgets) {
foreach (Widget w in widgets) {
Console.WriteLine(string.Format("{0} - {1}", w.Name, w.Size));
}
Console.WriteLine(string.Empty);
}
}
Running the application above results in the following output.
output
Straight forward enough? =)
So what do you do if you have a generic IList<T> and want to sort it? Since you only have the interface reference that can't make assumptions about what's implementing it, you have no Sort method. You can use the ToList() method to convert the IList<T> into a List<T>, and then call the Sort() method. See the updated example below. (Note: I previously blogged about a way to sort a generic IList using the ArrayList.Adapter. You can read about it HERE.)
class Program {
static void Main(string[] args) {
IList<Widget> widgets = new List<Widget>();

widgets.Add(new Widget("Zeta", 6));
widgets.Add(new Widget("Beta", 3));
widgets.Add(new Widget("Alpha", 9));

OutputWidgets(widgets);
widgets.ToList().Sort(new WidgetNameSorter());
OutputWidgets(widgets);
widgets.ToList().Sort(new WidgetSizeSorter());
OutputWidgets(widgets);

Console.ReadLine();
}

public static void OutputWidgets(IList<Widget> widgets) {
OutputWidgets(widgets.ToList());
}

public static void OutputWidgets(List<Widget> widgets) {
foreach (Widget w in widgets) {
Console.WriteLine(string.Format("{0} - {1}", w.Name, w.Size));
}
Console.WriteLine(string.Empty);
}
}

Saturday, March 08, 2008

How To Fix Dead/Stuck Pixels on a Playstation Portable (PSP)

So, I broke down and bought a Playstation Portable (PSP) this weekend. Lo and behold, it has a single stuck pixel. I didn't even notice it until I fired up a game for the first time, but there it is... A little, constantly red, annoying little bastard!

I really hate confronting retailers with returns, especially when its a single pixel that I'm complaining about. This wouldn't be the first time that I've done it though. But before I packed the PSP back up and headed for Best Buy, I figured I'd check online to see if this was a common problem. Unfortunately, it is a very common problem with PSPs and many people that have exchanged a PSP due to dead pixels, reports that their new unit has even more. So I count myself somewhat lucky to only have a single stuck pixel.

Luckily, I happened upon a post that talked about a little movie that that was originally distributed to help fix dead/stuck pixels on LCD TVs and monitors. The video is a series of screen that flash repeatedly in each of the colors that a pixel supports (Red, Greed, Blue.) The idea being that you can coax a stubbornly stuck pixel into working (or raise a dead pixel from the grave) by constantly forcing all of the pixels of the display to cycle through all of their possible states. (great idea!)

(Note: A stuck pixel is a pixel that only displays a single color (red in my case) and doesn't cycle to any other color. A dead pixel is a pixel that is black and never displays a color. The success rate seems to be higher with stuck pixels over dead ones.)

I downloaded the PSP version and have it currently running on my PSP, but some people have reported success in minutes, while other have had to let the video run for several hours. The consensus seems to be, that if the video doesn't fix your stuck/dead pixel in 24 hours, you might just be stuck with it.

I post this here, because I know of a lot of people that have stuck/dead pixels on their LCD monitors or laptop screen, and I figure that this might be worth a try on those as well. I'll post a follow up and let everyone know how it worked on my PSP. =)

You can download the movie file here, and read about it here.

Good luck and let me know if it works for you (especially when not used on a PSP.)

UPDATE

Well, I ran the video for probably 7 hours on my new PSP with no luck. The red little bastard pixel remained. Luckily, Best Buy was great at letting me return my unit for a new one. And I haven't been able to find any other stuck/dead pixels.

Hope you have better luck.