iLogic: Dynamically assemble sub or function name

iLogic: Dynamically assemble sub or function name

Curtis_Waguespack
Consultant Consultant
643 Views
7 Replies
Message 1 of 8

iLogic: Dynamically assemble sub or function name

Curtis_Waguespack
Consultant
Consultant

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

 

EESignature

0 Likes
Accepted solutions (2)
644 Views
7 Replies
Replies (7)
Message 2 of 8

C_Haines_ENG
Collaborator
Collaborator

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
Message 3 of 8

g.georgiades
Advocate
Advocate
Accepted solution

vb.net has CallByName you can try

https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/early-late...

 

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.

 

 

Message 4 of 8

Curtis_Waguespack
Consultant
Consultant

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. 

EESignature

0 Likes
Message 5 of 8

Curtis_Waguespack
Consultant
Consultant

@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

 

  

EESignature

Message 6 of 8

C_Haines_ENG
Collaborator
Collaborator
Accepted solution

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

 

Message 7 of 8

Curtis_Waguespack
Consultant
Consultant

@C_Haines_ENG , 😄 I'm certain I've swiped a snippet or two from you on here,  so we're even! 

 

 

EESignature

0 Likes
Message 8 of 8

JBerns
Advisor
Advisor
That is slick!
-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional