typename 的意外结果 [英] Unexpected results from typename
本文介绍了typename 的意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从 typename 得到了一些意想不到的结果,我很困惑.希望有些人可以为我指明正确的方向.
I am getting some unexpected results from typename and am stumped. Hopefully some can point me in the right direction.
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
从中我得到以下输出:
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
我期望得到的是:
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"吗?
Any ideas why I get "String" instead of "Document" for the first item in the collection?
推荐答案
c.Add (d)
与
c.Add d
首先,通过将 d
包裹在括号中,您将使其被评估为表达式和该表达式的结果(在本例中为字符串)被添加到集合中.在第二个中,d
对象本身被添加.
In the first, by wrapping d
in parentheses you're causing it to be evaluated as an expression and the result of that expression (in this case a String) gets added to the collection. In the second, the d
object itself is added.
尝试直接在立即窗口中进行比较:
Try comparing directly in the Immediate window:
? TypeName(ActiveDocument) '>> Document
和
? TypeName( (ActiveDocument) ) '>> String
这篇关于typename 的意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文