类型名意外的结果 [英] Unexpected results from 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
有什么想法为什么我要为集合中的第一项获取字符串"而不是文档"?
Any ideas why I get "String" instead of "Document" for the first item in the collection?
推荐答案
c.Add (d)
与
c.Add d
首先,通过将d
括在括号中,使它被评估为表达式,然后将该表达式的结果(在这种情况下为String)添加到集合中.在第二个中,添加了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
这篇关于类型名意外的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文