Monday, July 6, 2009

Setting the Exit Code when exiting a VB.NET or C# Console App

To set the Exit Code (ErrorLevel) when a VB.NET or C# Console Application exits, use the following (or similar):

Dim retCode as Integer = 0
.
.
.
System.Environment.Exit( retCode)



The call to Exit will end the application.

Thursday, July 2, 2009

Implementing a SQL-like 'LIKE' Comparison in VB.NET

I searched the net for some time to find an easy way to implement the logic in the SQL LIKE in a VB.NET function. There were many over simplified examples. Then I found this elegant solution using regular expressions (regex). The original is found here in C#:

http://bytes.com/groups/net-c/253519-using-regex-create-sqls-like-like-function


I have provided a VB.Net version below:


Public Function IsSqlLikeMatch(ByVal input As String, ByVal pattern As String) As Boolean

' Turn "off" all regular expression related syntax in
' the pattern string.


pattern = Regex.Escape(pattern)

' Replace the SQL LIKE wildcard metacharacters with the
' equivalent regular expression metacharacters.


pattern = pattern.Replace("%", ".*?").Replace("_", ".")

' The previous call to Regex.Escape actually turned off
' too many metacharacters, i.e. those which are recognized by
' both the regular expression engine and the SQL LIKE
' statement ([...] and [^...]). Those metacharacters have
' to be manually unescaped here.


pattern = pattern.Replace("\[", "[").Replace("\]", "]").Replace("\^", "^")

Return Regex.IsMatch(input, pattern)

End Function

Thursday, June 25, 2009

Converting code between VB.NET and C# using SharpDevelop

While I really love the online VB.NET to C# and C# to VB.NET code converters provided by developerFusion, I have also found the open source SharpDevelop application to be a very good tool for converting .NET code from one flavor to another. It is especially good at converting whole projects or larger amounts of code. I think it is worthy a post:

http://www.icsharpcode.net/OpenSource/SD/

Code Conversion Features:

http://community.sharpdevelop.net/blogs/mattward/articles/FeatureTourCodeConversion.aspx

Code Conversion is one very small part of SharpDevelop. The rest is worth a look too!

Friday, June 5, 2009

Converting a Byte Array to a String in VB.NET

Here is one way to convert a byte array to a String in VB.NET:


Dim bArray() As Byte = {65, 66, 67, 68, 69}
Dim str As String
str = System.Text.Encoding.ASCII.GetString(bArray)



More examples of converting Byte Arrays to Strings and vice versa in VB.NET and C# can be found here:

http://www.chilkatsoft.com/faq/DotNetStrToBytes.html

Getting Application Path for a Console App

In a previous post, I gave examples of getting the Application Path for a Windows Form app and a Windows Mobile app. Getting the Application Path for a Console app is similar to getting one for the Windows Mobile app:


Dim appPath As String = _
System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

Monday, May 18, 2009

TechEd 2009 in Los Angeles



This year the TechEd Developers Conference was merged with the TechEd IT conference and happened last week in Los Angeles. While there were significantly fewer Developer oriented sessions, it was interesting to sit in some of the IT sessions. With around 700 sessions to choose from and only about 20 you can actually attend, the need to be particular about what you want to learn becomes very important. I heavily weighted my track with “Mobile”, but I also checked out SQL Server’s Spatial capabilities, and the future directions of the development environments and programming languages.

The push, I mean focus, this year was definitely the yet to be released Windows 7 and Windows Server 2008 R2, followed by a healthy dose of Virtualization in the form of Hyper-V and Windows Virtual PC.

I did have an opportunity to run my apps on a beta copy of Windows 7 at the “Bring Your Own App Lab.” There were a few glitchy things with reading some values out of the registry, but overall I was very pleased with how my apps worked the first time on Windows 7. I have already checked off a lot of grief on my to-do list from this experience. The free booze they were passing out was not even necessary to favorably color my opinion of Windows 7.

The focus last year was definitely LINQ, which seems to have been toned down now to is actual value. WPF and Silverlight, looked cool to try, but now they look usable. Microsoft’s creation of tools to help spawn the development of new programming languages is supposed to bring about an explosion in new languages. I along with everyone else in the Polyglot Programmer session cringed at the idea of having to learn dozens of new languages, but like LINQ, this idea may fade into a manageable and productive tool in our programming arsenal. I was hoping to see Windows Workflow (WF) be a hotter topic than it apparently is, but it may take a few iterations before it catches on.

I was pretty pleased with TechEd 2009. I will continue to process the information for a while.



Tuesday, May 5, 2009

Scaling one Rectangle to fit in Another Rectangle

Here is a great posting that shows several approaches to scaling one rectangle to fit inside of another:

http://www.vbdotnetforums.com/graphics-gdi/33829-scale-rectangle-fit-another.html