S
Scott Stark
Hello,
I'm trying to get a better handle on OOP programming principles in VB.NET.
Forgive me if this question is sort of basic, but here's what I want to do.
I have a collection of Employee objects that I can iterate through
relatively easily. I've included code at the bottom of this message.
I can pretty easily iterate through my employee objects like so:
Dim theEmployees As Employees = New Employees
For Each Emp As Employee In theEmployees
Response.Write(Emp.Name & ": " & FormatCurrency(Emp.Salary, 2) & "<br
/>")
Next
My question is, let's say I want to create a new method...GetRichEmployees,
which returns a set of employee objects whose salary is >= 100,000. How
would I implement this? I know it's basic, but the answer to this will
probably help me solidify some of the more basic OOP priniciples in my head.
Still learning here, thanks!
Scott
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports Microsoft.ApplicationBlocks.Data
Public Class Employee
Private sName As String
Private dSalary As Double
Public Property Name() As String
...
Public Property Salary() As String
...
Public Sub New()
sName = ""
dSalary = 0
End Sub
Public Sub New(ByVal sn As String, ByVal ds As Double)
...
End Sub
Public Sub Save()
SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings("ConnString"),
Data.CommandType.Text, "INSERT INTO Employees ([Name], Salary) VALUES ('" &
sName & "', " & dSalary & ");")
End Sub
End Class
Public Class Employees
Implements IEnumerable, IEnumerator
Private iIndex As Integer 'index of the array
Private iCount As Integer 'count of the elements
Private oEmp As ArrayList = New ArrayList 'array of employee objects
Public Sub New()
'any employees?
Dim objReader As SqlDataReader =
SqlHelper.ExecuteReader(ConfigurationManager.AppSettings("ConnString"),
Data.CommandType.Text, "SELECT * FROM Employees;")
iCount = -1
While objReader.Read
Dim oE As Employee = New Employee
oE.Name = objReader("Name").ToString
oE.Salary = objReader("Salary")
oEmp.Add(oE)
iCount += 1
End While
objReader.Close()
iIndex = -1
End Sub
Public Function GetEnumerator() As IEnumerator Implements
IEnumerable.GetEnumerator
Return Me 'returns an IEnumerator
End Function
Public ReadOnly Property Current() As Object Implements
IEnumerator.Current
Get
Return oEmp(iIndex) 'return the current object at index iIndex
End Get
End Property
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
If iIndex < iCount Then
iIndex += 1
MoveNext = True
Else
MoveNext = False
End If
End Function
Public Sub Reset() Implements IEnumerator.Reset
iIndex = -1
End Sub
End Class
I'm trying to get a better handle on OOP programming principles in VB.NET.
Forgive me if this question is sort of basic, but here's what I want to do.
I have a collection of Employee objects that I can iterate through
relatively easily. I've included code at the bottom of this message.
I can pretty easily iterate through my employee objects like so:
Dim theEmployees As Employees = New Employees
For Each Emp As Employee In theEmployees
Response.Write(Emp.Name & ": " & FormatCurrency(Emp.Salary, 2) & "<br
/>")
Next
My question is, let's say I want to create a new method...GetRichEmployees,
which returns a set of employee objects whose salary is >= 100,000. How
would I implement this? I know it's basic, but the answer to this will
probably help me solidify some of the more basic OOP priniciples in my head.
Still learning here, thanks!
Scott
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports Microsoft.ApplicationBlocks.Data
Public Class Employee
Private sName As String
Private dSalary As Double
Public Property Name() As String
...
Public Property Salary() As String
...
Public Sub New()
sName = ""
dSalary = 0
End Sub
Public Sub New(ByVal sn As String, ByVal ds As Double)
...
End Sub
Public Sub Save()
SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings("ConnString"),
Data.CommandType.Text, "INSERT INTO Employees ([Name], Salary) VALUES ('" &
sName & "', " & dSalary & ");")
End Sub
End Class
Public Class Employees
Implements IEnumerable, IEnumerator
Private iIndex As Integer 'index of the array
Private iCount As Integer 'count of the elements
Private oEmp As ArrayList = New ArrayList 'array of employee objects
Public Sub New()
'any employees?
Dim objReader As SqlDataReader =
SqlHelper.ExecuteReader(ConfigurationManager.AppSettings("ConnString"),
Data.CommandType.Text, "SELECT * FROM Employees;")
iCount = -1
While objReader.Read
Dim oE As Employee = New Employee
oE.Name = objReader("Name").ToString
oE.Salary = objReader("Salary")
oEmp.Add(oE)
iCount += 1
End While
objReader.Close()
iIndex = -1
End Sub
Public Function GetEnumerator() As IEnumerator Implements
IEnumerable.GetEnumerator
Return Me 'returns an IEnumerator
End Function
Public ReadOnly Property Current() As Object Implements
IEnumerator.Current
Get
Return oEmp(iIndex) 'return the current object at index iIndex
End Get
End Property
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
If iIndex < iCount Then
iIndex += 1
MoveNext = True
Else
MoveNext = False
End If
End Function
Public Sub Reset() Implements IEnumerator.Reset
iIndex = -1
End Sub
End Class