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.
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
What would be an appropriate way to upgrade this to .NET?
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 is coming from the namespace Microsoft.VisualBasic.Compatibility.VB6.
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;
Value of type 'String' cannot be converted to 'Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString'
Argument not specified for parameter 'Length' of 'Public Function LSet(Source As String, Length As Integer) As String'
Value of type 'MyTypeBuffer' cannot be converted to 'String'
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?