ASP:调用存储在新类中的对象

时间:2013-08-22 08:39:22

标签: class dictionary asp-classic

这是我的代码的简化示例,用于显示我的类的外观...... 这两个类都是单独的asp文件(newsitem.asp和messageboard.asp)

Class NewsItem
    private c_title
    private c_content

    public property get title
        title = c_title
    end property 

    public property get content
        content = c_content
    end property

    public function loadById (id)
        'Fill all the variables with database records'
        c_title = rs(...)
        c_content = rs(...)
    end function
End Class

Class MessageBoard
    Dim dict

    private sub Class_Initialize
         set dict = Server.CreateObject("Scripting.Dictionary")     
    end sub

    public property get messages ()
        messages = dict
    end property

    public function fillBoard ()
        Dim oNews : set oNews = new NewsItem

        For all the newsitems in the database
            oNews.loadByID(newsitemID)
            dict.add newsitemsID, oNews
        Next
    end function
End Class

在第三个文件(showMessageboard.asp)中,我包含上述两个文件并尝试执行以下操作:

Dim oMB : set oMB = new MessageBoard
oMB.fillBoard() 'Dictionary of MessageBoard class is filled with NewsItem-objects'
response.write oMB.messages.item(1).content 'show the content of the first NewsItem-object'

我总是在最后一行收到错误,说明不支持该方法的对象属性(我不知道此刻的确切错误消息,因为我不在家:)将添加它稍后)。

任何人都可以查看此代码并告诉我可能出现的问题吗?我通过将 response.write dict.item(1).content 添加到公共属性获取消息来测试我的代码,它运行正常。因此在课堂外使用字典时会出现问题???

我也尝试使用功能代替公共属性,但结果相同......

1 个答案:

答案 0 :(得分:3)

在你的属性中获取Messages()你必须像这样设置返回值:

public property get messages ()
    set messages = dict
end property
相关问题