将固定长度语句从VB6转换为VB.Net

时间:2012-08-06 10:25:29

标签: vb.net string vb6 vb6-migration

我们执行基于协议的数据发送到设备需要格式化数据包的设备。

样本包数据为XXFSXXXFSXXXXXXXFSXXXXXX。提到的X是每个字符串的最大长度。如果数据小于字符串最大长度,则应填充NULL字符,如..11FS123FS1234XXXX(剩余的X将填充NULL)。

我只是想将VB6函数中的一个转换为VB.Net,以下是转换后的语句我面临的问题

Option Strict Off
Option Explicit On
Imports Microsoft.VisualBasic.Compatibility.VB6
Imports System
Imports System.Runtime.InteropServices
Module FunctionCmd_Msg

    Public FunCommand_Msg As Fun_CommandMessage = Fun_CommandMessage.CreateInstance()
    'Function Command Message
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _ _
    Public Structure Fun_CommandMessage
        <VBFixedString(1)> Public one As String
        <VBFixedString(1)> Public two As String
        <VBFixedString(3)> Public three As String
        Dim five As String
        <VBFixedString(8)> Public four As String
        Public Shared Function CreateInstance() As Fun_CommandMessage
            Dim result As New Fun_CommandMessage
            result.one = String.Empty
            result.two = String.Empty
            result.three = String.Empty
            result.four = String.Empty
            result.five = String.Empty
        End Function
    End Structure
End Module

假设:

one = "1"
two = "1"
three = "123"
four = "5678"           
five = "testing"
FS = character (field separator)

连接字符串我需要一个固定长度的字符串,如下所示:

one & two & FS & three & FS & five & FS & four

输出:因为4是一个8长度的固定长度字符串,剩下的4个字符应填充null,如下所示

11 FS 123 FS testing FS 5678XXXX

2 个答案:

答案 0 :(得分:6)

固定长度的字符串在.NET中根本没有意义。 Microsoft试图提供类似的类以便于升级,但事实是您应该根据使用情况更改代码:

固定长度字符串在VB6代码中做了什么?这是没有充分理由的吗?然后在.NET中使用普通的String

是否与C API互操作?然后使用编组来为C API调用中的数组设置大小。

答案 1 :(得分:0)

只需忘记固定长度,并使用常规的vb.net字符串即可。他们会很好地调用该代码,包括互操作。

所以,只要扎紧绳子,就可以参加比赛了。

实际上,建立了一个Msg类,可以为您完成肮脏的工作。

这对我来说很好:

请注意,我如何设置此设置,您只能在一个位置定义字符串的长度。 (所以我使用len(m_string)确定代码中从THEN开始的长度。

另外,对于调试和本示例,我使用X进行测试以代替vbcharNull(应使用)。

现在,在您的代码中?

只需使用此:

    Dim Msg As New MyMsg

    With Msg
        .one = "A"
        .two = "B"
        .three = "C"
        .four = "D"
        .Five = "E"
    End With

    Debug.WriteLine(Msg.Msg("*") & vbCrLf)

    Debug.WriteLine("total lenght = " & Len(Msg.Msg("X").ToString))

输出:

A*B*CXX*EXXXXXXX*DXXXXXXX

total lenght = 25

我在您的代码中指出,您在四点之前有五个-但这就是您想要的,那么没问题

请注意,该类始终为您维护长度。

因此,只需将此代码粘贴到您的模块中,甚至粘贴到一个新的单独的类中。

Public Class MyMsg
    'Dim cPad As Char = vbNullChar
    Dim cPad As Char = "X"

    Private m_one As String = New String(cPad, 1)
    Private m_two As String = New String(cPad, 1)
    Private m_three As String = New String(cPad, 3)
    Private m_four As String = New String(cPad, 8)
    Private m_five As String = New String(cPad, 8)

    Public Property one As String
        Get
            Return m_one
        End Get
        Set(value As String)
            m_one = MyPad(value, m_one)
        End Set
    End Property
    Public Property two As String
        Get
            Return m_two
        End Get
        Set(value As String)
            m_two = MyPad(value, m_two)
        End Set
    End Property

    Public Property three As String
        Get
            Return m_three
        End Get
        Set(value As String)
            m_three = MyPad(value, m_three)
        End Set
    End Property

    Public Property four As String
        Get
            Return m_four
        End Get
        Set(value As String)
            m_four = MyPad(value, m_four)
        End Set
    End Property

    Public Property Five As String
        Get
            Return m_five
        End Get
        Set(value As String)
            m_five = MyPad(value, m_five)
        End Set
    End Property


    Public Function Msg(FS As String) As String
        Return m_one & FS & m_two & FS & m_three & FS & m_five & FS & m_four
    End Function

    Private Function MyPad(str As String, strV As String) As String
        Return Strings.Left(str & New String(Me.cPad, Len(strV)), Len(strV))
    End Function

End Class

如前所述,将char的注释掉的“ X”行改回vbCharNull。

当然,您仍然可以选择定界符。我用过

Msg.Msg("*") 

所以我用了“ *”,但是您可以使用空格或任何您想要的东西。