确定对象类型 [英] Determine Object Type

查看:43
本文介绍了确定对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to determine the Object type, when passing a reference to a function?

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

I'm using a security permissions function, which determines if the user has permission to view/edit the Form passed to it by reference. I'd like to expand this to include reports as well.

为了保持函数的通用性,我想将表单或报告的引用作为对象传递,例如:function gfSecurity_Permission(obj as Object)

To keep the function generic, I'd like to pass a ref for either a Form or a Report as an Object, eg: function gfSecurity_Permission(obj as Object)

但是,我需要确定函数内对象的type.

However, I'd need to determine the type of the object within the function.

有人知道这样做的方法吗?

Does anyone know of a way to do that?

MTIA

推荐答案

看一看

typeOf 和 typeName

通用对象变量(即你声明为Object的变量)可以容纳来自任何类的对象.使用 Object 类型的变量时,您可能需要根据类别采取不同的行动目的;例如,某些对象可能不支持特定的属性或方法.Visual Basic 提供了两种确定方法哪种类型的对象存储在对象变量中:TypeName函数和 TypeOf...Is 运算符.

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 和 TypeOf...Is
这TypeName 函数返回一个字符串,是您的最佳选择需要存储或显示一个对象的类名,如图以下代码片段:

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))

TypeOf...Is 运算符是测试对象的最佳选择类型,因为它比等效的字符串比较快得多使用类型名称.以下代码片段使用 TypeOf...Is 在一个If...Then...Else 语句:

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

这篇关于确定对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆