pointclass.vb

来自「wrox出版社的另一套经典的VB2005数据库编程学习书籍,收集了书中源码,郑重」· VB 代码 · 共 95 行

VB
95
字号
Imports System
Imports System.Data.Sql
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server

'Added
Imports System.Runtime.InteropServices 'required for StructLayout()
Imports System.Math	'required for Sqrt

<Serializable(), SqlUserDefinedType(Format.Native), StructLayout(LayoutKind.Sequential)> _
Public Class PointClass
	Implements INullable

	Private blnIsNull As Boolean
	'Coordinates
	Private dblX As Double
	Private dblY As Double

	Public ReadOnly Property IsNull() As Boolean _
	  Implements INullable.IsNull
		'Required
		Get
			Return blnIsNull
		End Get
	End Property

	Public Shared ReadOnly Property Null() As PointClass
		'Required
		Get
			Dim ptNew As New PointClass
			ptNew.blnIsNull = True
			Return ptNew
		End Get
	End Property

	Public Sub New()
		'Default constructor required for class
        Me.blnIsNull = False
		Me.X = 0
		Me.Y = 0
	End Sub

	Public Overrides Function ToString() As String
		'Return the comma-separated values
		If Me.IsNull Then
			Return "Null"
		Else
			Return Me.dblX.ToString + "," + Me.dblY.ToString
		End If
	End Function

	Public Shared Function Parse(ByVal sqlString As SqlString) As PointClass
		'Creates a Point by splitting an input string at the separator character
		If sqlString.IsNull Then
			Return Nothing
		Else
			Dim ptNew As New PointClass
			Dim strNew As String = sqlString.ToString
			Dim astrXY() As String = strNew.Split(","c)
			ptNew.X = CType(astrXY(0), Double)
			ptNew.Y = CType(astrXY(1), Double)
			Return ptNew
		End If
	End Function

    Public Property X() As Double
        'X-coordinate
        Get
            Return Me.dblX
        End Get
        Set(ByVal Value As Double)
            Me.dblX = Value
            blnIsNull = False
        End Set
    End Property

	Public Property Y() As Double
		'Y-coordinate
		Get
			Return Me.dblY
		End Get
		Set(ByVal Value As Double)
			Me.dblY = Value
			blnIsNull = False
		End Set
	End Property

	Public Function DistanceTo(ByVal ptTest As PointClass) As Double
		'Optional function: Calculate the distance from Me to ptTest
		Return Sqrt((Me.X - ptTest.X) * (Me.X - ptTest.X) + _
		  (Me.Y - ptTest.Y) * (Me.Y - ptTest.Y))
	End Function
End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?