Creating ArrayList with progresive ArrayListName in a loop

Creating ArrayList with progresive ArrayListName in a loop

jake_pietrzak
Contributor Contributor
815 Views
9 Replies
Message 1 of 10

Creating ArrayList with progresive ArrayListName in a loop

jake_pietrzak
Contributor
Contributor

Hi All,

 

Does anyone can help me with mentioned issue?

I want to create a set of ArrayList that will have a variable names.

I have something like that in mind:

 

i=0
For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments
	i=+1
	Dim MyArrayList + i As New ArrayList
Next

 

 Of course this code doesn't work, but I hope it will give you the idea of what I have in mind.

 

Thanks a lot!

0 Likes
Accepted solutions (2)
816 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hi @jake_pietrzak.  What are you planning on putting into the ArrayList, and how do you intent to use the data you put into it?  If there is just one ArrayList per referenced document, then you do not need to assign a unique name to each ArrayList, because it will only be used within that one iteration, then it will be reset for the next iteration.  If you intend to hold onto data in a collection after the loop of referenced documents, then you would have to create the collection before the loop starts, that way it will still be recognized and usable after the loop has ended.  I also recommend using something else besides an ArrayList, because Microsoft also discourages its use in new development, in favor of the List(Of ) (even if it is List(Of Object)).  If you need to gather multiple pieces of data from every referenced document, you could use a List within a List like List(Of List(Of Object)).

Below is an example of that type of scenario.

Dim oDoc As Inventor.Document = ThisDoc.Document
Dim oListOfLists As New List(Of List(Of Object))
If oDoc.AllReferencedDocuments.Count > 0 Then
	For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
		Dim oList As New List(Of Object)
		oList.Add("DisplayName = " & oRefDoc.DisplayName)
		oList.Add("DocumentType = " & oRefDoc.DocumentType.ToString)
		oList.Add("IsModifiable = " & oRefDoc.IsModifiable)
		oListOfLists.Add(oList)
	Next oRefDoc
End If
If oListOfLists.Count > 0 Then
	For Each oList In oListOfLists
		If oList.Count > 0 Then
			For Each oEntry In oList
				Logger.Info(oEntry.ToString)
			Next oEntry
		End If		
	Next oList
End If

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 10

jake_pietrzak
Contributor
Contributor

Thanks a lot @WCrihfield

That was really helpful, not with just this issue.

but I have another one;)

 

Let's say I have 3 lists in oListOfLists

list1 contains list of entries:

Param1

Param2

Param3

 

List 2:

Param1

Param3

Param4

 

List 3:

Param1

Param3

Param4

Param5

 

I want to compare all lists (list, list2, list3) and create a new one (NewList), that will contain just shared parameters, in this case:

Param1

Param3

 

How can I do that?

Thanks in advance 😉

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor

Hi @jake_pietrzak.  First of all, what type of data / object do those list entries represent?  Are they just Strings (text, like just the names of parameters), or are they numerical values (the numerical values obtained from parameters), or actual parameter objects themselves, or something else.  Knowing what type of data / objects they are is usually important to knowing how to compare them for equality.  There are built-in methods for comparing two lists with each other, but I am not aware of any built-in methods for comparing more than two lists with each other.  Also, should the new list only contain entries that were present in all lists, or should it also contain entries that were present in more than one list, but not in all lists.  I know this simple example does not have any values like that, but just to clarify, in case we need to create a custom code routine to do the comparison.  If we did create a custom routine to figure this out, it would be awkward, because within each iteration of the overall list of lists, would need to be working with exactly two lists at a time, to compare those two (not just one list, or more than two lists).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

Stakin
Collaborator
Collaborator
Dim oNewList As list(of object)
For each oEntity in olistofList.item(0)
  if  olistofList.item(1).exist(oEntity) and olistofList.item(2).exist(oEntity) then
     oNewList.add(oEntity)
  end if
Next
0 Likes
Message 6 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hi guys.  I was talking about using the 'Intersect' method, but as I mentioned, it is essentially designed for comparing 2 arrays / lists, instead of 3 or more.  However, these can be 'stacked' to add more arrays / lists into the comparison, but only if you have access to all of them directly, and not just iterating through them one by one.  This method creates a new IEnumerable(Of ) type object each time it is called, but the result of one can be compared to the next, if that type of comparison procedure will work OK here.  Below is an example of what I am talking about.  However the problem with this example is that we are not really utilizing the List(Of List(Of )) object here in the end.  We are accessing the individual List objects by their individual variables.  Also, keep in mind that a List object is vb.net based, so its entries are zero based index (first item / element is at index of zero, not one).

 

This code outputs the correct two values to the iLogic Log window, but the end process needs to be able to work with the List of Lists somehow, which is awkward, since normal iterations only work on one instance at a time, not two at a time, and collections can have uneven numbers of entries, so some testing must be included to make sure the next element is actually there before comparison can be done.

Dim oListOfLists As New List(Of List(Of String))
Dim oList1 As New List(Of String)
oList1.Add("Param1")
oList1.Add("Param2")
oList1.Add("Param3")

oListOfLists.Add(oList1)

Dim oList2 As New List(Of String)
oList2.Add("Param1")
oList2.Add("Param3")
oList2.Add("Param4")

oListOfLists.Add(oList2)

Dim oList3 As New List(Of String)
oList3.Add("Param1")
oList3.Add("Param3")
oList3.Add("Param4")
oList3.Add("Param5")

oListOfLists.Add(oList3)

Dim oNewCommonList As List(Of String) = oList1.Intersect(oList2).Intersect(oList3).ToList
If oNewCommonList.Count > 0 Then
	For Each sEntry In oNewCommonList
		Logger.Info(sEntry)
	Next sEntry
End If

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 10

WCrihfield
Mentor
Mentor

OK @Stakin I see your intent now.  At first, I thought you were attempting to use the Exists method, which did not look correct to me.  In your code it was spelled "Exist" instead of "Exists", and did not include the Function(s) predicate codes, but then I realized you may be talking about the Contains() method, which is much simpler (no predicate Function involved).  That might work OK if again we had access to all Lists at the same time, so we could include a comparison to each one in one line of code like that.  But if we did not know the number of Lists, we may be able to use Count or Length, then use variables for them, one way or another.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 10

Stakin
Collaborator
Collaborator

https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-8.0

Hi,The Exists is an  Method of List<T> Class.

But in this case,Use the Extension Methods of Intersect is better.

Learnt,thank you!

0 Likes
Message 9 of 10

WCrihfield
Mentor
Mentor

Hi @jake_pietrzak.  I may have come up with something that will work to create a new List containing only the values that are common within all the Lists in a List Of Lists, when the total number of Lists is varying / unknown.  But it is likely more complicated than you were hoping for.  I have attached two text files, each containing the same starting code, where the 3 original lists, and the List of Lists are being created, but with slightly different versions of a block of code following that part, where it inspects all the Lists within the List of Lists, and creates a new List containing only the common values.  Again, I am using List(Of String) in these examples, because that is a common type of value, since I still do not know what types of entries/values you will actually be working with here.  There are likely more efficient ways of figuring something like this out, but I only had a certain amount of '~free~' time (while at work) to work on these, while keeping up with everything else.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 10

jake_pietrzak
Contributor
Contributor

Hi @WCrihfield ,

Thanks a lot again for all your help and suggestions.

At the end I came up with below code, that works great for.

@Stakin I tried at the beginning your method, but for some reason I had a variables class error.  

Anyway, big thank you to both of you!

 

 

'[ CREATING LIST OF COMMON PARAMETERS
	If oListOfLists.Count > 0 Then
		Dim oList1 As List(Of String) = oListOfLists.Item(0)
		Dim oList2 As List(Of String) = oListOfLists.Item(1)
		Dim oNewList As List(Of String) = oList1.Intersect(oList2).ToList
		For n = 2 To oListOfLists.Count
			For Each oEntry As Object In oNewList
				oList1 = oNewList
				oList2 = oListOfLists.Item(n-1)
		 		oNewList = oList1.Intersect(oList2).ToList
			Next
		Next
		Dim asmDocUserParameters As UserParameters = asmDoc.ComponentDefinition.Parameters.UserParameters
		Dim checkParam As UserParameter = Nothing
		Try
			checkParam = asmDocUserParameters.Item("SharedParameters")
		Catch ex As Exception
			checkParam = Nothing
		End Try
		If checkParam Is Nothing Then
			asmDocUserParameters.AddByValue("SharedParameters", "Type Here", UnitsTypeEnum.kTextUnits)
			MultiValue.List("SharedParameters") = oNewList
		Else
			MultiValue.List("SharedParameters") = oNewList
		End If
	End If

 

 

 

0 Likes