LSet equivalent in .NET
I'm upgrading an application from VB 6 to VB.NET.
Is there any equivalent for LSet in .NET for UDTs?
Else any work arounds?
Here's the code:
LSet Status = StatusMsg
Status is of Type Status_Msg
StatusMsg is of Type Status_Msg_Type
Structure
STATUS_MSG_TYPE<VBFixedString(2), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=2)>
Public nSYSTEM_IDAsString<VBFixedString(8), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=8)>
Public sDATE_SENTAsString<VBFixedString(6), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=6)>
Public sTIME_SENTAsString<VBFixedString(20), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=20)>
Public sMERCHANT_NUMBERAsString<VBFixedString(15), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=15)>
Public sTERMINAL_SERIAL_NUMBERAsString<VBFixedString(6), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=6)>
Public sTERMINAL_ID_NUMBERAsString<VBFixedString(3), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=3)>
Public nSTATUS_CODEAsString<VBFixedString(30), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=30)>
Public sCOMMENTSAsStringEndStructureStructure STATUS_MSG<VBFixedString(gnSTATUS_MSG_LEN), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=gnSTATUS_MSG_LEN)>
Public sMsgAsStringEndStructure
[3854 byte] By [
Krutika] at [2008-1-12]
I have added the dll. And I'm trying to use LSet in this way:
LSet Status = StatusMsg was replaced with the below statement
Status.Equals(LSet(StatusMsg.ToString, Len(StatusMsg)))
How do I convert one UDT to another UDT using LSet?
i.e. Converting StatusMsg type to Status Type?
Hi-
I think I have a workaround to do the conversion you want.
Per Ken the LSet function expects to input a string and return a string. The missing link is to define a custom (overrides) ToString function for your structs. It's also nice to define a Length function. You can of course tweak the ToString function to format and align the string exactly the way you want -- I simply concat all members of the UDT.
Once you've defined these functions you can use LSET on the ToString representations:
Dim msg As New STATUS_MSG_TYPEWith msg.nSYSTEM_ID =
"A1".sDATE_SENT =
"08/01/06".sTIME_SENT =
":00:00"' ...End WithDim stat As New STATUS_MSGstat.sMsg = LSet(msg.ToString, msg.Length)
Or, you in this case you can avoid Lset and just assign the ToString value to stat.sMsg:
stat.sMsg = msg.ToString
Adding these ToString functions looks like this:
Const gnSTATUS_MSG_LEN As Integer = 90Structure STATUS_MSG_TYPE<VBFixedString(2), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=2)> _
Public nSYSTEM_ID As String<VBFixedString(8), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=8)> _
Public sDATE_SENT As String<VBFixedString(6), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=6)> _
Public sTIME_SENT As String<VBFixedString(20), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=20)> _
Public sMERCHANT_NUMBER As String<VBFixedString(15), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=15)> _
Public sTERMINAL_SERIAL_NUMBER As String<VBFixedString(6), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=6)> _
Public sTERMINAL_ID_NUMBER As String<VBFixedString(3), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=3)> _
Public nSTATUS_CODE As String<VBFixedString(30), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=30)> _
Public sCOMMENTS As StringPublic Overrides Function ToString() As StringReturn String.Format("{0}{1}{2}{3}{4}{5}{6}{7}", Me.nSYSTEM_ID, Me.sDATE_SENT, Me.sTIME_SENT, Me.sMERCHANT_NUMBER, Me.sTERMINAL_SERIAL_NUMBER, Me.sTERMINAL_ID_NUMBER, Me.nSTATUS_CODE, Me.sDATE_SENT)End FunctionPublic Function Length() As IntegerReturn Me.ToString.LengthEnd FunctionEnd StructureStructure STATUS_MSG<VBFixedString(gnSTATUS_MSG_LEN), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=gnSTATUS_MSG_LEN)> _
Public sMsg As StringPublic Overrides Function ToString() As StringReturn Me.sMsgEnd FunctionPublic Function Length() As IntegerReturn Me.ToString.LengthEnd FunctionEnd Structure
I hope this does the trick for you. Let me know how it goes.
Best,
Paul
Thank you Paul. This might work.
There are many such conversions to be made and data passed and the number of parameter passed varies. Hence right now I'm manually concatenating all the strings.