如何确定对象的类型 [英] How to determine type of object

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

问题描述

您好,



有人可以告诉我如何获取对象类型吗?



我在对象类下创建一个数组

并在数组中放入几个用户控件



是可能的索引时数组被调用

vb知道对象的类型是什么?



因为我实际上想把事件监听器放到所有的

谢谢



我尝试了什么:



示例代码:



Hello,

can someone show me how to get the type of object?

I have create an array under the class of object
and put several of user control in the the array

is that possible when the index of array was call
vb know what is the type of object?

cause i actually want to put the event listener to all of it
thank you

What I have tried:

sample code:

dim _button as button = new button
dim _textbox as textbox = new textbox
dim _picturebox as picturebox = new picturebox

dim _objectArray() as object

_objectArray = new object() {}

_objectArray(_objectArray.count) = _button
_objectArray(_objectArray.count) = _textbox
_objectArray(_objectArray.count) = _picturebox

my idea was
for i as integer = 0 to _objectArray.count
addHandler _objectArray(i).MouseClick, addressOf BlaBlaBla
next

推荐答案

脱离我的脑海:
If TypeOf _textbox Is TextBox Then


如果您只是放置表单的控件,更好的用户控制超过对象

列表也许比数组更好



If you are only putting in Controls of your form, better user Control over Object
Also List maybe better than a Array

Dim _button As Button = New Button
Dim _textbox As TextBox = New TextBox
Dim _picturebox As PictureBox = New PictureBox

Dim _ControlList As New List(Of Control)

_ControlList.Add(_button)
_ControlList.Add(_textbox)
_ControlList.Add(_picturebox)

For Each c As Control In _ControlList
    Select Case c.GetType
          Case GetType(TextBox)
              'addhandler stuff for textboxes
    End Select

   '//Update: or using TypeOf
   
    Select Case True
          Case TypeOf c Is TextBox
              'addhandler stuff for textboxes
    End Select

Next

< br $>




as Graeme_Grant 已经提到 TypeOf 用于检查类型。

如果您有多个选择,Select Case Statement可能会更好地完成工作




as Graeme_Grant already mentioned TypeOf is for checking against Types.
if you have multiple selects a Select Case Statement might do the job better


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

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