类型名意外的结果

编程入门 行业动态 更新时间:2024-10-26 19:27:48
本文介绍了类型名意外的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从类型名中得到了一些意外的结果,并感到困惑.希望有人可以指出正确的方向.

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

更多推荐

类型名意外的结果

本文发布于:2023-11-13 14:13:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584535.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意外   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!