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 the name of a Sub statement

7 REPLIES 7
Reply
Message 1 of 8
Anonymous
441 Views, 7 Replies

Passing the name of a Sub statement

Hello, I am looking to pass the name of a the Sub statement, "Test2" to a Sub rule "Test" in the code below. However, I receive an error that otest () in Sub Test is not a method. Is this possible? If so could someone identify what I am missing? Thanks in advance.

 

Sub Main

otest = "Test2" 
Test(otest)

End Sub


Sub Test (otest)

otest ()


End Sub


Sub Test2 ()


MessageBox.Show("Message", "Title")


End Sub

 

7 REPLIES 7
Message 2 of 8
Anonymous
in reply to: Anonymous

If I understand correctly on what you are trying to do, this should do what you want or point you in the right direction. Hope this helps.

 

Dan V

Sub Main

otest = "Test2" 
Test(otest)

End Sub


Sub Test(ByVal PassedInValue As String)

MessageBox.Show(PassedInValue)


End Sub
Message 3 of 8
Anonymous
in reply to: Anonymous

Thank you for the reply. It worked for passing the argument to the message box , however I was looking to to see if the name of a Sub or Function can be passed from another Sub or Function.

As shown it looks strange, but this is just a simple test for a code that is looping through the parts in an assembly for replacement that has several sets of variables that provide several scenarios for a single function. Below I added more description on how it should function:

 

Sub Main

oTest = "Test2" 
'specifies the name of the rule I am going to call in Sub Test, "Test2"
Test(oTest)
'passes oTest value to Sub Test End Sub Sub Test (oTest) oTest ()
'Calls Sub Test2 to run End Sub Sub Test2 () MessageBox.Show("message", "Title")
'provides a message so that I can verify this method works End Sub

 

Message 4 of 8
Anonymous
in reply to: Anonymous

I understand now. I don't believe you can use a named argument as the function/method to call. In my years of development, I've never run across doing something like this where you want to take an argument and directly pass it as the function/method name. I would put in an if statement to check the value of the argument and then call the proper function/method.

 

With ilogic, if the Test2 sub can be another rule instead of a sub within the rule then you can use the iLogicVB.RunRule(PassedInValue).

 

DanV

 

Sub Main

otest = "Test2" 
Test(otest)

End Sub


Sub Test(ByVal PassedInValue As String)

If PassedInValue = "Test2" Then Call Test2


End Sub

Sub Test2()
	MessageBox.Show("test")
End Sub

 

Message 5 of 8
JamieVJohnson2
in reply to: Anonymous

You can actually.  It requires a level of coding that is 'one up' from iLogic, in the .Net environment in the type namespace.  VB.Net can run in the iLogic editor.

https://docs.microsoft.com/en-us/dotnet/api/system.type.invokemember?f1url=https%3A%2F%2Fmsdn.micros...

 

How deep down the rabbit hole do you wish to go?

 

jvj
Message 6 of 8
Anonymous
in reply to: JamieVJohnson2

I don't want to go too deep into the rabbit hole. Smiley LOL For what he wanted, there's other ways to do it.

 

hummm....I've only used InvokeMember with mouse or keyboard actions. But will have to look into it. Thanks!

Message 7 of 8
Anonymous
in reply to: Anonymous

Thank you all for the help. I think I am over complicating it, so I will move on to the next solution (no need to chase the rabbit).

Message 8 of 8
MjDeck
in reply to: Anonymous

You can do it with an Action delegate. This is simpler to use than Invoke. It doesn't use text strings for the Sub names, which could be a disadvantage. However, you can assign delegates to variables. Here's a sample.

Sub Main
Test(AddressOf Test1)
Dim delegate2 As Action = AddressOf Test2
Test(delegate2)
End Sub

Sub Test (oTest As Action)
oTest () 
End Sub

Sub Test1 ()
MessageBox.Show("Test1", "Title") 
End Sub

Sub Test2 ()
MessageBox.Show("Test2", "Title") 
End Sub

Mike Deck
Software Developer
Autodesk, Inc.

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

Post to forums  

Autodesk Design & Make Report