473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Basic OOP Collections Question

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.App Settings("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.AppSe ttings("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

Aug 22 '08 #1
3 1921
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" wrote:
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.App Settings("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.AppSe ttings("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

Aug 22 '08 #2
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
Aug 23 '08 #3
Scott Stark wrote:
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.
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
There is a pitfall in your implementation of an enumerator for your Employees
class, which is that you store the index directly in the collection class. This
means you can have only one enumerator at any time. It won't be long before that
gives you some unwanted results.

For instance, if you needed a list of employees that appeared in the list more
than once, you might write something like
Dim Duplicated As New ArrayList
For Each A As Employee In MyEmployees
Dim N As Integer = 0
For Each B As Employee In MyEmployees
If A.Name = B.Name Then
N += 1
End If
Next B
If N 1 Then
Duplicated.Add A
End If
Next A

This would fail, because it uses two enumerators on the same collection.

Also, since you put the database read code in New(), you can't create an empty
collection for other uses. Better to put the "fill from DB" code in an explicit
sub of its own.

- - - - -

One good principle of OOP is to use the best tools you have for a job. In your
case, using List(Of Employee) would make the most sense. The generic List(Of
<T>) takes care of lots of issues with typed collections for you.

To use List(Of Employee), you can ditch having an Employees class altogether.
The only question is then where do you put the database fill code, etc.? They
can go lots of places, but a good solution is to make them shared methods of the
Employee class. Shared methods are not specific to an instance, and are more
like methods in a module somewhere.

So you could add two shared methods to Employee:

Public Shared Sub FillFromDB(Emps As List(Of Employee))
' your database code goes here...
' add them using Emps.Add(oE)
End Sub

Public Shared Function IsRichGuy(ByVal Emp As Employee) As Boolean
' a shared Predicate of Employee for use with FindAll...
Return (Emp.Salary >= 100000)
End Function

You can now write something like

Dim MyEmployees As New List(Of Employees)

' used shared method in Employee to fill it...
Employee.FillFromDB(MyEmployees)

' use shared predicate to filter the list...
Dim RichGuys As List(Of Employees) = _
MyEmployees.FindAll(AddressOf Employee.IsRichGuy)

Have fun with OOP! :)
Aug 23 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marcel | last post by:
Hi, I'm using Microsoft Visual Basic .NET and want to reference the ThisApplication variable but cannot get to it or find the OfficeCodeBehind class. Is this a shortcomming of the fact that I...
11
by: fremenusul | last post by:
First let me say I am familar with programming, but not familiar with VB.net. I am loading an existing XML document with this procedure Dim xmlFile As String = "..\data\Products.xml" Dim...
6
by: Chua Wen Ching | last post by:
Hi there, 1) I am looking for the best collections techniques to be used in my program. Is hashtable or arraylist or any .net collection, which is the fastest when adding, getting out data???...
1
by: Mark Miller | last post by:
I just recently started getting the above error on a page I am posting MULTIPART/FORM-DATA. We have SoftArtisans FileUp component and Filter installed on the server in question and up until a day...
5
by: Simon | last post by:
Hi all, I am writing a windows application using vb.net on the 1.1 framework. We have in the application, some strongly typed collections that have been written as classes that do not inherit...
13
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
5
by: WebSnozz | last post by:
Some collections are such that efficient search algorithms work on them such as binary search if the collection is a type which is sorted. I'm wondering how LINQ searches these collections and if...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.