/
C# Migration Highlights
C# Migration Highlights
Software migration is a complex process. During a transformation one must consider multiple aspects of the output code (Equivalence, Complexity, Readability, Maintainability, etc.) In this blog, I wanted to highlight some of our VB6 to C# (.NET) transformations.
File IO
Before | After |
---|---|
If Dir(LogPath, vbDirectory) <> "" Then | if (System.IO.Directory.Exists(LogPath)) |
If Dir(FName) <> "" Then | if (System.IO.File.Exists(FName)) |
DateDiff Method
Before | After |
---|---|
i = DateDiff("YYYY", d2, d) i = DateDiff("M", d2, d) i = DateDiff("D", d2, d) i = DateDiff("H", d2, d) i = DateDiff("N", d2, d) i = DateDiff("S", d2, d) | i = (d.Year - d2.Year); i = ((d.Month + (d.Year * 12)) - (d2.Month + (d2.Year * 12))); i = ((int)d.Date.Subtract(d2.Date).TotalDays); i = ((int)d.Subtract(d2).TotalHours); i = ((int)d.Subtract(d2).TotalMinutes); i = ((int)d.Subtract(d2).TotalSeconds); |
MsgBox Method
Before | After |
---|---|
'MsgBox with message and title only i = MsgBox("a message box test", , "title") If MsgBox("a message box test", , "title") Then End If | // MsgBox with message and title only i = MessageBox.Show("a message box test","title",MessageBoxButtons.OK); if (MessageBox.Show("a message box test","title",MessageBoxButtons.OK) == DialogResult.Yes) { } |
i = MsgBox("Save File Before Closing ?", 51) | i = MessageBox.Show("Save File Before Closing ?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Exclamation); |
Call MsgBox("a message box test", vbCritical Or _ vbYesNoCancel Or _ vbAbortRetryIgnore Or _ vbApplicationModal Or _ vbDefaultButton2 Or _ vbQuestion, "title") | MessageBox.Show("a message box test","title",MessageBoxButtons.YesNoCancel | MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error | MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification); |
Like Statement
Before | After |
---|---|
'Tests copied from http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx ' The following statement returns True (does "F" satisfy "F"?) wBA "Like", "'F' Like 'F'", "F" Like "F" ' ' The following statement returns False for Option Compare Binary ' and True for Option Compare Text (does "F" satisfy "f"?) wBA "Like", "'F' Like 'f'", "F" Like "f" ' ' The following statement returns False (does "F" satisfy "FFF"?) wBA "Like", "'F' Like 'FFF'", "F" Like "FFF" ' ' The following statement returns True (does "aBBBa" have an "a" at the ' beginning, an "a" at the end, and any number of characters in ' between?) wBA "Like", "'aBBBa' Like 'a*a'", "aBBBa" Like "a*a" ' ' The following statement returns True (does "F" occur in the set of ' characters from "A" through "Z"?) wBA "Like", "'F' Like '[A-Z]'", "F" Like "[A-Z]" ' ' The following statement returns False (does "F" NOT occur in the ' set of characters from "A" through "Z"?) wBA "Like", "'F' Like '[!A-Z]'", "F" Like "[!A-Z]" ' ' The following statement returns True (does "a2a" begin and end with ' an "a" and have any single-digit number in between?) wBA "Like", "'a2a' Like 'a#a'", "a2a" Like "a#a" ' ' The following statement returns True (does "aM5b" begin with an "a", ' followed by any character from the set "L" through "P", followed ' by any single-digit number, and end with any character NOT in ' the character set "c" through "e"?) wBA "Like", "'aM5b' Like 'a[L-P]#[!c-e]'", "aM5b" Like "a[L-P]#[!c-e]" ' ' The following statement returns True (does "BAT123khg" begin with a ' "B", followed by any single character, followed by a "T", and end ' with zero or more characters of any type?) wBA "Like", "'BAT123khg' Like 'B?T*'", "BAT123khg" Like "B?T*" ' ' The following statement returns False (does "CAT123khg"?) begin with ' a "B", followed by any single character, followed by a "T", and ' end with zero or more characters of any type?) wBA "Like", "'CAT123khg' Like 'B?T*'", "CAT123khg" Like "B?T*" | // Tests copied from http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx // // The following statement returns True (does "F" satisfy "F"?) wBA("Like","'F' Like 'F'",Regex.IsMatch("F","F").ToString()); // // The following statement returns False for Option Compare Binary // and True for Option Compare Text (does "F" satisfy "f"?) wBA("Like","'F' Like 'f'",Regex.IsMatch("F","f").ToString()); // // The following statement returns False (does "F" satisfy "FFF"?) wBA("Like","'F' Like 'FFF'",Regex.IsMatch("F","FFF").ToString()); // // The following statement returns True (does "aBBBa" have an "a" at the // beginning, an "a" at the end, and any number of characters in // between?) wBA("Like","'aBBBa' Like 'a*a'",Regex.IsMatch("aBBBa","a.*a").ToString()); // // The following statement returns True (does "F" occur in the set of // characters from "A" through "Z"?) wBA("Like","'F' Like '[A-Z]'",Regex.IsMatch("F","[A-Z]").ToString()); // // The following statement returns False (does "F" NOT occur in the // set of characters from "A" through "Z"?) wBA("Like","'F' Like '[!A-Z]'",Regex.IsMatch("F","[^A-Z]").ToString()); // // The following statement returns True (does "a2a" begin and end with // an "a" and have any single-digit number in between?) wBA("Like","'a2a' Like 'a#a'",Regex.IsMatch("a2a","a\\da").ToString()); // // The following statement returns True (does "aM5b" begin with an "a", // followed by any character from the set "L" through "P", followed // by any single-digit number, and end with any character NOT in // the character set "c" through "e"?) wBA("Like","'aM5b' Like 'a[L-P]#[!c-e]'",Regex.IsMatch("aM5b","a[L-P]\\d[^c-e]").ToString()); // // The following statement returns True (does "BAT123khg" begin with a // "B", followed by any single character, followed by a "T", and end // with zero or more characters of any type?) wBA("Like","'BAT123khg' Like 'B?T*'",Regex.IsMatch("BAT123khg","B.?T.*").ToString()); // // The following statement returns False (does "CAT123khg"?) begin with // a "B", followed by any single character, followed by a "T", and // end with zero or more characters of any type?) wBA("Like","'CAT123khg' Like 'B?T*'",Regex.IsMatch("CAT123khg","B.?T.*").ToString()); |
FormatDateTime Method
Before | After |
---|---|
str = FormatDateTime(d, vbGeneralDate) str = FormatDateTime(d, vbLongDate) str = FormatDateTime(d, vbShortDate) str = FormatDateTime(d, vbLongTime) str = FormatDateTime(d, vbShortTime) | str = d.ToString(); str = d.ToLongDateString(); str = d.ToShortDateString(); str = d.ToLongTimeString(); str = d.ToString("HH:mm"); |