Click here to Skip to main content
15,030,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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
Posted
Updated 21-Feb-17 21:30pm
v3

off the top of my head:
VB
If TypeOf _textbox Is TextBox Then
   
Comments
Arnold bin Boon Hoo 22-Feb-17 4:46am
   
Hello mate.
Thank you for advice :)
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



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
   
v2
Comments
Arnold bin Boon Hoo 22-Feb-17 4:47am
   
Hello mate
Thank you for your help
Graeme_Grant 22-Feb-17 4:54am
   
Still can use TypeOf in a Select Case. ;)
F. Xaver 22-Feb-17 10:04am
   
yeha true, didn't think of that possibility, Added s sample ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900