Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic: Dynamically assemble sub or function name

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Curtis_Waguespack
385 Views, 7 Replies

iLogic: Dynamically assemble sub or function name

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

7 REPLIES 7
Message 2 of 8

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

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

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

Message 5 of 8

@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

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

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

 

 

EESignature

Message 8 of 8
JBerns
in reply to: Curtis_Waguespack

That is slick!
-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report