gpt4 book ai didi

vb.net - 如何在 VB.net(使用结构类型)中从 VB6 重写 "LSet"?

转载 作者:行者123 更新时间:2023-12-01 23:22:18 25 4
gpt4 key购买 nike

我有一个 VB6 应用程序,它使用 LSet() 和两种用户定义的数据类型 (Type),将数据从一种类型分配给另一种类型,例如:

Lset type1 = Type2

现在我必须在 VB.net 中应用等效逻辑。但是,在 VB.net 中,LSet cannot be used against different types (在 VB6 中键入)。

如何在 VB.net 中实现 VB6 LSet 逻辑?

示例/典型代码:

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

感谢您的所有帮助。

最佳答案

你不应该在 VB.NET 中这样做。
还有其他方法,比如定义从一种类型到另一种类型的转换运算符,然后在代码中使用它们:

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

但是,如果您非常确定应该执行按字节复制,并且您知道 alignment issues并且对他们感到满意,那么您可以这样做:

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

关于vb.net - 如何在 VB.net(使用结构类型)中从 VB6 重写 "LSet"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8677073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com