有没有办法以编程方式将 VB6 格式字符串转换为 .NET 格式字符串?

发布于 2024-09-30 08:16:04 字数 347 浏览 1 评论 0原文

  1. 有谁知道 VB6 格式字符串的好参考吗?
  2. 有谁知道从 VB6 格式字符串到 .NET 字符串的转换器吗?

我正在致力于将大型 VB6 代码库移植到 .NET。它是一个以数据库为中心的软件,数据库本身保存 VB6 格式字符串,稍后加载并用于显示数据库中的其他数据。

我的问题,就像这篇文章一样,是如何移植它。然而,为该问题选择的答案不足以满足我的需要。我对依赖专门为向后兼容而专门设计的库感到不舒服,我是专门聘请来移植的。

  1. Does anyone know of a good reference for VB6 format-strings?
  2. Does anyone know of a converter from VB6 formatting strings to .NET strings?

I'm working on porting a large VB6 code base to .NET. It is a database centric piece of software, and the database itself holds VB6 format-strings which are later loaded and used to display other data in the database.

My question, like this article, is how to port this. However the answer chosen for that question isn't adequate for my needs. I'm uncomfortable relying on libraries specifically designed for backwards compatibility with a language I've been specifically hired to port away.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花细雨 2024-10-07 08:16:04

VB6 使用的格式化例程实际上内置于操作系统中。 Oleaut32.dll,VarFormat() 函数。考虑到有多少代码依赖它,它已经存在了 15 年,并且将永远存在。尝试将格式化字符串转换为 .NET 复合格式化字符串是一项无望的任务。只需使用操作系统功能即可。

这是一个使用链接线程中的示例执行此操作的示例程序:

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);
}

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);
}
单身情人 2024-10-07 08:16:04

您最好的选择可能是自己编写转换库,但这可能不是您希望得到的答案。

Your best bet might be to write the conversion library yourself, but that probably wasn't the answer you had hoped to get.

因为看清所以看轻 2024-10-07 08:16:04

下面是一些 F# 代码,可将大多数预定义和自定义 VB6 样式的数字和日期格式字符串转换为适合 String.Format 的内容。当然,它可以很容易地从 C# 或 VB 调用。

open System

module VB6Format =

    /// Converts a VB6-style format string to something suitable for String.Format()
    let Convert(vb6Format) =
        if String.IsNullOrWhiteSpace(vb6Format) then "{0}" else
        match if vb6Format.Length > 1 then vb6Format.ToUpperInvariant() else vb6Format with
        // PREDEFINED NUMERIC: http://msdn.microsoft.com/en-us/library/y006s0cz(v=vs.71).aspx
        | "GENERAL NUMBER" | "G"       -> "{0:G}"
        | "g"                          -> "{0:g}"
        | "CURRENCY" | "C" | "c"       -> "{0:C}"
        | "FIXED" | "F"                -> "{0:F}"
        | "f"                          -> "{0:f}"
        | "STANDARD" | "N" | "n"       -> "{0:N}"
        | "PERCENT" | "P" | "p"        -> "{0:P}"
        | "SCIENTIFIC"                 -> "{0:E2}"
        | "E" | "e"                    -> "{0:E6}"
        | "D" | "d"                    -> "{0:D}"
        | "X" | "x"                    -> "{0:X}"
        | "YES/NO" | "ON/OFF"          // we can't support these
        | "TRUE/FALSE"                 -> "{0}"
        // PREDEFINED DATE/TIME: http://msdn.microsoft.com/en-us/library/362btx8f(v=VS.71).aspx
        | "GENERAL DATE" | "G"         -> "{0:G}"
        | "LONG DATE" | "D"            -> "{0:D}"
        | "MEDIUM DATE"
        | "SHORT DATE" | "d"           -> "{0:d}"
        | "LONG TIME" | "T"            -> "{0:T}"
        | "MEDIUM TIME"
        | "SHORT TIME" | "t"           -> "{0:t}"
        | "M" | "m"                    -> "{0:M}"
        | "R" | "r"                    -> "{0:R}"
        | "s"                          -> "{0:s}"
        | "u"                          -> "{0:u}"
        | "U"                          -> "{0:U}"
        | "Y" | "y"                    -> "{0:Y}"
        // USER-DEFINED: http://msdn.microsoft.com/en-us/library/4fb56f4y(v=vs.71).aspx
        //               http://msdn.microsoft.com/en-us/library/73ctwf33(v=VS.71).aspx
        // The user-defined format strings translate more-or-less exactly, so we're just going to use them.
        | _                            -> sprintf "{0:%s}" vb6Format

Here's some F# code that translates the majority of pre-defined and custom VB6-style numeric and date format strings to something suitable for String.Format. It's easily called from C# or VB of course.

open System

module VB6Format =

    /// Converts a VB6-style format string to something suitable for String.Format()
    let Convert(vb6Format) =
        if String.IsNullOrWhiteSpace(vb6Format) then "{0}" else
        match if vb6Format.Length > 1 then vb6Format.ToUpperInvariant() else vb6Format with
        // PREDEFINED NUMERIC: http://msdn.microsoft.com/en-us/library/y006s0cz(v=vs.71).aspx
        | "GENERAL NUMBER" | "G"       -> "{0:G}"
        | "g"                          -> "{0:g}"
        | "CURRENCY" | "C" | "c"       -> "{0:C}"
        | "FIXED" | "F"                -> "{0:F}"
        | "f"                          -> "{0:f}"
        | "STANDARD" | "N" | "n"       -> "{0:N}"
        | "PERCENT" | "P" | "p"        -> "{0:P}"
        | "SCIENTIFIC"                 -> "{0:E2}"
        | "E" | "e"                    -> "{0:E6}"
        | "D" | "d"                    -> "{0:D}"
        | "X" | "x"                    -> "{0:X}"
        | "YES/NO" | "ON/OFF"          // we can't support these
        | "TRUE/FALSE"                 -> "{0}"
        // PREDEFINED DATE/TIME: http://msdn.microsoft.com/en-us/library/362btx8f(v=VS.71).aspx
        | "GENERAL DATE" | "G"         -> "{0:G}"
        | "LONG DATE" | "D"            -> "{0:D}"
        | "MEDIUM DATE"
        | "SHORT DATE" | "d"           -> "{0:d}"
        | "LONG TIME" | "T"            -> "{0:T}"
        | "MEDIUM TIME"
        | "SHORT TIME" | "t"           -> "{0:t}"
        | "M" | "m"                    -> "{0:M}"
        | "R" | "r"                    -> "{0:R}"
        | "s"                          -> "{0:s}"
        | "u"                          -> "{0:u}"
        | "U"                          -> "{0:U}"
        | "Y" | "y"                    -> "{0:Y}"
        // USER-DEFINED: http://msdn.microsoft.com/en-us/library/4fb56f4y(v=vs.71).aspx
        //               http://msdn.microsoft.com/en-us/library/73ctwf33(v=VS.71).aspx
        // The user-defined format strings translate more-or-less exactly, so we're just going to use them.
        | _                            -> sprintf "{0:%s}" vb6Format
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文