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: 

Passing variables between ilogic subs

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
schleede.dave
4206 Views, 4 Replies

Passing variables between ilogic subs

I'm not very good with iLogic so please bear with me.  I am trying to figure out how to pass information between two separate private subs contained within the same Rule.  I think I understand how to get info from the first sub to the second but I don't know how to pass info back.  Can anyone explain this to me?

 

4 REPLIES 4
Message 2 of 5

Subs, as you will find in any programming that use them, are there because they are NOT supposed to return a value. It's not their intent. They are supposed to be there to perform a task based upon the given inputs.

 

If you want to return a value, you need to use a Function. Which has all the same functionality of a sub, but can return a value.

 

HOWEVER, you can declare variables as shared/global so that a Sub can act on them... however, this can make it hard to follow the data around when it comes to debugging.

 

Example:

 

 

Sub Main
     Dim x As Integer = 5
     Dim y As Integer = 2

     Call MathSub(x,y)
     FunctionResult = MathFunction(x,y)
     MsgBox("This is in main sub. The result of the function call is: " & FunctionResult)
End Sub

'note the sub does not have a type
Sub MathSub(var1 as Integer, var2 as Integer)

         Dim resultvar As Integer
         resultvar = var1 + var2
         MsgBox("I am in the sub MathSub. The result I give is: " & resultvar)
End Sub

'note the function has to have a type
Function MathFunction(var1 as Integer, var2 as Integer) As Integer
       Dim result As Integer
       result =  var1-var2
       Return result
End Function

Example using global variables:

See: for more info on access modifiers:

https://msdn.microsoft.com/en-us/library/76453kax.aspx

 

'Need to put all of the rule inside of a class in order to be able to use the shared variable.
Class TestShared
	'This shared access modifier keyword makes the information accessible to all subs/functions/properties in this class
	Shared oResult As Integer
	
		Sub Main
			oResult = 0
			Dim x As Integer = 5
			Dim y As Integer = 2
			MsgBox("Before sub call result: " & vbLf & oResult)
			PerformMath(x, y)
			MsgBox("After sub call result: " & vbLf & oResult)
		End Sub
		
		'note the sub does not have a type
		Sub PerformMath(var1 as Integer, var2 as Integer)
				oResult = var1 + var2
		End Sub
End Class

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 5

Thanks for the help. I'm going to try applying what you've shown me when I'm back in the office tomorrow.  Hopefully I can make it work!

Message 4 of 5

Didn't get around to working on the iLogic code until today.  I was able to get things working using your example as a guide.  Thank you for the help!

Message 5 of 5

No problem! Good luck in your future code escapades!

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report