2012-08-06 96 views
0
转换固定长度声明

我只是想转换VB6功能VB.Net之一,下面就是我面临的问题从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的固定长度字符串应采用空值填充,如下

11 FS 123 FS testing FS 5678XXXX 
+2

这不是VB6语句。你从什么转换而来,最终的结果是什么? – Deanna 2012-08-06 11:46:14

+0

你能显示原始代码吗? – 2012-08-06 14:37:35

+0

@MarkBertenshaw:放置一个示例代码和预期的结果... – Ragav 2012-08-09 18:10:20

回答

5

固定长度字符串简单地使在.NET没有意义了。微软试图提供一个类似的类,以方便升级,但事实是,你应该根据使用情况更改代码:

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

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

+0

对于迟到的回复感到抱歉..我们使用interop ...在这里,我们执行基于协议的数据发送到设备。其中设备需要格式化的数据包来理解...示例数据包数据是XXFSXXXFSXXXXXXXFSXXXXXX ...所提到的X是每个字符串的最大长度大小..如果数据小于字符串最大长度,它应该填充NULL字符..sayin像.11FS123FS1234XXXX(其余X将被填充NULL) – Ragav 2012-08-09 17:39:04