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 sCOMMENTSAsString

EndStructure

Structure STATUS_MSG

<VBFixedString(gnSTATUS_MSG_LEN), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=gnSTATUS_MSG_LEN)>Public sMsgAsString

EndStructure

[3854 byte] By [Krutika] at [2008-1-12]
# 1
LSet is available in the Microsoft.VisualBasic namespace.Adding Imports Microsoft.VisualBasic to the top of your code file will allow you to use it.
KenTucker at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 2

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?

Krutika at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 3
Sorry that is not possible. How is vb to know how to convert a structure with 1 member to a structure with more than one member?
KenTucker at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 4

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_TYPE

With msg

.nSYSTEM_ID = "A1"

.sDATE_SENT = "08/01/06"

.sTIME_SENT = ":00:00"

' ...

End With

Dim stat As New STATUS_MSG

stat.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 = 90

Structure 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 String

Public Overrides Function ToString() As String

Return 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 Function

Public Function Length() As Integer

Return Me.ToString.Length

End Function

End Structure

Structure STATUS_MSG

<VBFixedString(gnSTATUS_MSG_LEN), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=gnSTATUS_MSG_LEN)> _

Public sMsg As String

Public Overrides Function ToString() As String

Return Me.sMsg

End Function

Public Function Length() As Integer

Return Me.ToString.Length

End Function

End Structure

I hope this does the trick for you. Let me know how it goes.

Best,

Paul

PaulYuk_MS at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 5

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.

Krutika at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...