A class can only be a template for a single object not a template for a collection - vb.net

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

Related

How do I construct an "Add" routine for a custom object list?

I feel really stupid asking this question, but here goes...
I'm trying to create a custom object in VB, that is itself a list (or collection, or "tuple" - I'm not sure what the difference between these is) of custom objects, and I need to create routines to add and remove these secondary objects to/from the larger custom object. So far, my code goes something like this:
Public Class parameterSet
Friend _xParameter As String
Public Property xParameter() As String
Get
Return _xParameter
End Get
Set(value As String)
_xParameter = value
End Set
End Property
Friend _yParameter As String
Public Property yParameter() As String
Get
Return _yParameter
End Get
Set(value As String)
_yParameter = value
End Set
End Property
Friend _zParameter As String
Public Property zParameter() As String
Get
Return _zParameter
End Get
Set(value As String)
_zParameter = value
End Set
End Property
Public Sub New(ByVal xParameter As String, ByVal yParameter As String, ByVal zParameter As String)
_xParameter = xParameter
_yParameter = yParameter
_zParameter = zParameter
End Sub
End Class
Public Class parameterCollection
Friend _parameterCollection As New List(Of parameterSet)
Friend Sub Add(xParameter As String, yParameter As String, zParameter As String)
Throw New NotImplementedException()
End Sub
End Class
What do I have to put in the Add routine to make this work?
Your first class ought to look like this:
Public Class ParameterSet
Public Property X As String
Public Property Y As String
Public Property Z As String
Public Sub New(x As String, y As String, z As String)
Me.X = x
Me.Y = y
Me.Z = z
End Sub
End Class
Your second class ought to look like this:
Imports System.Collections.ObjectModel
Public Class ParameterSetCollection
Inherits Collection(Of ParameterSet)
Public Overloads Sub Add(x As String, y As String, z As String)
Add(New ParameterSet(x, y, z))
End Sub
End Class
You might even want to do this:
Imports System.Collections.ObjectModel
Public Class ParameterSetCollection
Inherits Collection(Of ParameterSet)
Public Overloads Function Add(x As String, y As String, z As String) As ParameterSet
Dim item = New ParameterSet(x, y, z)
Add(item)
Return item
End Function
End Class
The Collection(Of T) class already provides all the standard collection functionality and you can extend it as required.

Get elements with certain condition from List(Of)

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

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.

How should I store Child Classes in an enumerable and how to access them correctly?

Public Class UniqueList(Of T As BaseClass)
Inherits List(Of T)
Public Overridable Overloads Sub Add(value As T)
If Not Me.Contains(value) Then MyBase.Add(value)
End Sub
Public Function [Get](val As integer) As T
Return Me.Where(Function(cb) cb.Id = val)(0)
End Function
End Class
I get en error if I try to use it without casting it first, so I I cast it before trying to get my object :
dim mylist as new UniqueList(of Baseclass)
mylist.add(new ChildClass(1))
dim x as ChildClass= (mylist.get(1))
x.RandomMethod() ...
so I tried to create a new function in my UniqueList class that would cast it for me :
Public Function [GetChildClass](val As integer) As ChildClass
Return DirectCast(Me.Where(Function(cb) cb.Id = val)(0), ChildClass)
End Function
but I always get errors saying value T cannot be converted to ChildClass... is there any way to have this function returns me the correct object ?
edit: I cant change it to a list of ChildClass
...
edit : declare them however you want ...
Class BaseClass
public id as integer
public sub new(id as integer)
me.id = id
end sub
end class
Class ChildClass
inherits BaseClass
Public sub New(id as integer)
mybase.new(id)
end sub
public sub randomMethod()
'do nothing
end sub
end class
You may be looking for something like this:
Public Function [GetParent](val As integer) As ChildClass
Return DirectCast(Me.OfType(Of ChildClass).
Where(Function(cb) cb.id = val)(0), ChildClass)
End Function
Note - this will fail if no elements of type ChildClass are found, you can prevent it from failing by using .FirstOrDefault instead of manually indexing (0) into the first element. FirstOrDefault will return Nothing if no elements were found.

Incompatible interface

Please see the code below:
Public Class clsCar
Implements IVehicle
Public Function getWheels() As Integer Implements IVehicle.getWheels
Return 4
End Function
End Class
Public Interface IVehicle
Function getWheels() As Integer
End Interface
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim list As List(Of IVehicle) = New List(Of IVehicle)
Dim v1 As IVehicle = New clsCar
Dim v2 As IVehicle = New clsBus
list.Add(v1)
list.Add(v2)
Dim IVehicle As IVehicle = New clsCar
Dim IVehicle2 As IVehicle = New clsBus
For Each IVehicle In list
MsgBox(IVehicle.getWheels())
Next
End Sub
End Class
I want to add a new function to the clsBus class:
Public Function getWheels(ByVal strCarType As String) As Integer
If strCarType = "Robin Ryliant" Then
Return 3
Else
Return 4
End If
End Function
How do I call this from the FOR EACH statement? At the moment it will call getWheels with no arguments.
You will have to add the method overload to the interface in order to be able to call it from a IVehicle variable.
Public Interface IVehicle
Function getWheels() As Integer
Function getWheels(ByVal strCarType As String) As Integer
End Interface
But probably it is better to have different, more specialized car types
Public Class clsCar
Implements IVehicle
Public Overridable Function getWheels() As Integer Implements IVehicle.getWheels
Return 4
End Function
End Class
Public Class clsRobinRyliantCar
Inherits clsCar
Public Overrides Function getWheels() As Integer
Return 3
End Function
End Class
This does not break the inheritance hierarchy and is purely polymorphic.
I think I would go for something more like this, with the number of wheels being an instance property (code in LINQPad format):
Sub Main
Dim list As List(Of IVehicle) = New List(Of IVehicle)()
list.Add(New clsCar("Ford Focus", 4))
list.Add(New clsCar("Robin Ryliant", 3))
list.Add(New clsBus())
For Each v In list
Console.WriteLine(String.Format("{0}:{1}", v.GetVehicleType(), v.GetWheels()))
Next
End Sub
' Define other methods and classes here
Public Interface IVehicle
Function GetVehicleType() As String
Function GetWheels() As Integer
End Interface
Public MustInherit Class clsVehicle
Implements IVehicle
Protected Property VehicleType As String
Protected Property Wheels As Integer
Protected Sub New(vehicleType As String, wheels As Integer)
Me.VehicleType = vehicleType
Me.Wheels = wheels
End Sub
Public Function GetVehicleType() As String Implements IVehicle.GetVehicleType
Return Me.VehicleType
End Function
Public Function GetWheels() As Integer Implements IVehicle.GetWheels
Return Me.Wheels
End Function
End Class
Public Class clsCar
Inherits clsVehicle
Public Sub New(vehicleType As String, wheels As Integer)
MyBase.New(vehicleType, wheels)
End Sub
End Class
Public Class clsBus
Inherits clsVehicle
Public Sub New()
MyBase.New("Bus", 4)
End Sub
End Class
If your clsBus and clsCar are meant to refer to a specific car then the type should be a member of that class already, not something you pass in when you want to get information. To this end I'd suggest that you have "type" as something you can pass in the constructor and then the method on the bus would have no parameters and would just refer to its internal type to determine how many wheels it has.
I'm not too fluent with VB.NET so would probably make mistakes in example code but hopefully you get what I mean. If not I'll knock up some code. :)