Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Basic VB question

SometimesInventorMakesMeAngry
Advocate

Basic VB question

SometimesInventorMakesMeAngry
Advocate
Advocate

This is probably something very basic, but I would like to know the reason why the following does not work:

 

Sub Main()
    Dim oDoc as Inventor.Document = ThisApplication.ActiveDocument
    Dim sDispName as String = oDoc.DisplayName
    sDispName = "doc123"
End Sub

 

Whereas the following works fine:

 

Sub Main()
    Dim oDoc as Inventor.Document = ThisApplication.ActiveDocument
    oDoc.DisplayName = "doc123"
End Sub

 

 I'm guessing it has something to do with not referencing the object or something, but would like to be sure. If this is the case, when would I need to make sure I set a reference to an object with the various properties in the API? Is it only if the type of the property is not a built-in VB data type?

0 Likes
Reply
Accepted solutions (2)
491 Views
6 Replies
Replies (6)

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @SometimesInventorMakesMeAngry 

In your first example you're simply changing which string your variable stores, while in the second example you're changing the value of a property in an object.

 

Consider your variable oDoc for example:

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument

 

If you'd change that to another document object like oDoc = someotherdocument it would not change which document is active in Inventor. It would only change which document is stored in your variable oDoc.

In other words, that does not change the property ActiveDocument in the application object.

Same thing :slightly_smiling_face:

 

Both your examples work, they're just doing different things.

 

 

0 Likes

That makes sense. Is there a way to store 

ThisApplication.ActiveDocument.DisplayName

all in one variable so I can change the active document's display name like?

DocDisplayName = "doc123"

 

0 Likes

I really don't think so...

Hopefully someone here will correct me if im wrong.

0 Likes

Thank you!

0 Likes

DRoam
Mentor
Mentor
Accepted solution

To add to Jhoel's comments, what you're really dealing with here is the difference between Value types and Reference types. To quote the Microsoft documentation (emphasis mine):

 

Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable. With value types, each variable has its own copy of the data, and it is not possible for operations on one variable to affect the other

Strings are one of the Value types. Other Value types include all numeric types, Boolean, Char, and Date. Because the oDoc.DisplayName property returns a string, it's not possible to store the DisplayName property as a variable to be modified directly later. When you say Dim sDispName as String = oDoc.DisplayName, all this does is take the data that oDoc.DisplayName returns and store a copy of it in sDispName. There's not actually any connection (reference) between sDispName and oDoc.DisplayName.

 

This is in contrast to something like oDoc.PropertySets, which returns a PropertySets object, which is a reference type. So you can say Dim propSets As PropertySets = oDoc.PropertySets, and then later say propSets.Add("name") and it will actually add to oDoc's property sets, because propSets references (points to) the same object as oDoc.PropertySets.

 

But in order to actually modify the value of a Value-type property of an object, you have to invoke the object itself, as you do here:

 

 

Sub Main()
    Dim oDoc as Inventor.Document = ThisApplication.ActiveDocument
    oDoc.DisplayName = "doc123"
End Sub

 

 

So that is going to be the most succinct way to modify the DisplayName if you need to do it multiple times.

 

All that said, depending on what you're actually trying to do (I would assume this was just an example), there may be some tricks to make things more succinct, perhaps by writing a Function or even a custom Class. If you come across a real-life situation where it would be useful to have some kind of shortcut for setting the value of a value-type property, post it here and we'll see what we can come up with.

0 Likes

SometimesInventorMakesMeAngry
Advocate
Advocate

Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable. With value types, each variable has its own copy of the data, and it is not possible for operations on one variable to affect the other

That makes perfect sense. Thanks!

 

This must be related to why you can say ByRef or ByVal when declaring a function.

0 Likes