Unable to cast object of type 'System.String' to type 'Autodesk.iLogic.Types.ComponentArgument'.

Unable to cast object of type 'System.String' to type 'Autodesk.iLogic.Types.ComponentArgument'.

tomislav.peran
Advocate Advocate
620 Views
5 Replies
Message 1 of 6

Unable to cast object of type 'System.String' to type 'Autodesk.iLogic.Types.ComponentArgument'.

tomislav.peran
Advocate
Advocate

Hello,

 

I am adding some constraints with iLogic. However, what I do not understand is:

 

a) This does not work: 

Constraints.AddInsert("Part Insert:" & 1, "Part:"  & number_1, "Connection_A", "Part:" & number_2, "Connection_A", axesOpposed := True) 	

 The problem here is, I think, that I am using the string "Part:" and concatenating it with an integer ( & number_1) inside the method "AddInsert" when defining the component As ComponentArgument. 

 

I can use though:

"Part:1",  "Connection_A"

 

b) This work as well:

 

Dim oPart1 = "Part:" & 1
Dim oPart3 = "Part:" & 3

Constraints.AddInsert("Part Insert:" & 1, "oPart1, "Connection_A", oPart3,"Connection_B", axesOpposed := True)

Does anybody know why is first way wrong? What hinders this way of running method?

 

Tom

0 Likes
Accepted solutions (2)
621 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @tomislav.peran.  My guess is that it is just too much Type conversion going on where those two input variables are concerned.  Even when you create a variable, then set "Part:" & 1 as its value, you still did not declare the variable's Type to be String, and you are still forcing an Integer to be converted to a String, before it can be included into the other String, and form "Part:1".  Almost every type of object has the '.ToString' method, to help converting it to a String, but that was not used either.  You may just need to add an extra set of ( and ) characters around those internal data type conversions, because after that conversion to String, it then needs to be converted into a ComponentAsset object, because that is what the method wants.

IManagedConstraints.AddInsert Method 

ComponentArgument Class 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

tomislav.peran
Advocate
Advocate

Hi WCrihfield,

 

Yes, seems you are right. There is too much conversion inside the constraint method. 

I have just tested  ".ToString method" for another case just like b) case I described in my opening post which for some reason did not work and it works now:

 

Dim New_part = "Part:" & variable_number.ToString 

 Conclusion: It is best to avoid using conversions inside the methods. Then it is always working. 

 

Thanks a lot for the idea. 

Tom

 

0 Likes
Message 4 of 6

Frederick_Law
Mentor
Mentor
Accepted solution

@tomislav.peran wrote:

Conclusion: It is best to avoid using conversions inside the methods. Then it is always working. 

 


Conclusion: declare variable properly with correct type.

All type conversion slow down the program.

0 Likes
Message 5 of 6

tomislav.peran
Advocate
Advocate

Hi Frederick,

 

Oke, I will do it that way. 

 

Thanks 🙂

0 Likes
Message 6 of 6

Frederick_Law
Mentor
Mentor

VB allow declare without type which most other language  will not allow.

VB also auto cast (guess) type which will not work in other language.

This make converting and learning other language difficult.