?
Solved

How to assign Objects to an ASP array

Posted on 2007-07-25
8
Medium Priority
?
770 Views
Last Modified: 2012-08-14
Very simply I have defined two classes called Card and Suit. The Card Object has 2 properties, Value and Suit. The Suit Object has a property that I want to store as an array of Card objects. The code below is returning 'Object doesn't support this property or method: 'm_cards'' When I attempt to assign a Card Object to the m_cards Array. Can anyone help? Are there inherant issues with storing Objects in an array? Many thanks


Dim ArrayCardTypes(12)
ArrayCardTypes(0) = "two"
ArrayCardTypes(1) = "three"
ArrayCardTypes(2) = "four"
ArrayCardTypes(3) = "five"
ArrayCardTypes(4) = "six"
ArrayCardTypes(5) = "seven"
ArrayCardTypes(6) = "eight"
ArrayCardTypes(7) = "nine"
ArrayCardTypes(8) = "ten"
ArrayCardTypes(9) = "jack"
ArrayCardTypes(10) = "queen"
ArrayCardTypes(11) = "king"
ArrayCardTypes(12) = "ace"

Dim arraySuits(3)
arraySuits(0) = "hearts"
arraySuits(1) = "clubs"
arraySuits(2) = "spades"
arraySuits(3) = "diamonds"

Class Card

      Private m_suit
      Private m_value
      
      Public Property Get Suitname()
          Suitname = m_suit
      End Property

      Public Property Let Suitname(p_Data)
            m_suit = p_Data
      End Property
      
      Public Property Get Value()
          Value = m_value
      End Property

      Public Property Let Value(p_Data)
            m_value = p_Data
      End Property

End Class

Class Suit

      Private m_name
      Private m_cards(12)
      
      Public Property Get SuitName()
          SuitName = m_name
      End Property

      Public Property Let SuitName(p_Data)
            m_name = p_Data
      End Property
      
      Public Property Get Cards()
          Cards = m_cards
      End Property

      Public Property Let Cards(p_Data)
            intCounter = 0
            For each item in p_Data
                  Set c = New Card
                  c.Suitname = SuitName
                  c.Value = item
                  m_cards(intCounter) = c
                  intCounter = intCounter + 1
            Next
      End Property
      
End Class


Dim d
Set d = New Suit
d.SuitName = "hearts"
d.Cards = ArrayCardTypes



0
Comment
Question by:digital_soul
7 Comments
 
LVL 14

Expert Comment

by:bugs021997
ID: 19563992
VBScript's arrays have quite a few limitation, you need to declare the array without a size (or with a constant size, e.g. 0), and then re-dimension it from the variable. Here is the example;
 
<%
    x = 15
    Dim demoArray()
    ReDim demoArray(x)
%>
0
 
LVL 14

Expert Comment

by:bugs021997
ID: 19563997
if you want to increase the size of an array within a loop, and want to preserve the existing values assigned to the array, you need to use the Preserve keyword (otherwise, the array gets erased and re-created, thus destroying all your values):
 
<%
    Dim demoArray()
    for i = 1 to 5
        ReDim Preserve demoArray(i)
        DemoArray(i) = "test" & i
    next
%>
0
 
LVL 2

Author Comment

by:digital_soul
ID: 19564021
My problem isnt actually assigning values to the array, its specifically when I try and assign an Object (In this case a Card Object) to it. If I just assign string variables to each element then I dont have a problem, but as soon as I try and assign an Object as below it falls over

Public Property Let Cards(p_Data)
            intCounter = 0
            For each item in p_Data
                  Set c = New Card
                  c.Suitname = SuitName
                  c.Value = item
                  'm_cards(intCounter) = c.Value --- this would not cause an error, however the line below does
                  m_cards(intCounter) = c
                  intCounter = intCounter + 1
            Next
      End Property
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:digital_soul
ID: 19564037
In fact If I try and assign an object to any variable it doesnt like it.

im d
Set d = New Suit
Dim c
c=d    ' Returns Object doesn't support this property or method

Is there a certain way of assigning Object Instances to variables or array elements?

0
 
LVL 2

Author Comment

by:digital_soul
ID: 19564081
My bad. Seems I wasnt using the SET keyword which is required when you are assigning an Object to any variable.

SET m_cards(intCounter) = c now works

thanks
0
 
LVL 7

Expert Comment

by:SimonBlake
ID: 19565240

Correct you do have to use th set when assigning objects to variables - and don't forget to set them to nothing when done, as the asp runtime is not great at clearing up after itself otherwise you might run into memory leak issues if your site is busy.

0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 21091520
PAQed with points refunded (125)

Computer101
EE Admin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question