转换VB6自定义类型(固定长度的字符串)到VB .NET [英] Converting VB6 Custom Type (with Fixed Length Strings) to VB .NET

查看:1792
本文介绍了转换VB6自定义类型(固定长度的字符串)到VB .NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经升级了一些VB6 code,它采用固定长度的字符串中的自定义类型,以VB .NET使用UpgradeWizard时遇到与使用LSET方法,我希望有人能帮助我走出困境与

I have upgraded some VB6 code, which uses fixed length strings in custom types, to VB .NET by using the UpgradeWizard and am having trouble with the use of the LSet method that I was hoping someone could help me out with.

在现有VB6 code(类型声明);

The Existing VB6 code (type declarations);

Public Type MyType
    PROP1       As String * 15
    PROP2       As String * 25
End Type

Public Type MyTypeBuffer
    Buffer As String * 40
End Type

用法示例;

Example usage;

LSet instOfMyTypeBuffer.Buffer = ...
LSet instOfMyType = instOfMyTypeBuffer

什么是适当的方式该升级到.NET?

What would be an appropriate way to upgrade this to .NET?

使用UpgradeWizard,我得到以下;

Using the UpgradeWizard, I get the following;

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
 _
Public Structure MyType
    <VBFixedString(15),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,SizeConst:=15)> _
    Dim PROP1 As FixedLengthString

    <VBFixedString(25),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,SizeConst:=25)> _
    Dim PROP2 As FixedLengthString

    Public Shared Function CreateInstance() As MyType
        Dim result As New MyType
        result.PROP1 = New FixedLengthString(15)
        result.PROP2 = New FixedLengthString(25)
        Return result
    End Function
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
 _
Public Structure MyTypeBuffer
    <VBFixedString(CLVHDR_REC_LENGTH),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr,SizeConst:=40)> _
    Dim Buffer As FixedLengthString
    Public Shared Function CreateInstance() As MyTypeBuffer
        Dim result As New MyTypeBuffer
        result.Buffer = New FixedLengthString(40)
        Return result
    End Function
End Structure

FixedLengthString从命名空间中Microsoft.VisualBasic.Compatibility.VB6来了。

FixedLengthString is coming from the namespace Microsoft.VisualBasic.Compatibility.VB6.

在哪里升级向导失败是当它涉及到LSET。它产生了以下;

Where the Upgrade Wizard fails is when it comes to LSet. It produced the following;

instOfMyTypeBuffer.Buffer = LSet(...)
instOfMyType = LSet(instOfMyTypeBuffer)

这无法编译,让这些错误;

Which fails to compile, giving these errors;

键入字符串的值不能为   转换到   Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString

Value of type 'String' cannot be converted to 'Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString'

参数没有为参数指定   对公共职能长度   LSET(来源作为字符串,长度   整数)作为字符串

Argument not specified for parameter 'Length' of 'Public Function LSet(Source As String, Length As Integer) As String'

型MyTypeBuffer的值不能为   转换为字符串

Value of type 'MyTypeBuffer' cannot be converted to 'String'

所以,我可以使用toString()方法来获取的方式出现的一部分,但仍有LSET方法调用自身的问题。我应该怎么做来重建原始的功能?已升级向导给了我一个完全不恰当的转换,或者是挽救成可用的东西?

So, I can use ToString() to get part of the way there, but there is still the issue of the LSet method call itself. What should I do to recreate the original functionality? Has the Upgrade Wizard given me a totally inappropriate conversion, or is it salvageable into something usable?

推荐答案

LSET是一个相当奇特的声明在VB6:看的说明在手动

LSet is a rather quirky statement in VB6: see description in the manual.

  • 在当前对字符串使用时,左对齐字符串中的原始字符串和替换任何剩余的字符空格。
  • 当在用户定义类型中使用的,它只是复制来自另一个用户定义类型,即使他们有不同的定义的存储器。这是不推荐的。

它被使用在code,你有一个特别古怪的方式。

It's being used in a particularly quirky way in the code you have.

  1. LSET instOfMyTypeBuffer.Buffer = ...
    这无论是在VB6和迁移的Vb.Net冗余。当你分配一个新值固定长度的字符串,它总是垫用空格呢!
    因此,只要改变这个(无论是VB6或VB.Net)
    instOfMyTypeBuffer.Buffer = ...
  2. LSET instOfMyType = instOfMyTypeBuffer
    更有意思的。这将文件从一种类型的实例存储为另一种类型的实例,不检查。 咕嘟咕嘟!
    综观类型的定义,我认为这只是把从 instOfMyBuffer 第15个字符为 instOfMyType.PROP1 和剩下的25个字符为 instOfMyType.PROP2
    我偶尔看到了这个用作从文件中读取处理固定长度的字符串记录丑陋的方式。例如第一15个字符可能是一个人的名字,并在接下来的25姓氏。
    你可以只用这个code(在任一VB6或VB.Net)取代。
    instOfMyType.PROP1 =左(instOfMyBuffer.Buffer,15)
    instOfMyType.PROP2 = MID(instOfMyBuffer.Buffer,16)
  1. LSet instOfMyTypeBuffer.Buffer = ...
    This is redundant both in the VB6 and the migrated Vb.Net. When you assign a new value to a fixed-length string, it always pads out with spaces anyway!
    So just change to this (in either VB6 or VB.Net)
    instOfMyTypeBuffer.Buffer = ...
  2. LSet instOfMyType = instOfMyTypeBuffer
    More interesting. This copies the memory from an instance of one type into an instance of another type, with no checks. Gulp!
    Looking at the definitions of the types, I think this simply puts the first 15 characters from instOfMyBuffer into instOfMyType.PROP1 and the remaining 25 characters into instOfMyType.PROP2.
    I have occasionally seen this used as an ugly way of processing fixed-length string records read from a file. For example the first fifteen characters might be a person's first name, and the next 25 the last name.
    You could just replace with this code (in either VB6 or VB.Net).
    instOfMyType.PROP1 = Left(instOfMyBuffer.Buffer, 15)
    instOfMyType.PROP2 = Mid(instOfMyBuffer.Buffer, 16)

汉斯建议开沟固定长度的字符串。如果这很容易 - 这取决于你的code基的其余部分,它的也许的容易,或者它的可能的硬 - 这是个好建议。

Hans suggested ditching the fixed-length strings. If that's easy - and it depends on the rest of your code base, it might be easy, or it might be hard - it's good advice.

这篇关于转换VB6自定义类型(固定长度的字符串)到VB .NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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