Converting VB6 Custom Type (with Fixed Length Strings) to VB .NET

前端 未结 1 1287
醉梦人生
醉梦人生 2021-01-16 10:36

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 t

相关标签:
1条回答
  • 2021-01-16 11:36

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

    • When used on strings, it left-aligns the string in the original string and replaces any leftover characters with spaces.
    • When used on user-defined types, it just copies the memory from one user-defined type over the other, even if they had different definitions. This is not recommended.

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

    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)

    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.

    0 讨论(0)
提交回复
热议问题