How to assign Objects to an ASP array

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



LVL 2
digital_soulAsked:
Who is Participating?
 
Computer101Connect With a Mentor Commented:
PAQed with points refunded (125)

Computer101
EE Admin
0
 
bugs021997Commented:
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
 
bugs021997Commented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
digital_soulAuthor Commented:
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
 
digital_soulAuthor Commented:
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
 
digital_soulAuthor Commented:
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
 
SimonBlakeCommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.