Classes and collections in ASP?

N

Noozer

I've always had problems getting my head around using collections and
classes together. Separately, they seem like fairly simple subjects, but I'm
getting muddled up when trying to use them together.

Even trying to cobble together an example to post here with my question has
me confused.

Can someone toss together a simple example of how I'd create my classes and
collection?

What I was going to use was...

Class "cName" with properties of "First" and "Last"
Class "cMovie" with properties of "Title" and "Year"
Class "cActor" with properties of a "cName" and a collection of "cMovie"'s

So in my code I'd do...

<%

Dim Actor
Set Actor = New cActor

Actor.FirstName = "Pierce"
Actor.LastName = "Brosnan"
Actor.Movies.Add "Tomorrow Never Dies","1997"
Actor.Movies.Add "GoldenEye","1995"
Actor.Movies.Add "The World is Not Enough", "1999"

response.write "Actor: " & Actor.FirstName & " " & Actor.Lastname & "<br>"
response.write "Films: <br>"

Dim Film

for each Film in Actor.Movies
response.write Film.Title & " in " & Film.Year & " <br>"
next

%>
 
A

Anthony Jones

It's not actually possible to build collection classes that support For Each
in VBScript
In fact VBScript doesn't even have a standard Collection object like VB6,
the closest thing is the Scripting.Dictionary but that's an annoying object
to use.

The following is the probably the nearest you can get to it in script:-

<%
Option Explicit

Dim Actor
Dim Film
Dim i

Set Actor = New CActor

Actor.FirstName = "Pierce"
Actor.LastName = "Brosnan"
Actor.Movies.Add "Tomorrow Never Dies","1997"
Actor.Movies.Add "GoldenEye","1995"
Actor.Movies.Add "The World is Not Enough", "1999"

For i = 1 To Actor.Movies.Count
Set Film = Actor.Movies.Item(i)
Response.Write Film.Title & " in " & Film.Year & "<br />"
Next

Class CActor
Public FirstName
Public LastName
Private moMovies

Public Property Get Movies()
Set Movies = moMovies
End Property

Private Sub Class_Initialize()
Set moMovies = New CMovies
End Sub

End Class

Class CMovie
Public Title
Public Year
End Class

Class CMovies

Private maoMovie
Private mlCount

Private Sub Class_Initialize()
ReDim maoMovie(8)
End Sub

Public Function Add(rsTitle, rdatYear)
If UBound(maoMovie) = mlCount Then
ReDim Preserve maoMovie(mlCount * 2)
End If
mlCount = mlCount + 1
Set maoMovie(mlCount) = New CMovie
maoMovie(mlCount).Title = rsTitle
maoMovie(mlCount).Year = rdatYear
Set Add = maoMovie(mlCount)
End Function

Public Property Get Count()
Count = mlCount
End Property

Public Default Property Get Item(Index)
Set Item = maoMovie(Index)
End Property

End Class
%>
 
N

Noozer

Thanks for the excellent example!

Arrays work for me. Had me scratching my head for a minute at the "Set Add =
maoMovie(mlCount)" line in the Add function. Makes perfect sense now.

Thanks again!
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top