【问题标题】:Converting Fixed length statement from VB6 to VB.Net将固定长度语句从 VB6 转换为 VB.Net
【发布时间】:2012-08-03 07:35:54
【问题描述】:

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

样本数据包数据为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

输出:由于四个是8个长度的定长字符串,剩下的4个字符应该用null填充,如下所示

11 FS 123 FS testing FS 5678XXXX

【问题讨论】:

  • 这不是 VB6 语句。你是从什么转换而来的,最终结果应该是什么?
  • 能否出示原码?
  • @MarkBertenshaw :放置示例代码和预期结果...
  • 查看String.Pad* 方法,可能与.Length.SubString() 结合使用。
  • @Deanna yes 会尝试实施。看起来它符合我的要求

标签: vb.net string vb6 vb6-migration


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    忘记固定长度,使用常规的 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("*") 
    

    所以我使用了“*”,但你可以使用空格,或者任何你想要的。

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多