经典asp中的类结构

zan*_*tet 5 asp-classic

我需要在经典的asp中使用类结构.我写了三节课.

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

%>
Run Code Online (Sandbox Code Playgroud)

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

%>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我发现了一些问题.错误是:

Microsoft VBScript运行时(0x800A01B6)对象不支持此属性或方法:'CategoryVar'".

怎么解释这个?请帮我.

Che*_*vel 8

在VBScript中,如果您知道属性将包含对象引用,则必须使用该Property Set语句定义它.此外,Set在为变量分配对象引用时,必须使用该语句.考虑到这一点,需要进行以下更改:

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
Run Code Online (Sandbox Code Playgroud)

TEST.ASP

<%
    ' <snip>    

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

%>
Run Code Online (Sandbox Code Playgroud)