Fastest way to get index of the choosen PartFeature in PartFeatures collection

Fastest way to get index of the choosen PartFeature in PartFeatures collection

Maxim-CADman77
Advisor Advisor
863 Views
2 Replies
Message 1 of 3

Fastest way to get index of the choosen PartFeature in PartFeatures collection

Maxim-CADman77
Advisor
Advisor

I'd like to know if there any faster way to get index of the given PartFeature in PartFeatures collection than cycling through it one-by-one?

Does LINQ have anything to suggest?

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Accepted solutions (1)
864 Views
2 Replies
Replies (2)
Message 2 of 3

DRoam
Mentor
Mentor

See this post from StackExchange: How to get index using LINQ? Qutoing the accepted answer:

 

An IEnumerable is not an ordered set.
Although most IEnumerables are ordered, some (such as Dictionary or HashSet) are not.

Therefore, LINQ does not have an IndexOf method.


Even if it did, it would not be any faster than iterating through the PartFeatures collection until you find your specific feature, because this is essentially what the "IndexOf" method does under the hood (see here).

 

Iterating until you find your feature will be just as fast as (or faster than) anything else. I would probably use a function that has the exact same implementation is the IndexOf method I linked to above:

 

 

Function IndexOfFeature(Features As PartFeatures, TargetFeature As PartFeature) As Integer
	For i As Integer = 1 To Features.Count
		If Features.Item(i) Is TargetFeature Then Return i
	Next
	Return -1
End Function

 

 

0 Likes
Message 3 of 3

DRoam
Mentor
Mentor
Accepted solution

Actually, you could simplify that a bit by making the function get the Features collection from the feature itself, rather than having to provide the collection as an argument:

 

Function IndexOfFeature(TargetFeature As PartFeature) As Integer
	Dim features As PartFeatures = TargetFeature.Parent.Features
	
	For i As Integer = 1 To features.Count
		If features.Item(i) Is TargetFeature Then Return i
	Next
	Return -1
End Function

 

0 Likes