IEnuermator?

R

Rob Meade

Hi all,

I've tried to use the above to make me class and collection class
enumerable - but I dont seem to be having much success.

I was wondering if my problem is because the ArrayList is zero based where
as when I run the code, the MoveNext gets triggered setting the
currentPosition to 1 - (thus I've set it to -1), but it still doesn't work.

The 'GuestbookEntry' class is definately being populated, and as far as I
can see being added to _guestbook, my only problem seems to be when I try to
put this back out.

The 'collection' class is as follows:

Imports Microsoft.VisualBasic
Imports System.Data.SqlClient

Public Class Guestbook
Implements IEnumerable, IEnumerator

#Region " Class Variables "

' class variables
Private _guestbook As ArrayList
Private _currentPosition As Integer

#End Region

#Region " Instantiate "

' instantiate
Public Sub New()

' create a new instance of our object
_guestbook = New ArrayList
_currentPosition = -1

End Sub

#End Region

#Region " Properties "

' properties

#End Region

#Region " Methods "

' methods
Public Function GetAuthorisedEntries() As Boolean

' declare variables
Dim Result As Boolean
Dim Connection As SqlConnection
Dim Command As SqlCommand
Dim Reader As SqlDataReader
Dim GuestbookEntry As GuestbookEntry

' exception handling
Try

' create and open our connection object
Connection = New
SqlConnection(ConfigurationManager.ConnectionStrings("AidenMeade").ConnectionString)
Connection.Open()

' create and define our command object
Command = New SqlCommand()
Command.CommandType = Data.CommandType.StoredProcedure
Command.CommandText = "getGuestbookEntries"
Command.Connection = Connection

' execute
Reader = Command.ExecuteReader()

' check that we have return some data
If Reader.HasRows Then

' iterate
Do While Reader.Read

' create a new instance of our object
GuestbookEntry = New GuestbookEntry(Reader("EntryID"),
Reader("EntryDetail").ToString, Reader("EntryName").ToString,
CType(Reader("EntryDateTime"), Date))

' add guestbook entry to collection
Me._guestbook.Add(GuestbookEntry)

Loop

' set result
Result = True

Else

' set result
Result = False

End If

' tidy up
Reader.Close()
Reader = Nothing
Command.Dispose()
Command = Nothing
Connection.Close()
Connection = Nothing

Catch ex As Exception

' TODO: Error handling

' tidy up
If Reader Is Nothing = False Then

Reader.Close()
Reader = Nothing

End If

If Command Is Nothing = False Then

Command.Dispose()
Command = Nothing

End If

If Connection Is Nothing = False Then

Connection.Close()
Connection = Nothing

End If

End Try

' return result
Return Result

End Function

#End Region

Public Function GetEnumerator() As System.Collections.IEnumerator
Implements System.Collections.IEnumerable.GetEnumerator
Return Me
End Function

Public ReadOnly Property Current() As Object Implements
System.Collections.IEnumerator.Current
Get
Return _guestbook(_currentPosition)
End Get
End Property

Public Function MoveNext() As Boolean Implements
System.Collections.IEnumerator.MoveNext
_currentPosition += 1
End Function

Public Sub Reset() Implements System.Collections.IEnumerator.Reset
_currentPosition = -1
End Sub

End Class


The code I'm using to pull the info back out is on the code behind page for
my web page:

' declare variables
Dim Guestbook As Guestbook
Dim GuestbookEntry As GuestbookEntry

' create a new instance of our object
Guestbook = New Guestbook

' get guestbook entries
Guestbook.GetAuthorisedEntries()

' iterate
For Each GuestbookEntry In Guestbook

Dim row As TableRow
Dim cell As TableCell

row = New TableRow
cell = New TableCell

cell.Text = GuestbookEntry.Name

row.Cells.Add(cell)

tblGuestbookItems.Rows.Add(row)

Next



I'd really appreciate any help - I *think* the problem is in the
GetEnumerator() function, but I thought returning 'me' was correct.

Regards

Rob
 
R

Rob Meade

Doh!

Ok, so I didn't have a property to return all of the items in the
arraylist - I have this now - and it works - yay me! :blush:)

Rob
 

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