Classes and collections in ASP?

Discussion in 'ASP General' started by Noozer, Feb 4, 2006.

  1. Noozer

    Noozer Guest

    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

    %>
    Noozer, Feb 4, 2006
    #1
    1. Advertising

  2. 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
    %>
    Anthony Jones, Feb 4, 2006
    #2
    1. Advertising

  3. Noozer

    Noozer Guest

    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" <> wrote in message
    news:...
    > 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
    > %>
    >
    >
    >
    Noozer, Feb 4, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug Poland
    Replies:
    9
    Views:
    708
    VisionSet
    Sep 27, 2003
  2. xen
    Replies:
    22
    Views:
    813
    Jim Garrison
    Oct 6, 2007
  3. mutex
    Replies:
    0
    Views:
    196
    mutex
    Jul 27, 2003
  4. Ugur ASLAN

    Classes & Collections and some related issue

    Ugur ASLAN, Apr 2, 2006, in forum: ASP General
    Replies:
    9
    Views:
    186
  5. Eric Mahurin
    Replies:
    4
    Views:
    102
    Robert Klemme
    May 1, 2005
Loading...

Share This Page