首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >typename的意外结果

typename的意外结果
EN

Stack Overflow用户
提问于 2014-03-05 01:43:56
回答 1查看 3.1K关注 0票数 1

我从typename中得到了一些意想不到的结果,我感到很困惑。希望有人能给我指明正确的方向。

代码语言:javascript
复制
Private Sub T()
    Dim d As Word.Document
    Dim s As String
    Dim c As Collection
    Dim i As Long
    Dim o As Object

    Set d = ActiveDocument
    s = "X"
    Set c = New Collection

    Debug.Print "d is a " & TypeName(d)
    Debug.Print "s is a " & TypeName(s)
    Debug.Print "c is a " & TypeName(c)

    c.Add (d)
    c.Add (s)
    For i = 1 To c.count
        Debug.Print "Item " & i & " of the collection is a " & " " & TypeName(c.Item(i))
    Next i
End Sub

从中我得到以下输出:

代码语言:javascript
复制
d is a Document
s is a String
c is a Collection
Item 1 of the collection is a String
Item 2 of the collection is a String

我想得到的是:

代码语言:javascript
复制
d is a Document
s is a String
c is a Collection
Item 1 of the collection is a Document
Item 2 of the collection is a String

对于集合中的第一项,我为什么要得到"String“而不是"Document”,有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-05 04:57:51

代码语言:javascript
复制
c.Add (d) 

代码语言:javascript
复制
c.Add d 

在第一个示例中,通过将d封装在括号中,使其被计算为表达式,并将该表达式的结果(在本例中为字符串)添加到集合中。在第二个部分中,添加了d对象本身。

尝试直接在直接窗口中进行比较:

代码语言:javascript
复制
? TypeName(ActiveDocument)       '>> Document

代码语言:javascript
复制
? TypeName( (ActiveDocument) )   '>> String
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22186853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档