What is it about .NET that won't let it support an array of objects?WHat is the purpose of leaving out this functionality?
Printable View
What is it about .NET that won't let it support an array of objects?WHat is the purpose of leaving out this functionality?
An array of objects?
Dim a() As Object
An array of controls?
Dim ctrl() As Textbox
'or if they exist already
Dim ctrl() As TextBox={TextBox1,TextBox2,TextBox3}
And as far as events go why not just add more controls to the same event handle. If this is something you'll need to do often then just make a macro to speed up the process.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click
I don't see that any functionality was lost in fact I'd say we gained some. Now you can have controls of different types act the same as a control array.
Yah one control can handle events from multiple controls, but how do you know which control was clicked?
With a control array you could find out by getting the index....msgbox Button(Index). But how do you do that with .Net?
Generally the sender argument is the control that is raising the event.
VB Code:
If TypeOf (sender) Is Button Then Dim c As Button c = sender MsgBox(c.Name) End If
thanks, that seems to work fine.
One other question, why is the name property not in the Sender list?
If you type msgbox Sender. there is only GetType there is no Name property.
Because the sender may not have a name property. I know, sounds ludicrous... but i think it has more to do with being able to have any object raise the event, and not all objects have the same properties, so you need a layer of abstraction in there.
Not all the objects have a name property. If you just type MessageBox.Show(Sender.Name), then if the object has a name property it should show you.Quote:
Originally posted by Arc
thanks, that seems to work fine.
One other question, why is the name property not in the Sender list?
If you type msgbox Sender. there is only GetType there is no Name property.
Didn't mean to repeat you!Quote:
Originally posted by nemaroller
Because the sender may not have a name property. I know, sounds ludicrous... but i think it has more to do with being able to have any object raise the event, and not all objects have the same properties, so you need a layer of abstraction in there.
Makes sense I guess... but it makes it difficult to figure out on your own :)
Considering we both posted at the same time, it could have easily concluded with your message being posted first. But as it turns out, I won!!!!
HARARHAR!
;)
just a little comment on this: I dont think there is a need to check the type of the object (unless ofcourse the sub handles events for different types of objects). Umm also if option strict is on, c = sender wouldn't work:p I think it's a better coding practice to convert it even if option strict is offQuote:
Originally posted by nemaroller
VB Code:
If TypeOf (sender) Is Button Then Dim c As Button c = sender MsgBox(c.Name) End If
I would use c=DirectCast(sender, Button)
(I think directCast works faster than Ctype )
Umm also if option strict is on, c = sender wouldn't work I think it's a better coding practice to convert it even if option strict is off
Yea... well in my race against Lunatic, I skipped that part :)
hehe I think Arc would figure it out anyways:DQuote:
Originally posted by nemaroller
Umm also if option strict is on, c = sender wouldn't work I think it's a better coding practice to convert it even if option strict is off
Yea... well in my race against Lunatic, I skipped that part :)
oh btw I announce you the winner of this so called "race", congratulations :)