3

I have an VB6 application which uses LSet() with two user defined data types (Type), to assign data from one type to another, e.g.:

Lset type1 = Type2

Now I have to apply equivalent logic in VB.net. However, in VB.net, LSet cannot be used against different types (Types in VB6).

How can I implement the VB6 LSet logic in VB.net?

Sample / typical code:

Public MQ_Policy As typPolicyDataRetrieval 
Public typPolicyDataBlock As gtypPolicyDataBlock 

With MQ_Policy.Input 
   .PcPolicyIDNum = Mid(InputString, 1, 8) 
   .PcPolicyIDNumPrefix = " " 
   .Datalength = Format$(CLng(Len(MQ_Policy)) - (Len(MQ_Policy.MQHeader) + Len(MQ_Policy.Input.Datalength)), String$(Len(.Datalength), "0")) 
End With 

LSet typPolicyDataBlock = MQ_Policy

Appreciate all your help.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Rajdeep
  • 485
  • 4
  • 15
  • 28
  • Please do accpt my appology.But the problem is that I cannot bring the code and paste it here, neither is their any network connecting this machine to that of the development machine nor can I use any carriage media. Is their any other way I can make it clear ? i sincerely appolozise. – Rajdeep Dec 30 '11 at 07:23
  • @Rajdeep Then post a small working (but made up) example. –  Dec 30 '11 at 07:24
  • (I do not even know what LSet in VB6 is supposed to have done :-) –  Dec 30 '11 at 07:31
  • Public MQ_Policy As typPolicyDataRetrieval Public typPolicyDataBlock As gtypPolicyDataBlock ****************************** CODE ********************************************** With MQ_Policy.Input .PcPolicyIDNum = Mid(InputString, 1, 8) .PcPolicyIDNumPrefix = " " .Datalength = Format$(CLng(Len(MQ_Policy)) - (Len(MQ_Policy.MQHeader) + Len(MQ_Policy.Input.Datalength)), String$(Len(.Datalength), "0")) End With LSet typPolicyDataBlock = MQ_Policy= – Rajdeep Dec 30 '11 at 07:47
  • possible duplicate of [Converting VB6 Custom Type (with Fixed Length Strings) to VB .NET](http://stackoverflow.com/questions/3864422/converting-vb6-custom-type-with-fixed-length-strings-to-vb-net) – MarkJ Dec 31 '11 at 14:24

2 Answers2

1

See a better answer here: Converting VB6 Custom Type (with Fixed Length Strings) to VB .NET

In short LSet, when used with types, copies memory from one place to another! So it should NOT be used under any circumstances. The original code is not good so converting it 'as is' will be a bad idea.

That said you should be able to replace it with either a Cast if the objects are compatible or write a converter function to change the type manually.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • Situation sounds the same as in the question you have linked. The accepted answer applies (written by, ahem, me) – MarkJ Dec 31 '11 at 14:23
1

You shouldn't be doing that in VB.NET.
There are other ways, such as defining conversion operators from one type to another and then using them in the code:

Private Structure t1
    Public a As Short
    Public b As Short
End Structure

Private Structure t2
    Public v As Integer

    Public Shared Widening Operator CType(ByVal v As t1) As t2
        Return New t2 With {.v = v.a Or (CType(v.b, Integer) << 16)}
    End Operator
End Structure

Sub Main()
    Dim v1 As t1, v2 As t2

    v2 = v1 ' Works!
End Sub

However, if you are damn sure you should perform a bytewise copy, and you are aware of the alignment issues and are happy with them, then you can do so:

Imports System.Runtime.InteropServices

Public Function CopyStructure(ByVal s As Object, ByVal ResultType As System.Type) As Object
    Dim h As GCHandle

    Try
        h = GCHandle.Alloc(s, GCHandleType.Pinned)

        Return Marshal.PtrToStructure(h.AddrOfPinnedObject, ResultType)
    Finally
        If h.IsAllocated Then h.Free()
    End Try
End Function

Sub Main()
    Dim v1 As t1, v2 As t2

    v2 = DirectCast(CopyStructure(v1, GetType(t2)), t2)
End Sub
GSerg
  • 76,472
  • 17
  • 159
  • 346