- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear Community,
I was developing a DLL interface when I encountered an unusual behavior for parameters storing Lists of Strings.
The assembly contains four parts. Each part contains custom materials.
The DLL (Windows Form) would receive these lists and allow the user to select the material for each part.
Once the unusual behavior occurred, I simplified the program so as not to use the DLL.
The program would initially get a custom library materials and store them in a List of Strings named, oAsmLibMat_List. I modified it here to use InventorMaterialLibrary.
'Declare string lists
Dim oAsmLibMat_List, oLib_List1, oLib_List2, oLib_List3, oLib_List4 As New Generic.List(Of String)
'Declare asset library
Dim oAssetLib As AssetLibrary
'Declare asset
Dim oAsset As Asset
'Loop thru asset libs looking for NMC
For Each oAssetLib In ThisApplication.AssetLibraries
'if NMC, get materials
'If oAssetLib.DisplayName = "NMC Materials" Then
If oAssetLib.DisplayName = "InventorMaterialLibrary" Then
'Declare a material asset
Dim material As MaterialAsset
'Loop to get each material
For Each material In oAssetLib.MaterialAssets
'Add to string list
oAsmLibMat_List.Add(material.DisplayName)
Next
End If
Next
' Display string list at top of program
MessageBox.Show("Top : oAsmLibMat_List =" & vblf & vbLf & String.Join(", ", oAsmLibMat_List.ToArray()), "DEBUG", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
For each of the four parts, I attempt to use a function to get the custom materials and then store respectively in List Strings named, oLib_List1, oLib_List2, etc.
oLib_List1 = GetPartDocMatls(oPartDoc, oAsmLibMat_List)
oLib_List2 = GetPartDocMatls(oPartDoc, oAsmLibMat_List)
oLib_List3 = GetPartDocMatls(oPartDoc, oAsmLibMat_List)
oLib_List4 = GetPartDocMatls(oPartDoc, oAsmLibMat_List)
I pass to the function the part document and the assembly material list. I use this so as not to add an existing assembly material to the part's list of custom materials.
Private Function GetPartDocMatls(LocPartDoc As PartDocument, MatList As Generic.List(Of String))
Dim TempMatList As Generic.List(Of String) = MatList
Dim oPartAsset As Asset
For Each oPartAsset In LocPartDoc.MaterialAssets
'Add material to list if it is not already in the list
If Not TempMatList.Contains(oPartAsset.DisplayName) Then
TempMatList.Add(oPartAsset.DisplayName)
End If
Next
Return TempMatList
End Function
I don't understand why the passed (oAsmLibMat_List) List of Strings is getting changed. I have tried with and without ByVal declarations.
At the end of the first function call, I expected only oLib_List1 to contain the assembly materials and the first part's custom materials.
However, the oAsmLibMat_List and oLib_List1 both contain the custom materials from part 1.
Because both lists change, the next call to the function to get custom materials from part 2, contains the custom materials from part 1.
This continues for parts 3 and 4.
At the end of the program, all the lists are identical. They contain the assembly materials and the custom materials from all four parts.
I thought I understood how functions, arguments, and returns behaved, but this has me confused.
I would welcome some insight and clarity.
Once I get this resolved, I can return to the task of building the DLL.
Thank you for your time and attention. I will attach the model once the forums are repaired.
See Rule_1 in the assembly.
Regards,
Jerry
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Solved! Go to Solution.