Controlling Events

  • Thread starter Thread starter Brent McIntyre
  • Start date Start date
B

Brent McIntyre

Good evening all,

I am trying to set-up an array of procedures, which HELP informs me is
impossible, so as a secondary idea I thought I could set up an array of
initializing an event, which I also seem unable to do.

What I am basically trying to do, is set-up an array that can somehow
initialize procedures, as I have nearly two-hundred panels I need to
cycle through and I don't want to use an extended IF Statement.

Any help you may be able to provide would be much appreciated. Please
see the example below of what I am basically trying to do.

Yours sincerely,

Brent McIntyre


EXAMPLE of current set-up

If Selection_Variable = 1 then
Procedure_Panel_One_Selected
Else
If Selection_Variable = 2 then
Procedure_Panel_Two_Selected
Else

etc....
End If
End If

EXAMPLE of what I would like

Procedure_Panel_Selection(1) ' Which would the run
Procedure_Panel_One_Selected

This takes a lot less code and can allow me to make my code easier for
others to read and understand.
 
Hi Brent,

A procedure can be called indirectly by using a Delegate. [Simplified, a
Delegate is a fancy function pointer]. It would be easy to utilise an array of
Delegates. There would still be the requitrement of setting this array up This
itself may be stuck in a loop, perhaps. Good things to learn about, are
Delegates.

However, another possibility, and easier to use, is CallByName() to which
you give the object and the name of the method to call. You might find it
useful to change "Proc_Panel_One_Sel" to "Proc_Panel_1_Sel". Then you can
have:

Dim sProcSel As String _
= "Procedure_Panel_" & Selection_Variable & "_Selected"
CallByName (Me, sProcSel, CallType.Method, Nothing)

I'm not sure whether the Me and the Nothing are applicable for you, but
have a play with it.

Regards,
Fergus
 
Hi Brent,
I dont think you mean it but your example what you want, looks exact as case
select.
\\\
case select Procedure_Panel_Selection
case 1
Procedure_Panel_One_Selected
case 2
Procedure_Panel_Two_Selected
end select
///
And to extend it with some events
\\\
Private Sub Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
Dim btn As Button = DirectCast(sender, Button)
Select Case btn.Name
Case "Button1"
Procedure_Panel_One_Selected
Case "Button2"
Procedure_Panel_Two_Selected
End Select
///
Of course I mis something but, for me it looks like that.
Cor
 
Brent,
As Furgus suggested the array of Delegates is how you get an array of
procedures.

Delegate Sub PanelSelected()

Dim Procedure_Panel_Selection() As PanelSelected = _
{ _
AddressOf Procedure_Panel_One_Selected, _
AddressOf Procedure_Panel_Two_Selected, _
AddressOf Procedure_Panel_Three_Selected _
}

Remember that arrays are zero based:

Procedure_Panel_Selection(0).Invoke()

Will execute Procedure_Panel_One_Selected.

Note:
If Selection_Variable = 1 then
Procedure_Panel_One_Selected
Else
If Selection_Variable = 2 then
Procedure_Panel_Two_Selected
Else

etc....
End If
End If
Also Select Case or ElseIf both simplify your selection statement.

Select Case Selection_Variable
Case 1
Procedure_Panel_One_Selected
Case 2
Procedure_Panel_Two_Selected
End Select

If Selection_Variable = 1 Then
Procedure_Panel_One_Selected
ElseIf Selection_Variable = 2 Then
Procedure_Panel_Two_Selected
ElseIf Selection_Variable = 3 Then
Procedure_Panel_Three_Selected
End if

However my concern is you really need Polymorphism. Is the initialization
code largely the same for each panel or unique for each panel?

I would consider creating one or more new classes that are derived from
Panel that has the initialization code in the constructor of this new class.
Depending on how unique this initialization code is, I would have custom
properties, a custom event, or multiple derived classes for the unique code.
Applying OOP principles to simplify the code.

Hope this helps
Jay
 
Brent McIntyre said:
I am trying to set-up an array of procedures, which HELP informs me is
impossible, so as a secondary idea I thought I could set up an array of
initializing an event, which I also seem unable to do.

What I am basically trying to do, is set-up an array that can somehow
initialize procedures, as I have nearly two-hundred panels I need to
cycle through and I don't want to use an extended IF Statement.

Any help you may be able to provide would be much appreciated. Please
see the example below of what I am basically trying to do.

Why not loop through the panels and add a shared event handler using
'AddHandler'? In the event handler you can determine which control the
event belongs to by looking at the 'sender' parameter.
 
Good morning all,

Thank you very much for all your ideas ! I will review them tonight.

Yours sincerely,

Brent McIntyre
 
Good evening all,

Thanks to everyone that helped me out, Fergus, Cor, Jay and Herfried.
In the end I used Delegates as described by Furgus and Jay, as it
provided me with not only a new idea, but a great way of reducing code
and increasing simplicity so that anyone can modify what I have now
set-up.

Thank you all for your great ideas, if only the in-built help could
offer solutions rather than saying what isn't possible.

Yours sincerely,

Brent McIntyre
 
Back
Top