gpt4 book ai didi

vba - 确定对象类型

转载 作者:行者123 更新时间:2023-12-04 05:27:35 25 4
gpt4 key购买 nike

在传递对函数的引用时,有没有办法确定对象类型?

我正在使用安全权限功能,它确定用户是否有权查看/编辑通过引用传递给它的表单。我想扩展它以包括报告。

为了保持函数的通用性,我想将表单或报告的 ref 作为对象传递,例如:function gfSecurity_Permission(obj as Object)
但是,我需要确定 type函数内的对象。

有谁知道这样做的方法?

MTIA

最佳答案

看一眼

typeOf and typeName

Generic object variables (that is, variables you declare as Object) can hold objects from any class. When using variables of type Object, you may need to take different actions based on the class of the object; for example, some objects might not support a particular property or method. Visual Basic provides two means of determining which type of object is stored in an object variable: the TypeName function and the TypeOf...Is operator.

TypeName and TypeOf…Is
The TypeName function returns a string and is the best choice when you need to store or display the class name of an object, as shown in the following code fragment:

Dim Ctrl As Control = New TextBox  
MsgBox(TypeName(Ctrl))

The TypeOf...Is operator is the best choice for testing an object's type, because it is much faster than an equivalent string comparison using TypeName. The following code fragment uses TypeOf...Is within an If...Then...Else statement:

If TypeOf Ctrl Is Button Then  
MsgBox("The control is a button.")
End If

关于vba - 确定对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2090578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com