Basic OOP Collections Question

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
 
A

AMercer

I suggest a function in the Employees class like this:

public function RichEmployees As Employees
dim emps as New Employees
' loop over Me adding Employee objects that meet your critera to emps
return emps
end function

I would have used a Collection rather than an ArrayList in the Employees
class, but that is just my preference.


Scott Stark said:
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
 
M

mnadeem.abbasi

User EmployeesCollection Class as AmercerSaid. This class must be
inherited from CollectionBaseClass.
And add other functionality as you want.


Imports System
Imports System.Collections

Public Class EmployeesCollection
Inherits CollectionBase


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Add Definition

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Sub Add(ByVal obj As Employees)
Try
List.Add(obj)

Catch ex As Exception
'Exception Block
End Try
End Sub


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'IndexOf Definition

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Function IndexOf(ByVal obj As Employees) As Integer
Try
Return List.IndexOf(obj)

Catch ex As Exception
'Exception Block
End Try
End Function


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Insert Definition

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Sub Insert(ByVal index As Integer, ByVal obj As Employees)
Try
List.Insert(index, obj)

Catch ex As Exception
'Exception Block
End Try
End Sub


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Remove Definition

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Sub Remove(ByVal obj As Employees)
Try
List.Remove(obj)
Catch ex As Exception
'Exception Block
End Try
End Sub


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Contains Definition

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Function Contains(ByVal value As Employees) As Boolean
Try
' If value is not of type Int16, this will return false.
Return List.Contains(value)
Catch ex As Exception
'Exception Block
End Try
End Function


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Item Definition

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Default Public Property Item(ByVal index As Integer) As Employees
Get
Return CType(List(index), Employees)
End Get

Set(ByVal value As Employees)
Try
List(index) = value

Catch ex As Exception
'Exception Block
End Try
End Set
End Property
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top