将固定长度语句从 VB6 转换为 VB.Net [英] Converting Fixed length statement from VB6 to VB.Net

查看:22
本文介绍了将固定长度语句从 VB6 转换为 VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We perform a protocol based data sending to device where the device requires a formatted data packets.

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

the sample packet data is XXFSXXXFSXXXXXXXFSXXXXXX. The X mentioned is the max length size of each string. if data is less than string max length it should be filled with NULL character like ..11FS123FS1234XXXX (the remaining X will be filled with NULL).

我只是想将 VB6 函数之一转换为 VB.Net,下面是我遇到问题的转换语句

I am just trying to convert one of VB6 function to VB.Net and below is the converted statement where i am facing issue

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)

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

on concatenating the strings i need a fixed length string such like this:

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

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

output: since four is a fixed length string of 8 length remaining 4 characters should be filled with null as below

11 FS 123 FS testing FS 5678XXXX

推荐答案

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

Fixed-length strings simply make no sense in .NET any more. Microsoft tried to provide a similar class for easier upgrade but the truth is that you should change your code depending on usage:

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

What did the fixed-length string do in your VB6 code? Was it for no good reason? Then use a normal String in .NET.

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

Was it for interop with a C API? Then use marshalling to set a size for an array in the C API call.

这篇关于将固定长度语句从 VB6 转换为 VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆