Monthly Archives: May 2010

When is the VB.Net Replace function not Replace?

0
Filed under VB Feng Shui

I ran into a little surprise a few days ago. With something as innocuous as the “Replace” function, of all things!

Now, Replace has been around since, well, forever. The basic function has always looked like this:

X = Replace(StringToSearch, StringToSearchFor, StringToReplaceWith)

No big deal. Used it for years.

So imagine my surprise when I got an “Object not set” error on a string variable that I’d just used Replace on! How could that be?

So I checked the variable value. It was, in fact, “Nothing”.

Ok, So I rerun the code, stepping through. The variable starts out as a string containing some data, then gets trimmed, and the resulting string in empty (still a string, just containing no characters).

Then I perform a replace on it, and suddenly, the string is Nothing?!

So, I coded up the following little test snippet.


    Private Sub Test()

        Dim x = "Test A B C"
        Debug.Print(Replace(x, "A", "Z"))
        'Results in "Test Z B C"

        Debug.Print(x.Replace("A", "Z"))
        'also results in "Test Z B C"

        x = ""
        x = x.Replace("A", "Z")
        Debug.Print(x Is Nothing)
        'result is false, (x stays "")

        x = ""
        x = Microsoft.VisualBasic.Replace(x, "A", "Z")
        Debug.Print(x Is Nothing)
        'result is true!?!

        x = ""
        x = Replace(x, "A", "Z")
        Debug.Print(x Is Nothing)
        'Result is true!?!

    End Sub

The first two results are exactly as expected. The search string is replaced with the replace string.

The next result, ALSO, is as expected. x starts out as an empty string. I perform a replace on it, and the result is, duh! an empty string! Now, for as long as I’ve known the Replace function, this has not been how it works.

But the next two results both result in FALSE, meaning the result of the String.Replace function and of the Microsoft.VisualBasic.Replace function is Nothing, and not an empty string!

Google It!

So I started googling. I turned up this bug report from .net 1.1.

The response from Microsoft is:

Posted by Microsoft on 5/18/2006 at 3:02 PM
Hi,
Thank you very much for reporting this issue. Unfortunately, VB had this behavior in previous version too and we can’t change it due to backward compatibility problem.
Thanks,
VB Development Team.

“Can’t be!” I yelled. Well, ok, I didn’t yell, but I exclaimed heartily <g>.

So I fired up VB6 and wrote up another test program, then ran it.

You can see the VB supplied variable inspector tooltip as I hovered over the value of X after I’d just run the normal REPLACE function on it in the EXACT SAME WAY as I’m doing in VB.net.

image

I hate to burst bubbles, but X starts out as an empty string, I run the REPLACE function on it, and it ends up… wait for it… an empty string! Not a null/nothing variable.

Maybe it’s another weird edge case scenario that the VB Team is talking about there, but, come on! This has got to be THE SINGLE MOST COMMON invocation of Replace. Why on earth should it work differently from VB6 to VB.net?

And one other thing. String.Replace is part of the .net FRAMEWORK people. It has virtually nothing to do with VB6 and certainly, compatibility shouldn’t have been a concern there. May for the version in the VisualBasic namespace, but not that String.Replace. Weak!

Short version of the story

Be VERY CAREFUL of using either String.Replace or the Microsoft.VisualBasic.Replace functions.

If you DO need them (they have a nice case-insensitive switch, which makes them handy), write a wrapper function that deals with this VB.net bug (yeah, I went there <g>) so you won’t get bitten and end up with rash.

PicPick, A Great Little Freeware ScreenCapture Tool

0
Filed under Utilities

At my last job, we used Snagit exclusively for screen captures. It’s a great tool, but it’s a tad pricey for doing quick screenshots on your home machine.

Just today, I was at a new contract gig and needed to do some screenshots. There wasn’t anything decent already available so I went googling for a bit.

First up. I believe I’ve talked about Faststone Capture before and that’s one of the first hits I got. But after downloading the latest version, I discovered that they’ve gone to a paid version with a 30 day eval. Don’t get me wrong, Faststone is a good screen capture util, and you might want to give it a whirl, but in this case, I was looking for something small and free.

Next try. Struck Gold!

PicPick is a little freeware app from wiziple.net. Odd name, but for free, it’s one amazingly polished screen capture app.

image

Let’s see, what’s to love?

  • It’s got a built in image editor for quick annotations.
  • It can do full screen caps, region caps, window caps, freehand caps, you name it.
  • It’s fast.
  • It can save to several different image formats

and lastly…

It’s got a built in screen protractor! WTH!

image

If you need to do screencaps on a budget, definitely check this one out!

Visual Studio Styles Made Easy

0
Filed under .NET

Luke Sampson’s put up a nice little site that’s all about custom color styles for Visual Studio (looks like most schemes support VS2005/2008 AND 2010!)

For example, the high rated scheme right now is WekeRoad:

image

Pick one and you can download the scheme right there. Very easy to get a custom scheme (or a good base for your own scheme) going quickly.

I tend to prefer something with a few less colors (I’m not keen on the circus look, no matter how muted), but some of these schemes look quite nice.

Finally, check out my post about programming fonts here for a good font comparison.

XPath Testing Tool (XPathMania)

0
Filed under Utilities, VB Feng Shui, XML

I mentioned RAD Software’s RegularExpression Designer a few posts back, but this time, I’m talking XPath.

If you haven’t already played with XPath, and you do much of anything at all with XML data, you owe it to yourself to check it out. Basically, it’s a query language that allows you to query for specific nodes or nodesets from an XML data source very easily. It’s also got some limited function and expression handling capabilities (typical math functions, string manipulation functions, etc).

XPath 1.0 is fully supported by the .net framework. Unfortunately, XPath 2.0, while much more capable, has been dropped by Microsoft in favor of, I believe, XQuery. Trouble is, I can’t find much in the way of VB.net support for XQuery right off.

But, all that aside, XPath 1.0 is still very much a useful tool to have in your arsenal, but, like regular expressions, getting an XPath query right can take some experimenting. That’s where XPathMania comes in.

It’s basically an XPath query tool extension to the VS 2008 IDE.

In the screenshot below, you can see that I’ve got an XML file loaded, and I’ve docked the XPathMania window just below it.

With that, I can easily enter a query again that XML file and view the results (along with the matching nodes actually highlighted in the XML file view! Good stuff!

image

Finally, you’ll also definitely want to check out this page for lots of good XPath example queries.

In the future, I’ll try to put together a little sample app that illustrates how to use the XPathNavigator to easily execute these queries against an XML document and enumerate the resulting nodeset.

FreeCommander Ain’t Half Bad

0
Filed under Utilities

The other day, I was needing to quickly view lots of log files in a folder. The Ol’ “Double click/open in notepad/close notepad/arrow down/repeat” process was getting old quick.

Now, at home, I’ve got Directory Opus, which would make this a breeze. But, it’s a licensed copy, and unfortunately, their licensing is a tad draconian. Besides, I was at an office, so I didn’t have my sernum or anything like that handy.

So, I started poking around. I’d looked at a pile of Explorer replacements awhile back. That’s how I ended up buying DirOpus. But this time, I needed something free I could download, install and get my job done.

A few minutes of googling pointed my to FreeCommander. I can’t say I even looked at this package before, because it’s billed as a “Norton Commander Clone”, and I never really got into Norton Commander.

image

That picture viewer you see on the right? It’ll view just about any file type, which was just what I was needing.

But come to find out, FreeCommander is much more than just a Norton Commander clone.

There’s lots of very cool features here, including:

  • Tabbed explorers
  • A decent Tree (it doesn’t show the full tree like explorer does, but it comes close enough to be useful)
  • A decent file viewer that can be “in place”, meaning it doesn’t open a new window to show the file being viewed.
  • A “Quick Filter” which is very handy.
  • Predefined Filters, for when the quick filter isn’t quite enough.
  • Lots of config options
  • Reasonably fast, even in dirs with lots of files.

And finally, so far, it’s been very stable.

In all, a very compelling Explorer replacement, especially considering the price!