Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Classes and collections in ASP?

Reply
Thread Tools

Classes and collections in ASP?

 
 
Noozer
Guest
Posts: n/a
 
      02-04-2006
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

%>




 
Reply With Quote
 
 
 
 
Anthony Jones
Guest
Posts: n/a
 
      02-04-2006
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(
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
%>



 
Reply With Quote
 
 
 
 
Noozer
Guest
Posts: n/a
 
      02-04-2006
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!

"Anthony Jones" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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(
> 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
> %>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes & Collections and some related issue Ugur ASLAN ASP General 9 04-05-2006 03:38 AM
collections with values of fixed classes/lengths Eric Mahurin Ruby 4 05-01-2005 08:07 PM
Sorting collections based on nested collections Doug Poland Java 9 09-27-2003 10:46 PM
InnerProperty Persistance for Collections containing other Collections mutex ASP .Net Building Controls 0 07-27-2003 02:45 PM



Advertisments