Can we dynamically assemble a function or sub name to call a specific one?
I tried a few things based on searches, but came up empty... and feel like I might be over looking something simple.
Just wondering if someone knows of a clever way to do this from an ilogic rule.
Thanks! Curtis
Sub Main 'Type is set in another rule before running this rule Call SharedVariable("Type") & "_Thing" End Sub Sub Left_Thing MsgBox("This is the left sub") End Sub Sub Center_Thing MsgBox("This is the center sub") End Sub Sub Right_Thing MsgBox("This is the right sub") End Sub
Solved! Go to Solution.
Solved by C_Haines_ENG. Go to Solution.
Solved by g.georgiades. Go to Solution.
I suppose a select case statement would be too large?
Sub Main 'Type is set in another rule before running this rule oVariable = InputBox("Input Direction", "", "") & "_Thing" Select Case oVariable Case "Left_Thing" : Call Left_Thing Case "Center_Thing" : Call Center_Thing Case "Right_Thing" : Call Right_Thing End Select End Sub Sub Left_Thing MsgBox("This is the left sub") End Sub Sub Center_Thing MsgBox("This is the center sub") End Sub Sub Right_Thing MsgBox("This is the right sub") End Sub
vb.net has CallByName you can try
you might have to add Microsoft.VisualBasic.dll reference
You can also use reflection - there are lots of examples around, but here is a pretty simple one.
https://stackoverflow.com/a/37925932
Regardless, I would not recommend it too much though both methods are runtime compiled so it can cause program delay depending on what you are doing.
@C_Haines_ENG's method is definitely faster and simpler to use.
Thanks for the reply. In this case I am wanting to set this rule up for a user, so that they can just add subs over time without having to do anything more to the rule.... kind of hard to explain, but that is the reason I was trying to avoid listing the subs out explicitly in a case statement or otherwise.
@g.georgiades , thanks!
CallByName was the missing piece of the puzzle for me.
example in case it helps someone in the future.
Imports System.Reflection
Imports Microsoft.VisualBasic
Sub Main
Dim sList = New String(){"Left", "Center", "Right"}
sSubName = InputListBox("Prompt", sList, sList(0), "iLogic", "List")
CallByName(Me, sSubName & "_Thing", CallType.Method)
End Sub
Sub Left_Thing
MsgBox("This is the left sub")
End Sub
Sub Center_Thing
MsgBox("This is the center sub")
End Sub
Sub Right_Thing
MsgBox("This is the right sub")
End Sub
I got it! I had to help a legend of the forums. Guess were even then? 🤣
Ah i see you beat me to it by 20 minutes. Darn.
Sub Main
Dim oClass As New NewClass 'Type is set in another rule before running this rule oVariable = InputBox("Input Direction", "", "") & "_Thing" CallByName(oClass, oVariable, Microsoft.VisualBasic.CallType.Method) End Sub Class NewClass Sub Left_Thing MsgBox("This is the left sub") End Sub Sub Center_Thing MsgBox("This is the center sub") End Sub Sub Right_Thing MsgBox("This is the right sub") End Sub End Class
Can't find what you're looking for? Ask the community or share your knowledge.