久しぶりの本連載。画面はちょっと変更しました。
簡単なソフトウェアを作ってみよう(3) posted by
(C)Satohru
データ表示部をListBoxからDataGridViewに変更し、データにユニークキーを持たせました。
このソフトウェアのデータを保持するのが、employeeクラス。
こんな感じです。
1. |
2. |
3. |
4. |
5. |
6. |
7. |
8. |
9. |
10. |
11. |
12. |
13. |
14. |
15. |
16. |
17. |
18. |
19. |
20. |
21. |
22. |
23. |
24. |
25. |
26. |
27. |
28. |
29. |
30. |
31. |
32. |
33. |
34. |
35. |
36. |
37. |
38. |
39. |
40. |
41. |
42. |
43. |
44. |
45. |
46. |
47. |
48. |
49. |
50. |
51. |
52. |
53. |
54. |
55. |
56. |
57. |
58. |
59. |
60. |
61. |
62. |
63. |
64. |
65. |
66. |
67. |
68. |
69. |
|
Public Class employee |
|
Private _id As Integer |
Private _name As String |
Private _birthday As Date |
Private _PostID As Integer |
|
|
|
Public Property ID() As Integer |
Get |
Return _id |
End Get |
Set(ByVal value As Integer) |
_id = value |
End Set |
End Property |
|
|
|
Public Property Name() As String |
Get |
Return _name |
End Get |
Set(ByVal Value As String) |
_name = Value |
End Set |
End Property |
|
|
|
Public Property Birthday() As Date |
Get |
Return _birthday |
End Get |
Set(ByVal Value As Date) |
_birthday = Value |
End Set |
End Property |
|
|
|
Public ReadOnly Property Age() As Integer |
Get |
|
Dim _age As Integer = _ |
CType(Today.ToString("yyyy"), Integer) - _ |
CType(_birthday.ToString("yyyy"), Integer) |
|
If CType(Today.ToString("MMdd"), Integer) < _ |
CType(_birthday.ToString("MMdd"), Integer) Then |
_age = _age - 1 |
End If |
Return _age |
End Get |
End Property |
|
|
|
Public Property PostID() As Integer |
Get |
Return _PostID |
End Get |
Set(ByVal value As Integer) |
_PostID = value |
End Set |
End Property |
|
End Class |
|
そして、employeeクラスを束ねるコレクションクラスがEmployeeCollectionクラス。
1. |
2. |
3. |
4. |
5. |
6. |
7. |
8. |
9. |
10. |
11. |
12. |
13. |
14. |
15. |
16. |
17. |
18. |
19. |
20. |
21. |
22. |
23. |
24. |
25. |
26. |
27. |
28. |
29. |
30. |
31. |
32. |
33. |
34. |
35. |
36. |
37. |
38. |
39. |
40. |
41. |
42. |
43. |
44. |
45. |
46. |
47. |
48. |
49. |
50. |
51. |
52. |
53. |
54. |
55. |
56. |
57. |
58. |
59. |
60. |
61. |
62. |
63. |
64. |
65. |
66. |
67. |
68. |
69. |
70. |
71. |
72. |
73. |
74. |
75. |
76. |
77. |
78. |
79. |
80. |
81. |
82. |
83. |
84. |
85. |
86. |
87. |
88. |
89. |
90. |
91. |
92. |
93. |
94. |
95. |
96. |
97. |
98. |
99. |
100. |
101. |
102. |
103. |
104. |
105. |
|
Public Class EmployeeCollection |
|
Inherits System.Collections.CollectionBase |
Private da As New DataAccess |
|
|
|
Public Sub Add(ByVal id As Integer, ByVal name As String, |
ByVal birthday As Date, ByVal postid As Integer) |
|
If IDOverlap(id) = True Then |
MessageBox.Show("このIDはすでに登録されています。", |
"登録エラー", |
MessageBoxButtons.OK, |
MessageBoxIcon.Error) |
Exit Sub |
End If |
|
Dim _employee As New employee With { |
.ID = id, |
.Name = name, |
.Birthday = birthday, |
.PostID = postid |
} |
List.Add(_employee) |
da.DataCreate(_employee) |
End Sub |
|
|
|
Private Function IDOverlap(ByVal ID As Integer) As Boolean |
|
Dim Response As Boolean = False |
Dim rs = From o As employee In List Where o.ID = ID |
If rs.Count = 1 Then |
Response = True |
End If |
Return Response |
End Function |
|
|
|
Public Sub Remove(ByVal TargetEmployee As employee) |
Dim TargetIndex As Integer = _ |
GetTargetIndex(TargetEmployee.ID) |
List.RemoveAt(TargetIndex) |
da.DataDelete(TargetIndex) |
End Sub |
|
|
|
|
Public Function GetTargetIndex(ByVal ID As Integer) As Integer |
Dim TargetIndex As Integer |
Dim rs = From o As employee In List Where o.ID = ID |
|
If rs.Count = 1 Then |
TargetIndex = List.IndexOf(rs.First) |
Else |
MessageBox.Show("IDが変更されています。", |
"入力エラー", |
MessageBoxButtons.OK, |
MessageBoxIcon.Error) |
TargetIndex = -1 |
End If |
Return TargetIndex |
End Function |
|
|
|
Public Property Item(ByVal index As Integer) As employee |
Get |
Return CType(List.Item(index), employee) |
End Get |
Set(ByVal value As employee) |
List.Item(index) = value |
End Set |
End Property |
|
|
|
Public Sub Update(ByVal TargetEmployee As employee) |
Dim TargetIndex As Integer = _ |
GetTargetIndex(TargetEmployee.ID) |
If TargetIndex = -1 Then Exit Sub |
List.Item(TargetIndex) = TargetEmployee |
da.DataUpdate(TargetIndex, TargetEmployee) |
End Sub |
|
|
|
Public ReadOnly Property AveAge() As Double |
Get |
Dim rs = From o As employee In List Select o.Age |
Return rs.Average() |
End Get |
End Property |
|
|
|
Public Sub New() |
da.FileLoad(List) |
End Sub |
|
End Class |
|
これがこのソフトウェアのコアドメインです。
続く…
0 件のコメント:
コメントを投稿