How to use "Generic List" functions in iLogic rule

How to use "Generic List" functions in iLogic rule

DRoam
Mentor Mentor
864 Views
4 Replies
Message 1 of 5

How to use "Generic List" functions in iLogic rule

DRoam
Mentor
Mentor

I've been using VB "generic lists" very successfully for working with dynamic lists in my iLogic rules. The built-in functions like "Sort", "Contains", "Count", etc. are very handy. However, there are a couple of functions I really want to use, but I can't figure out how. Namely, the "Count(Of ...)" and "RemoveAll" functions.

 

See the script below for an example of how I'd like to use these functions:

 

Dim oStringList As New List(Of String)

oStringList.Add("Dog")
oStringList.Add("Cat")
oStringList.Add("Dog")
oStringList.Add("Fish")
oStringList.Add("Dog")
oStringList.Add("Fish")

oDogCount = oStringList.Count(Of "Dog") 'Should equal 3

oStringList.RemoveAll("Dog") 'List should then contain Cat, Fish, and Fish

I can't find any documentation whatsoever on the "Count(Of ...)" function, or any other built-in function for counting the occurrences of a specific item in my list.

 

There's a very nice example in the .NET documentation (here) demonstrating how to use "RemoveAll", but I can't figure out how to implement what they've done into my iLogic code.

 

Of course I can accomplish these things with for loops, but it'd be great if I could take advantage of the built-in functions like I can for "Sort", "Contains", "Count", etc.

 

Any advice on how to use these in an iLogic rule would be greatly appreciated.

0 Likes
865 Views
4 Replies
Replies (4)
Message 2 of 5

JamieVJohnson2
Collaborator
Collaborator

When using generics that ask for Predicate(t) what its saying is 'give me a function to do this'  you can write the function inline (small simple functions) or reference delegate to a separate addressed function.

 

Example:

dim frogs as  new list(of frogs)

frogs.Add(new frog(red))

frogs.Add(new frog(reddish))

frogs.Add(new frog(red like))

frogs.Add(new frog(green))

frogs.RemoveAll(function(x) x.color.contains("red"))'inline function declares x as type of frog (the of T from the list), and allows me to access its properties, and perform a function that assumes the return type of Boolean.

would remove all frogs from list with color that had red in its text.

list(of frogs)

 

For a more detailed study check this out:

https://app.pluralsight.com/library/courses/vb-generics/table-of-contents

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
Message 3 of 5

DRoam
Mentor
Mentor

Thanks for the reply, @JamieVJohnson2! I don't fully understand the use of an inline function and the proper syntax for that, but I managed to get it to work with my example by using the following:

 

oStringList.RemoveAll(Function(x) x = "Dog")

I'm having a strange side-effect, though. When I use an inline function like that, the iLogic editor thinks I've started a new function and begins wrapping together everything after my inline function. See the image below:

 

Inline Function Wrapping.png

 

Is there any way to tell the editor that the "function" is over so it stops wrapping? I tried just typing "End Function" and it didn't like that. I think the compiler knows that the function is over and done with, but for some reason the editor doesn't.

 

And one more question. I tried doing the same thing with the "Count (Of ...)" function, and it didn't work. I tried it two different ways...:

 

oDogCount = oStringList.Count(Function(x) x = "Dog")
oDogCount = oStringList.Count(Of Function(x) x = "Dog")

...and neither way works. What's the proper syntax here?

0 Likes
Message 4 of 5

JamieVJohnson2
Collaborator
Collaborator

Another method of inline function is to add a return statement and end function statement.

        SiteSchedule.Sort(Function(x, y)
                              Dim cmp As Integer = x.StartDate.CompareTo(y.StartDate)
                              If cmp = 0 Then
                                  cmp = x.CycleTypeID.CompareTo(y.CycleTypeID)
                                  If cmp = 0 Then
                                      cmp = x.ID.CompareTo(y.ID)
                                  End If
                              End If
                              Return cmp
                          End Function)

maybe if it see's the End Function it will behave.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 5 of 5

AlexFielder
Advisor
Advisor

Whenever I'm working with the System Generic Lists object, I always include this at the top of the rule:

 

Imports System.Collections.Generic

perhaps that is what you're missing?

0 Likes