How to Programmatically Convert Vb6 Formatting Strings to .Net Formatting Strings

Is there a way to programmatically convert VB6 Formatting strings to .NET Formatting strings?

The formatting routine that VB6 uses is actually built into the operating system. Oleaut32.dll, the VarFormat() function. It's been around for 15 years and will be around for ever, considering how much code relies on it. Trying to translate the formatting strings to a .NET composite formatting string is a hopeless task. Just use the OS function.

Here's a sample program that does this, using the examples from the linked thread:

using System;
using System.Runtime.InteropServices;

class Program {
static void Main(string[] args) {
Console.WriteLine(Vb6Format("hi there", ">"));
Console.WriteLine(Vb6Format("hI tHeRe", "<"));
Console.WriteLine(Vb6Format("hi there", ">!@@@... not @@@@@"));
Console.ReadLine();
}

public static string Vb6Format(object expr, string format) {
string result;
int hr = VarFormat(ref expr, format, 0, 0, 0, out result);
if (hr != 0) throw new COMException("Format error", hr);
return result;
}
[DllImport("oleaut32.dll", CharSet = CharSet.Unicode)]
private static extern int VarFormat(ref object expr, string format, int firstDay, int firstWeek, int flags,
[MarshalAs(UnmanagedType.BStr)] out string result);
}

VB.Net Replacing the usage of VB6.Format

Here's a simple function that will take a string formatted like any of your inputs and the format you specify, and returns a string formatted like your outputs:

Private Function FormatTime(Input As String, Format as String) As String
Dim TimeFormat As New DateTime
Dim GoodString As Boolean = DateTime.TryParse(Input, TimeFormat)
If Not GoodString Then
If Integer.TryParse(Input, vbNull) Then
If Input.Length > 2 Then
Input = Input.PadLeft(4, "0"c)
TimeFormat = New DateTime(Now.Year, Now.Month, Now.Day, Integer.Parse(Input.Substring(0, 2)), Integer.Parse(Input.Substring(2)), 0)
Else
TimeFormat = New DateTime(Now.Year, Now.Month, Now.Day, Integer.Parse(Input.PadLeft(2, "0"c).Substring(0, 2)), 0, 0)
End If
End If
End If
Return TimeFormat.ToString(Format)
End Function

This does simple validating as well. It checks for proper time format and if not, if all characters are digits

Your statement would look like this:

`TimeFormatVariable = FormatTime(OriginalTimeInput, "hhmm")

How to update Format function from VB to VB.NET

In VBNet, you can also do this:

Dim rFormat As String = String.Empty
Dim fmt As String = "########.000"
Dim value As Object = 12345.2451212

rFormat = (CDbl(value)).ToString(fmt)

Adding a - with String.Format VB.Net

If the length of the string remains constant:

Dim WorkOrderNum = "WO123000"   
Dim testStr As String = String.Format("{0}-{1}", WorkOrderNum.SubString(0, 2), WorkOrderNum.SubString(2, 6))
Console.WriteLine(testStr)

'outputs WO-123000

Although I'm sure there's a better way of doing this which should include error handling etc this was just a demo!

VB6 Format function: analog in .NET

Another solution to look at is to use the Microsoft.VisualBasic.Compatibility.VB6 namespace, which contains several classes and methods that are backwards compatible with Visual Basic 6. It's primarily meant for upgrade tools, but it will save you the hassle of having to purchase a migration tool or write the code yourself.

MSDN Documentation: Support.Format Method (Microsoft.VisualBasic.Compatibility.VB6)

The parameters don't change and it basically supports the same functionality at least given your examples:

Imports Microsoft.VisualBasic.Compatibility.VB6

Console.WriteLine("HI THERE ")
Console.WriteLine(Support.Format("hi there", ">"))

Console.WriteLine("hi there ")
Console.WriteLine(Support.Format("hI tHeRe", "<"))

Console.WriteLine("HI ... not THERE")
Console.WriteLine(Support.Format("hi there", ">!@@@... not @@@@@"))

equivalent for vb6.Format function in vb.net without using Microsoft.Visualbasic.Compatibility.dll

You can use the .ToString(string) method

Dim value As Integer = 1234567
value.ToString("###,###,###,###")

or the String.Format Method which uses Composite Formatting

String.Format("{0:###,###,###,###}", 1234567)

C# equivalent to this VB6 string formatting?

Use the same separation by semi-column. Read more about that separator at msdn (supports up to three sections)

Console.WriteLine("{0:positive;negative;zero}", +1); //prints positive
Console.WriteLine("{0:positive;negative;zero}", -1); //prints negative
Console.WriteLine("{0:positive;negative;zero}", -0); //prints zero

You can use ToString on numeric value and pass format there

string formatted = 1.ToString("positive;negative;zero"); //will return "positive"

or use string.Format as shown in the comment section. But still you need to pass order position {0} to it.

string formatted = string.Format("{0:positive;negative;zero}", 1); 

In order to check for null, you can use null coalescing operator (cast to object is required, since there is no implicit cast from int? to string). It becomes quite messy, so I would recommend to consider simple if statement.

int? v = null;
var formatted = string.Format("{0:positive;negative;zero}", (object) v ?? "null");

Trouble with Time Formating in Asp.Net

You can use TimeSpan.

This Function will return a TimeSpan from your String-value.

Public Function GetTimeSpan(s As String)
Dim span As TimeSpan
s = s.Trim 'Trim spaces

'Check if string only contains numbers and if length is valid, else throw exception
If System.Text.RegularExpressions.Regex.IsMatch(s, "^[0-9]+$") AndAlso
s.Length >= 3 AndAlso
s.Length <= 6 Then

s = s.PadRight(6, "0")
Else
Throw New Exception("Invalid String!")
End If

TimeSpan.TryParseExact(s, "hhmmss", Globalization.CultureInfo.InvariantCulture, span)

Return span
End Function

And use it like this:

MessageBox.Show(GetTimeSpan("135700").ToString)
MessageBox.Show(GetTimeSpan("135").ToString)


Related Topics



Leave a reply



Submit