Option
Explicit
Private
Const
E_CANNOT_PARSE
As
Long
= &HA
Private
Const
E_INVALID
As
Long
= &HC
Private
m_strFirstName
As
String
Private
m_strSecondName
As
String
Private
m_strLastName
As
String
Public
Property
Get
FullName()
As
String
FullName =
Me
.FirstName
If
Me
.HasSecondName
Then
FullName = FullName &
" "
&
Me
.SecondName
FullName = FullName &
" "
&
Me
.LastName
End
Property
Public
Property
Let
FirstName(RHS
As
String
)
m_strFirstName = RHS
End
Property
Public
Property
Get
FirstName()
As
String
FirstName = m_strFirstName
End
Property
Public
Property
Let
SecondName(RHS
As
String
)
m_strSecondName = RHS
End
Property
Public
Property
Get
SecondName()
As
String
SecondName = m_strSecondName
End
Property
Public
Property
Let
LastName(RHS
As
String
)
m_strLastName = RHS
End
Property
Public
Property
Get
LastName()
As
String
LastName = m_strLastName
End
Property
Public
Property
Get
HasSecondName()
As
Boolean
HasSecondName =
Not
(
Me
.SecondName = vbNullString)
End
Property
Public
Sub
Parse(Expression
As
Variant
)
Dim
vntParts
As
Variant
vntParts = Expression
Do
While
InStr(vntParts,
" "
)
vntParts = Replace$(vntParts,
" "
,
" "
)
Loop
vntParts = Split(Trim$(vntParts),
" "
)
Select
Case
UBound(vntParts)
Case
1
Me
.FirstName = vntParts(0)
Me
.SecondName = vbNullString
Me
.LastName = vntParts(1)
Case
2
Me
.FirstName = vntParts(0)
Me
.SecondName = vntParts(1)
Me
.LastName = vntParts(2)
Case
Else
Call
Err.Raise(vbObjectError + E_CANNOT_PARSE,
"Person"
,
"Cannot parse expression: '"
& Expression &
"'"
)
End
Select
If
Len(
Me
.FirstName) = 0
Or
Len(
Me
.LastName) = 0
Or
(
Me
.HasSecondName
And
Len(
Me
.SecondName) = 0)
Then
Call
Err.Raise(vbObjectError + E_INVALID,
"Person"
,
"The person's name is invalid."
)
End
If
End
Sub
Public
Function
Compare( _
Other
As
Person, _
Optional
IncludeSecondName
As
Boolean
=
False
, _
Optional
CompareMethod
As
VbCompareMethod = VbCompareMethod.vbTextCompare _
)
As
Integer
Compare = StrComp(
Me
.LastName, Other.LastName, CompareMethod)
If
Compare <> 0
Then
Exit
Function
End
If
Compare = StrComp(
Me
.FirstName, Other.FirstName, CompareMethod)
If
Compare <> 0
Then
Exit
Function
End
If
If
IncludeSecondName =
False
Then
Exit
Function
End
If
Compare = (StrComp(
Me
.SecondName, Other.SecondName, CompareMethod) = 0)
End
Function
Public
Function
Equals(Other
As
Object
)
As
Boolean
If
Not
TypeOf
Other
Is
Person
Then
Exit
Function
End
If
Equals = (
Me
.Compare(Other) = 0)
End
Function