Extension Method for List(Of Object) in VB - vb.net

I have an extension method that works great in converting an object to my form object.
'Copy an object to a form object
<Extension()> _
Public Function FromModel(ToObject As BaseFormObject, ByRef FromObject As Object) As Boolean
ToObject = FromObject
Return True
End Function
I want to do the same thing for a list of objects.
'Copy a list of objects to form objects
<Extension()> _
Public Function FromModelList(ToList As List(Of BaseFormObject), ByRef FromList As List(Of Object)) As Boolean
For Each FromItem As Object In FromList
'Create a new BaseFormObject for every item in FromList, Add it to our From List
Dim newFormObject = New BaseFormObject()
newFormObject.FromModel(FromItem)
ToList.Add(newFormObject)
Next
Return True
End Function
Creating a new List(Of BaseFormObject) does not give me access to the method "FromModelList." What is the proper way to go about doing this?

Your code works fine.
I created this code:
Dim lst As New List(Of BaseFormObject)
I got the extension member when I typed in lst.
I'm going to go out on a limb here and suggest that you're not actually instantiating a List(Of BaseFormObject), but instead something like List(Of ActualFormObject) where ActualFormObject inherits from BaseFormObject.
So if I use this code:
Dim lst As New List(Of ActualFormObject)
Then you don't get the extension member. If that's what you're hoping for, then it's easy to fix.
Change your code to this:
<Extension()> _
Public Function FromModelList(Of T As {New, BaseFormObject})(ToList As List(Of T), ByRef FromList As List(Of Object)) As Boolean
For Each FromItem As Object In FromList
'Create a new BaseFormObject for every item in FromList, Add it to our From List
Dim newFormObject = New T()
newFormObject.FromModel(FromItem)
ToList.Add(newFormObject)
Next
Return True
End Function
Then you get the extension member on lists of sub-classes.

Related

Cannot pass List of subtype to a List of interface type (implemented by subtype) to a function in Visual Basic + Cannot be casted either

I need to use only 1 function to process a list containing interface types so that I can reuse it for each subtype, but I also need to know the concrete type when handling the list-items higher up the function call stack:
LoadAItems calls GetA which calls FilterItems.
FilterItems needs to be generic so that GetB can also call and make use of it.
The problem is that trying to pass the list of subtypes to the general FilterItems method is not allowed:
"Cannot convert type 'List(Of AListItem)' to parameter type 'List(Of IListItem)'"
I tried converting each AListItem object in the list to IListItem and adding it to a new list but the problem is that the FilterItems function is supposed to remove elements from the list. If I remove elements from the new list then it will not affect the old list. I could convert it back but this is a lot of hassle just to be able to use the function.
I cannot just change everything to List(Of IListItem) because then I would need to always cast down the returned value from either FilterItems or GetA / GetB because LoadAItems / LoadBItems needs to know the concrete type.
I can see why casting down is bad, but why can't I cast up to the interface type the concrete types are implementing?
I have already tried:
FilterItems(CType(items, List(Of IListItem))
but this is not allowed:
"Value of type 'List(Of AListItem)' cannot be converted to 'List(Of IListItem)'"
Here is my code example:
Public Class AListItem
Implements IListItem
'Properties here
End Class
Public Class BListItem
Implements IListItem
'Properties here
End Class
Private Sub FilterItems(items As List(Of IListItem))
'Remove items from the list that meet some condition
items.RemoveAll(Function(item) ...)
'Does not matter what the items class type is
End Sub
Public Function GetA() As List(Of AListItem)
Dim items As List(Of AListItem)
items = CallDatabase()
FilterItems(items) ' Does not allow!
Return items
End Function
Public Function GetB() As List(Of BListItem)
Dim items As List(Of BListItem)
items = CallDatabase()
FilterItems(items) ' Does not allow!
Return items
End Function
Public Sub LoadAItems()
Dim items As List(Of AListItem)
items = GetA()
'Do specific AListItem stuff (cannot use interface!)
End Sub
Public Sub LoadBItems()
Dim items As List(Of BListItem)
items = GetB()
'Do specific BListItem stuff (cannot use interface!)
End Sub
If I correctly understand what you're trying to do you should be able to make the function itself generic:
Private Sub FilterItems(Of T As IListItem)(items As List(Of T))
Of T As IListItem adds a constraint that T must be, or inherit from IListItem.
Then you can call it like:
Public Function GetA() As List(Of AListItem)
Dim items As List(Of AListItem)
items = CallDatabase()
FilterItems(Of AListItem)(items)
Return items
End Function
Public Function GetB() As List(Of BListItem)
Dim items As List(Of BListItem)
items = CallDatabase()
FilterItems(Of BListItem)(items)
Return items
End Function
Rather than having a function that takes and manipulates a List(Of IListItem), instead create a function with a signature of:
Private Boolean IsGoodItem(IListItem)
With a hopefully straightforward conversion of your existing code within FilterItems to fit there. Then just change:
Public Function GetA() As List(Of AListItem)
Dim items As List(Of AListItem)
items = CallDatabase()
FilterItems(items) ' Does not allow!
Return items
End Function
To
Public Function GetA() As List(Of AListItem)
Dim items As List(Of AListItem)
items = CallDatabase()
Return items.Where(IsGoodItem).ToList()
End Function
And you still get (a fair bit of) code reuse without stumbling over covariance/contravariance issues. (I think the compiler will be happy with inferring types for the Where or you may have to insert explicit type annotations here)

Is there a handy way to pass function arguments to initialize a Dictionary(of string,string) in Vb.net

We can do this in vb.net:
Dim d= new Dictionary(of string, string) from {{"a","valA"},{"b","valB"}}
Please how can we make the following possible for convenience:
public sub Setup(args)
Dim d= new Dictionary(of string, string) from args
end sub
Thanks.
No, you can't initialize a dictionary with collection initializer syntax from a variable or parameter. It's just syntactic sugar that the compiler eats if you assign it in the way you've shown in the first snippet.
However, you can pass an IDictionary(Of String, String) and use that for the constructor of the dictionary:
Public sub Setup(dict As IDictionary(Of String, String))
Dim d = new Dictionary(Of String, String)( dict )
End Sub
Or you can pass an IEnumerable(Of KeyValuePair(Of String, String)) and use that for args.ToDictionary(Function(kv) kv):
Public sub Setup(keyVals As IEnumerable(Of KeyValuePair(Of String, String)))
Dim d = args.ToDictionary(Function(kv) kv)
End Sub
The former approach is more efficient, the latter is more general. Allowing IDictionary(Of TKey, TValue) has the advantage that you can pass more types since there are many classes that implement that interface.

Casting substrings with linq into a list of object and than sorting it base on property in vb.net

This have to be in vb.net linq, i'm pretty sure I could do it in c#, but I cant find any good enough translator to help me ... even the answers I find here in SO seems to only be written in linq, hence the question which might be a duplicate of a c# one.
That being said, considering these 2 classes :
Public class User
Public Property Name() As String
Public Property Teams As TeamList
Public sub New(d as string, results as TeamList)
me.name = d
me.Teams = results
end sub
end class
Public Class TeamList
Public Property TeamName() As String
Public Property fullscore() As list(of object)
Public sub New(name as string, value as list(of string))
me.TeamName = name
me.fullscore = value
me.fullscore = getFullScore(value) (return a list of object)
end sub
End Class
I'm struggling in the final steps of my linq -to - object : (you can copy /paste this in linqpad)
Sub Main
dim Definition as new Dictionary(of String, object)
definition.add("user1_redTeam-02", new object)
definition.add("user1_redTeam-01", new object)
definition.add("user1_blueTeam-03", new object)
definition.add("user2_redTeam-01", new object)
definition.add("user1_redTeam-03", new object)
definition.add("user1_blueTeam-01",new object)
definition.add("user2_blueTeam-01", new object)
definition.add("user1_blueTeam-02", new object)
definition.add("user2_redTeam-02", new object)
Dim q3 = (From userlists In Definition.Keys.GroupBy(Function(s) s.Split("_")(0)) _
Select New With _
{.UserName = userlists.Key, _
.animationList = (From scList In userlists.GroupBy(Of String)(Function(s) s.Split("-")(0)) _
Select New With {.Team = scList.Key, _
.Score = scList.ToList()})})
q3.dump()
End Sub
this is the result :
now, all I want is to sort the .score attribute (just a simple .sort(), and instead of returning an anonymous q3 object, which I,m cluless to transform, I'd like the q3 to be a list(of User)
it think it should looks like this ... but I cant make it works, i always gets some linq conversion errors :
Unable to cast object of type 'WhereSelectEnumerableIterator2[System.Linq.IGrouping2[System.String,System.String],UserQuery+User]' to type 'System.Collections.Generic.List`1[UserQuery+User]'.
Dim q3 as List(of User)= (From userlists In Definition.Keys.GroupBy(Function(s) s.Split("_")(0)) _
Select New User(userlists.Key, (From scList In userlists.GroupBy(Of String)(Function(s) s.Split("-")(0)) _
Select New TeamList(scList.Key, scList.ToList()))))
Your code examples seem to be incorrect - for example, it seems like User.Teams should be a list of some type, not a TeamList object, which isn't really a list. Anyway, with a little modification, this is what I came up with - maybe it's close to what you were looking for (a list of users with the scores sorted). You can paste into LINQPad to run it.
Sub Main
Dim Definition As New Dictionary(of String, Object)
definition.add("user1_redTeam-02", New Object)
definition.add("user1_redTeam-01", New Object)
definition.add("user1_blueTeam-03", New Object)
definition.add("user2_redTeam-01", New Object)
definition.add("user1_redTeam-03", New Object)
definition.add("user1_blueTeam-01",New Object)
definition.add("user2_blueTeam-01", New Object)
definition.add("user1_blueTeam-02", New Object)
definition.add("user2_redTeam-02", New Object)
Dim q3 = (
From userlists In Definition.Keys.GroupBy(Function(s) s.Split("_"c)(0))
Select New User(
userlists.Key,
(From scList In userlists.GroupBy(Function(s) s.Split("-"c)(0))
Select New Team(scList.Key.Split("_"c)(1), scList.OrderBy(Function(s) s).ToList())).ToList()
)
).ToList()
q3.dump()
End Sub
' Define other methods and classes here
Public class User
Public Property Name() As String
Public Property Teams() As List(Of Team)
Public Sub New(d As String, results As List(Of Team))
Me.Name = d
Me.Teams = results
End Sub
End Class
Public Class Team
Public Property TeamName() As String
Public Property FullScore() As List(Of String)
Public Sub New(name As String, value As List(Of String))
Me.TeamName = name
Me.FullScore = value
End Sub
End Class

Does it matter if the object of List(T) is being passed ByVal or ByRef

In the following example, does it matter if i pass the List(T) object in both functions ByRef or ByVal?
Is this right that as List is a reference type so value will always be changes even if i pass the object ByVal.
Would it be better if i pass the object byRef in function "ListChanged" as list is being updated.
Public Class MyClass_
Public Sub TestMethod()
Dim List_1 As New List(Of Integer)()
Dim List_2 As New List(Of Integer)()
List_1.Add(100)
List_2.Add(50)
List_1 = ActualListNotChanged(List_1) '---101
List_2 = ListChanged(List_2) '---50,51
End Sub
Private Function ActualListNotChanged(ByVal lst As List(Of Integer)) As List(Of Integer)
Dim nList As New List(Of Integer)()
For Each item As Integer In lst
If item <> 50 Then
nList.Add(101)
End If
Next item
Return nList
End Function
Private Function ListChanged(ByVal lst As List(Of Integer)) As List(Of Integer)
lst.Add(51)
Return lst
End Function
End Class
In your example, ByVal (the default) is the most appropriate.
Both ByVal and ByRef allow you to modify the list (e.g. add/remove items).
ByRef also allows you to replace the list with a different list, e.g.
Dim List1 As New List(Of Int)
List1.Add(1)
ListReplacedByVal(List1)
' List was not replaced. So the list still contains one item
Debug.Assert(List1.Count = 1) ' Assertion will succeed
ListReplacedByRef(List1)
' List was replaced by an empty list.
Debug.Assert(List1.Count = 0) ' Assertion will succeed
Private Sub ListReplacedByVal(ByVal lst As List(Of Integer))
lst = New List(Of Int)
End Sub
Private Sub ListReplacedByRef(ByRef lst As List(Of Integer))
lst = New List(Of Int)
End Sub
In general you should use ByVal. The object you pass can be modified (in the sense that you can call its methods and property setters to change its state). But it can't be replaced by a different object.
I'd say best practise would be to pass using ByRef if (and only if) you are changing the list. Not a long answer, but it's short and sweet!

Creating a Generic List of a specified type

I want to create a generic list - but I want to specify the type at runtime - is there a way I can do this? using reflection perhaps?
Something like this...
Public Shared Sub create(ByVal t As Type)
Dim myList As New Generic.List(Of t)
End Sub
Thanks in advance
James
If callers know the type, you can make the method itself generic:
Public Shared Sub create(Of t)()
Dim myList As New Generic.List(Of t)
End Sub
If callers do not know the type, you'll have to resort to reflection - see the accepted answer to this question for more information.
I have a function to do exactly that:
Public Shared Function CreateList(Of T)(ByVal ParamArray items() As T) As List(Of T)
Return New List(Of T)(items)
End Function
For instance, I can create a list of integers by doing this:
dim L as list(of Integer) = CreateList(1,2,3,4,5)
Or create a list of, say, textboxes:
dim L as list(of TextBox) = CreateList(txtPhone1, txtPhone2, txtPhone3)
Or in general, any type.