Get elements with certain condition from List(Of) - vb.net

I'm iterating through a List(Of MyClass) in order to find elements with certain conditions.
For example, in one case, I need to find all of these elements and do something with them:
For Each nCell As clsCell In colCell
If nCell.TempClickIndex = nCell.ClickIndex Then
If nCell.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE Then
I would like to know if there's any way to simplify this.
I'm dreaming of something like this:
For Each nCell As clsCell in colCell.GetSkypeCells()
The call "GetSkypeCells" would do just what I do above and would handle the selection internally.
Is there a way to do this?
Edit:
This is my colCell:
Public colCell As New clsCellListExtender.List(Of clsCell)
Imports System.Collections.ObjectModel
Public Class clsCellListExtender
Public Class List(Of T)
Inherits Collection(Of T)
Private _iID As Integer = 0
Private i As Integer = 0
Protected Overrides Sub InsertItem(index As Integer, item As T)
'your checks here
'i += 1
'If i > 20000 Then
' i = 0
'End If
Debug.Assert(g_bCheck = False)
If TypeOf (item) Is clsCell Then
_iID += 1
Dim nCell As clsCell = TryCast(item, clsCell)
nCell.TempID = _iID
End If
MyBase.InsertItem(index, item)
End Sub
End Class
End Class

You could use this:
For Each nCell as clsCell In colCell.Where(Function(x) x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE)
'Do stuff with nCell
Next
For your "dream" solution, you could add an extension method to whatever type colCell is that returns the result of the above LINQ.
Getting this to work with the nested class, and the generic type was a little tricky, but I finally got it.
Public Module Extensions
<System.Runtime.CompilerServices.Extension()> _
Public Function GetSkypeCells(Of T As clsCell)(colCell As clsCellListExtender.List(Of T)) As IEnumerable(Of T)
Return colCell.Where(Function(x) x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE)
End Function
End Module
Here is a small console application with a working extension method. I left the implementation blank to save space, but you should be able to fill it in from what is above. Just let me know if you have any issues.
Imports System.Collections.ObjectModel
Imports System.Runtime.CompilerServices
Module Module1
Sub Main()
Dim a As New clsCellListExtender.List(Of clsCell)
For Each cell As clsCell In a.GetSkypeCells()
'Do things with cell here
Next
End Sub
End Module
Public Class clsCellListExtender
Public Class List(Of T)
Inherits Collection(Of T)
Protected Overrides Sub InsertItem(index As Integer, item As T)
'...
End Sub
End Class
End Class
Public Class clsCell
'...
End Class
Module Extensions
<Extension>
Public Function GetSkypeCells(Of T As clsCell)(colCell As clsCellListExtender.List(Of T)) As IEnumerable(Of T)
Return colCell.Where(Function(x) x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE)
End Function
End Module

Try this:
For Each nCell As clsCell In colCell.FindAll(Function(c) c.TempClickIndex = c.ClickIndex And
c.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE)
Next
You can adapt this and create an extension-method, then you can call it with colCell.GetSkypeCells()
<Extension>
Public Function GetSkypeCells(c As List(Of clsCell)) As List(Of clsCell)
Return c.FindAll(Function(cc As clsCell) cc.TempClickIndex = cc.ClickIndex And
cc.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE)
End Function

You can use LINQ's extension method Where
Dim skypeCalls =
colCell.Where(Function(cell) cell.TempClickIndex = cell.ClickIndex).
.Where(Function(cell) cell.StandardCellType = eStandardCellType.SCT_SKYPEMESSAG)
For Each skypeCall in skypeCalls
' Do something
Next

Related

Diffrence between Collection<T> and custom implement class

Some people advice to inherit from Collection to get collection class. Some other people advice to have class and implement interfaces from scratch. I would like to understand when to use one over another.
I see that when i use:
class MyCollection
Inherits Collection(Of SomeObject)
I have possibility to add, insert, for each etc that's probably because Collection<T> uses internally List<T>
However if i just do:
class MyCollection : IList(Of SomeObject), IEnumerable<SomeObject>, IEnumerable(Of SomeObject)
myList As List(Of SomeObject)
i can also implement such things like Add(), Remove(), for each
Is it like it's good to use Collection(Of T) because it's already contains all of this implemented interfaces and inner List(Of T) rather that implementing all interfaces myself in self class from scratch? Is it the point people advice to inherit from COllection(Of T)?
EDIT (for further discussion):
Public Class Merge
Property Size As Integer
Property Datee As Date
Property Min As Integer
Property Max As Integer?
Property Value As Double
Public Sub New(min As Integer, max As Integer?, value As Integer)
Me.Min = min
Me.Max = max
Me.Value = value
End Sub
End Class
Public Enum SortCriteria
MinThenMax
MaxThenMin
End Enum
Public Class MergeComparer
Implements IComparer(Of Merge) 'do oddzielnej klasy sortowania obiektu jak tutaj potrzebujemy IComparer a nie IComparable (ten jest bezposrednio na klasie)
Public SortBy As SortCriteria = SortCriteria.MinThenMax
Public Function Compare(x As Merge, y As Merge) As Integer Implements IComparer(Of Merge).Compare
'to be implemented
End Function
End Class
Public Class MergeCollection
Inherits Collection(Of Merge)
Public SortBy As SortCriteria = SortCriteria.MinThenMax
''' <summary>
''' Ovveride because
''' There could be only one item on list which contains Max prop = Nothing
''' </summary>
''' <param name="index"></param>
''' <param name="item"></param>
Protected Overrides Sub InsertItem(index As Integer, item As Merge)
if IsNothing(item.Max)
If Items.Any(Function(myObject) IsNothing(Items.Max)) Then
Return
End If
End If
MyBase.InsertItem(index, item)
End Sub
Public Sub Sort()
Dim allItems = Items.ToArray()
Array.Sort(allItems)
For i = 0 To allItems.GetUpperBound(0)
Items(i) = allItems(i)
Next
End Sub
Public Sub Sort(comparison As Comparison(Of Merge))
Dim allItems = Items.ToArray()
Array.Sort(allItems, comparison)
For i = 0 To allItems.GetUpperBound(0)
Items(i) = allItems(i)
Next
End Sub
Public Sub Sort(comparer As IComparer(Of Merge))
Dim allItems = Items.ToArray()
Array.Sort(allItems, comparer)
For i = 0 To allItems.GetUpperBound(0)
Items(i) = allItems(i)
Next
End Sub
End Class
Here's an example of a custom collection that implements Collection(Of T) and then adds it's own Sort method:
Public Class StringCollection
Inherits Collection(Of String)
Public Sub Sort()
Dim allItems = Items.ToArray()
Array.Sort(allItems)
For i = 0 To allItems.GetUpperBound(0)
Items(i) = allItems(i)
Next
End Sub
End Class
Sample usage:
Dim strings As New StringCollection
strings.Add("Peter")
strings.Add("Paul")
strings.Add("Mary")
strings.Sort()
For Each s In strings
Console.WriteLine(s)
Next
That Sort method relies on the IComparable implementation of the items themselves. If you want to support other sorting methods or your items don't implement IComparable then you can implement different Sort methods, e.g.
Public Class StringCollection
Inherits Collection(Of String)
Public Sub Sort()
Dim allItems = Items.ToArray()
Array.Sort(allItems)
For i = 0 To allItems.GetUpperBound(0)
Items(i) = allItems(i)
Next
End Sub
Public Sub Sort(comparison As Comparison(Of String))
Dim allItems = Items.ToArray()
Array.Sort(allItems, comparison)
For i = 0 To allItems.GetUpperBound(0)
Items(i) = allItems(i)
Next
End Sub
End Class
Sample usage:
Dim strings As New StringCollection
strings.Add("Peter")
strings.Add("Paul")
strings.Add("Mary")
strings.Sort(Function(a, b) a.Length.CompareTo(b.Length))
For Each s In strings
Console.WriteLine(s)
Next
In that case, we're explicitly comparing the String items by their Length rather than their implicit "alphabetic" IComparable implementation.
You can include an overload of Sort for every relevant overload of Array.Sort and/or you can provide your own explicit comparisons if you want. Note that one of those overloads takes an IComparer. Here's how you might make use of that:
Public Class StringCollection
Inherits Collection(Of String)
Public Sub Sort()
Dim allItems = Items.ToArray()
Array.Sort(allItems)
For i = 0 To allItems.GetUpperBound(0)
Items(i) = allItems(i)
Next
End Sub
Public Sub Sort(comparison As Comparison(Of String))
Dim allItems = Items.ToArray()
Array.Sort(allItems, comparison)
For i = 0 To allItems.GetUpperBound(0)
Items(i) = allItems(i)
Next
End Sub
Public Sub Sort(comparer As IComparer(Of String))
Dim allItems = Items.ToArray()
Array.Sort(allItems, comparer)
For i = 0 To allItems.GetUpperBound(0)
Items(i) = allItems(i)
Next
End Sub
End Class
Here's an example comparer:
Public Class StringLengthComparer
Implements IComparer, IComparer(Of String)
Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
Return Compare(CStr(x), CStr(y))
End Function
Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare
Return x.Length.CompareTo(y.Length)
End Function
End Class
Sample usage:
Dim strings As New StringCollection
strings.Add("Peter")
strings.Add("Paul")
strings.Add("Mary")
Dim comparer As New StringLengthComparer
strings.Sort(comparer)
For Each s In strings
Console.WriteLine(s)
Next
We're sorting the items by their Length again but, this time, we're using an object that implements IComparer to make the comparisons rather than a Comparison(Of T) delegate. You should notice, though, that the Compare method of the IComparer in this last example takes pretty much the exact same form as the Lambda used to create the Comparison(Of T) delegate in the previous example.

List Of - Insert only if GUID doesn't exist

Before I add an item to my List (Of clsUser), I check if no clsUser with the same GUID exists in my list.
Currently I check for the existance like this:
Public Function GUIDExists(ByRef uList As List (Of clsUser), ByVal uGUID As String) As Boolean
For Each nItem As clsUser In uList
If nItem.GUID = uGUID Then
Return True
End If
Next
Return False
End Function
I would very much like simplify it and add this check to the List (Of clsUser) so that I don't have to write the same code over and over again.
Some like MyList.AddIfGUIDDoesntExists(nNewUser)
Is this possible?
If yes, could anybody tell me how this would be done?
Imports System.Runtime.CompilerServices
Public Module ExtensionMethods
<Extension()>
Public Sub AddIfGUIDDoesntExists(ByRef inputList As List(Of clsUser), _
ByVal item As clsUser)
Dim contains As Boolean = False
For Each i As clsUser In inputList
If (i.GUID = item.GUID) Then
contains = True
Exit For
End If
Next
If Not contains
inputList.Add(item)
End If
End Sub
End Module
Usage:
MyList.AddIfGUIDDoesntExists(nNewUser)

how to get the Index of object in collection

I'm trying to make a application, in this application I have a List(of T) collection that holds an object.
When processing the object I need to know it's Index from the list.
Example:
Public Class
Public oList as New List(of TestObject)
Private Sub Test()
Dim NewObject As New TestObject
oList.add(NewObject)
Index(NewObject)
End Sub
Private Sub Index(Byval TestObject As TestObject)
debug.print(Testobject.index)
End Sub
End Class
Is something like this possible? Ive seen it available in a reference file I used some time ago, but now I would like to make this available within my own class.
Can someone provide a sample?
PS: I know I can get the index using the List(Of T).IndexOf Method (T) but for future possibilities I would like to make the call from the object itself.
What usually happen is that they have a custom list, they don't directly used List(Of T) and store the list inside the object when they add that item to the list.
Module Module1
Sub Main()
Dim someList As New CustomList
someList.Add(New CustomItem())
someList.Add(New CustomItem())
someList.Add(New CustomItem())
Console.WriteLine(someList(1).Index)
Console.ReadLine()
End Sub
End Module
Class CustomItem
' Friend since we don't want anyone else to see/change it.
Friend IncludedInList As CustomList
Public ReadOnly Property Index
Get
If IncludedInList Is Nothing Then
Return -1
End If
Return IncludedInList.IndexOf(Me)
End Get
End Property
End Class
Class CustomList
Inherits System.Collections.ObjectModel.Collection(Of CustomItem)
Protected Overrides Sub InsertItem(index As Integer, item As CustomItem)
If item.IncludedInList IsNot Nothing Then
Throw New ArgumentException("Item already in a list")
End If
item.IncludedInList = Me
MyBase.InsertItem(index, item)
End Sub
Protected Overrides Sub RemoveItem(index As Integer)
Me(index).IncludedInList = Nothing
MyBase.RemoveItem(index)
End Sub
End Class
It looks like this
Public oList As New List(Of TestObject)
Private Sub Test()
Dim NewObject As New TestObject(oList.Count)
oList.add(NewObject)
End Sub
Public Class TestObject
Public index As Integer
Public Sub New(IndxOfObj As Integer)
Me.index = IndxOfObj
End Sub
End Class
If you necessarily need to have it as a property on the object I would suggest the following:
Public Class Main
Public oList As New List(Of TestObject)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim NewObject As New TestObject(Me)
oList.Add(NewObject)
Dim NewObject2 As New TestObject(Me)
oList.Add(NewObject2)
MsgBox(NewObject2.Index)
End Sub
Public Function Index(ByVal TestObject As TestObject) As Integer
Return oList.IndexOf(TestObject)
End Function
End Class
Public Class TestObject
Private _main As Main
Public ReadOnly Property Index() As Integer
Get
Return _main.Index(Me)
End Get
End Property
Public Sub New(RootClass As Main)
_main = RootClass
End Sub
End Class
If you happen to have the Main class as a Singleton you can skip the whole sending 'Me' into the constructor business. Then you can just call Main.Index without storing it as a property on all TestObjects.

A class can only be a template for a single object not a template for a collection

I have a simple class List.vb which is the following:
Public Class List
Public fList As List(Of Integer)
Public Sub New()
fList = New List(Of Integer)
fList.Add(1)
fList.Add(2)
fList.Add(3)
fList.Add(4)
fList.Add(5)
End Sub
End Class
The Console application is using this class like the following:
Module Module1
Sub Main()
Dim fObject As List = New List
Dim cnt As Integer = 0
For Each x As Integer In fObject.fList
Console.WriteLine("hello; {0}", fObject.fList.Item(cnt).ToString())
cnt = cnt + 1
Next
Console.WriteLine("press [enter] to exit")
Console.Read()
End Sub
End Module
Can I change the class code so that List.vb is a list(of integer) type?
This would mean that in the Console code I could replace In fObject.fList with just In fObject?
Or am I barking up the wrong tree - should classes be single objects and lists should be collections of classes ?
Yes, you can do that. In order for an object to be compatible with For Each, it must have a GetEnumerator function:
Public Function GetEnumerator() As IEnumerator _
Implements IEnumerable.GetEnumerator
Return New IntListEnum(fList)
End Function
The IntListEnum class must, in turn, implement IEnumerator, like this:
Public Class IntListEnum Implements IEnumerator
Private listInt As List(Of Integer)
Dim position As Integer = -1
Public Sub New(ByVal fList As List(Of Integer))
listInt = fList
End Sub
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
position = position + 1
Return (position < listInt.Count)
End Function
Public Sub Reset() Implements IEnumerator.Reset
position = -1
End Sub
Public ReadOnly Property Current() As Object Implements IEnumerator.Current
Get
Try
Return listInt(position)
Catch ex As IndexOutOfRangeException
Throw New InvalidOperationException()
End Try
End Get
End Property
End Class
Now you can make fList private, and iterate your List as follows:
For Each x As Integer In fObject
You can see a complete example here.
The answer that dasblinkenlight has provided is excellent, but if all you need is a list that of integers that is pre-populated, you can just inherit from List(Of Integer) and then have the class populate itself in the constructor:
Public Class List
Inherits List(Of Integer)
Public Sub New()
Add(1)
Add(2)
Add(3)
Add(4)
Add(5)
End Sub
End Class
When you inherit from List(Of Integer), your class automatically gets all of the functionality implemented by that type, so your class also becomes a list class that works the same way. Then, you can just use it like this:
Dim fObject As New List()
For Each x As Integer In fObject
Console.WriteLine("hello; {0}", x)
Next

Do local variables in shared method work like static variable in C?

Will the list in this shared method keep its state throughout the life of the method? Or will a new list be created every time this method is called?
Protected Shared Function newResxNodes(ByVal newName As String, ByVal newValue As String, Optional ByVal newComment As String = "") As List(Of ResXDataNode)
Dim newResxNodesList As List(Of ResXDataNode) = New List(Of ResXDataNode)
Dim newResxNode As ResXDataNode = New ResXDataNode(newName, newValue)
If newComment <> String.Empty Then
newResxNode.Comment = newComment
End If
newResxNodesList.Add(newResxNode)
Return newResxNodesList
End Function
No, It does not work like static variables in C. It will be a new list for every call. If you want to retain the list and list items, create a shared class field.
I've done a test and it returns 3 lines.
Module Module1
Class b
Public Sub New()
Console.WriteLine("New")
End Sub
End Class
Class a
Public Shared Sub Test()
Dim c As b = New b
End Sub
End Class
Sub Main()
a.Test()
a.Test()
a.Test()
Console.ReadLine()
End Sub
End Module