カスタムタイプの固定長文字列を使用する一部のVB6コードをUpgradeWizardを使用してVB.NETにアップグレードしましたが、誰かが助けてくれることを期待していたLSetメソッドの使用に問題があります。
既存のVB6コード(型宣言);
Public Type MyType
PROP1 As String * 15
PROP2 As String * 25
End Type
Public Type MyTypeBuffer
Buffer As String * 40
End Type
使用例;
LSet instOfMyTypeBuffer.Buffer = ...
LSet instOfMyType = instOfMyTypeBuffer
これを.NETにアップグレードする適切な方法は何でしょうか?
UpgradeWizardを使用すると、次のようになります。
<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から取得されます。
アップグレードウィザードが失敗するのは、LSetの場合です。それは以下を生み出しました。
instOfMyTypeBuffer.Buffer = LSet(...)
instOfMyType = LSet(instOfMyTypeBuffer)
これはコンパイルに失敗し、これらのエラーを引き起こします。
タイプ「String」の値を「Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString」に変換できません
'Public Function LSet(Source As String、Length As Integer)AsString'のパラメーター'Length'に引数が指定されていません
タイプ「MyTypeBuffer」の値を「文字列」に変換できません
したがって、ToString()を使用してその方法の一部を取得できますが、それでもLSetメソッド呼び出し自体の問題があります。元の機能を再現するにはどうすればよいですか?アップグレードウィザードは私に完全に不適切な変換を与えましたか、それとも使用可能なものに回収できますか?