Class structure in classic asp

I need to use class structure in classic asp. I wrote the following three classes.

Category.asp

<%

Class Category

    Private NameVar

    Public Property Get Name()
        Name = NameVar
    End Property

    Public Property Let Name(nameParam)
        NameVar = nameParam
    End Property

End Class

%>

Item.asp

<%

Class Item

    Private NameVar
        Private CategoryVar

    Public Property Get Name()
        Name = NameVar
    End Property

    Public Property Let Name(nameParam)
        NameVar = nameParam
    End Property

    Public Property Get Category()
        category = categoryVar
    End Property

    Public Property Let Category(categoryParam)
    CategoryVar = categoryParam
    End Property

End Class

%>

test.asp

<%

    Dim CategoryVar
    Set CategoryVar = New Category

    CategoryVar.Name = "Weight"

    Dim ItemVar
    Set ItemVar = New Item

    ItemVar.Name = "kg"
    ItemVar.Category = CategoryVar

%>
<html>
    <head>
        <title>UoM Componet Testing</title>
    </head>
    <body>
        <%= ItemVar.Name %><br/>
    </body>
</html>

When I run this code, I found some problem. Error:

Microsoft VBScript Runtime (0x800A01B6) The object does not support this property or method: "CategoryVar".

How can this be explained? Please help me.

+5
source share
3 answers

In VBScript, if you know that a property will contain a reference to an object, you must define it using the operator Property Set. In addition, when assigning object references to variables, you must use the operator Set. With this in mind, the following changes must be made:

Item.asp

Class Item

    '<snip>

    Public Property Get Category()
        ' Add Set here
        Set category = categoryVar
    End Property

    ' Change "Property Let" to "Property Set"
    Public Property Set Category(categoryParam)
        Set CategoryVar = categoryParam
    End Property

End Class

test.asp

<%
    ' <snip>    

    ItemVar.Name = "kg"
    Set ItemVar.Category = CategoryVar

%>
+8

, , , , .

Name Item, Item. -, ItemVar.Category = CategoryVar, Item Category.

, , :

<%
    Class Category
        Private NameVar

        Public Property Get Name()
            Name = NameVar
        End Property

        Public Property Let Name(nameParam)
            NameVar = nameParam
        End Property
    End Class

    Class Item
        Private NameVar
        Private ItemVar

        Public Property Get Name()
            Name = NameVar
        End Property

        Public Property Let Name(nameParam)
            NameVar = nameParam
        End Property

        Public Property Get Item()
            Item = ItemVar
        End Property

        Public Property Let Item(itemParam)
            ItemVar = itemParam
        End Property
    End Class

    Dim CategoryVar
    Set CategoryVar = New Category

    CategoryVar.Name = "Weight"

    Dim ItemVar
    Set ItemVar = New Item

    ItemVar.Name = "kg"
    'ItemVar.Category = CategoryVar ' There is no 'Category' property in your class
%>

<html>
    <head>
        <title>UoM Componet Testing</title>
    </head>
    <body>
        <%= ItemVar.Name %><br/>
    </body>
</html>
0

I ran into the same problems as Rory, overriding the name and missing Category property.

I adjusted the code below to accommodate the edited question:

Shouldn't it be something like below?

Class Item      
    Private NameVar   
    Public Property Get Name()         
        Name = NameVar     
    End Property      
    Public Property Let Name(nameParam)         
        NameVar = nameParam     
    End Property      

    Private CategoryVar   
    Public Property Get Category()         
        Category = CategoryVar     
    End Property      
    Public Property Let Category(CategoryParam)         
        CategoryVar = CategoryParam     
    End Property 
End Class 




Dim CategoryVar     
Set CategoryVar = New Category      
CategoryVar.Name = "Weight"      

Dim ItemVar     
Set ItemVar = New Item      
ItemVar.Name = "kg"     
ItemVar.Category = CategoryVar.Name
0
source

All Articles