如何在vb中编写此代码
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public static implicit operator Point(POINT point)
{
return new Point(point.X, point.Y);
}
}
我已经尝试过了,但它没有用
<StructLayout(LayoutKind.Sequential)> _
Public Structure POINT
Public X As Integer
Public Y As Integer
Public Shared Widening Operator CType(point As POINT) As Point
Return New Point(point.X, point.Y)
End Operator
End Structure
我得到了这两个错误:
Error 1 Conversion operators cannot convert from a type to the same type.
Error 2 Type 'MousePosition.Form1.POINT' has no constructors.
是否有可能在vb中继承自身的结构?
答案 0 :(得分:4)
请注意,在VB中,您没有区分大小写的代码。
所以POINT和Point是相同的类型。
例如,您必须将结构重命名为PointStruct。
答案 1 :(得分:2)
您的代码基本上是正确的,但您遇到了命名空间冲突。该结构称为POINT
,但System.Drawing
命名空间中已存在同名的类型。在C#中,类型名称区分大小写,因此没有冲突,但在VB.NET中,类型不区分大小写,因此它不知道您的意思。最简单的方法是将类重命名为其他类似的东西,如:
<StructLayout(LayoutKind.Sequential)>
Public Structure ApiPoint
Public X As Integer
Public Y As Integer
Public Shared Widening Operator CType(point As ApiPoint) As Point
Return New Point(point.X, point.Y)
End Operator
End Structure
但是,如果您真的想要,可以使用该名称。每次需要区分两者时,您只需要明确指定完整的命名空间,例如:
<StructLayout(LayoutKind.Sequential)>
Public Structure POINT
Public X As Integer
Public Y As Integer
Public Shared Widening Operator CType(point As POINT) As System.Drawing.Point
Return New System.Drawing.Point(point.X, point.Y)
End Operator
End Structure