what is happening with .net object list? - vb.net

when I do something like
a = New List(Of Object)
a.Add(1)
a.Add("1")
Is there some information stored for each element, which type it is?

Related

Clear List in Session Object

Im very new to Vb..
I get the Following error 'Option Strict On disallows late Binding' when I try to clear a list in my seesionObject like shown.
Private Sub ClearSessionList()
Dim context As Object = System.Web.HttpContext.Current.Session("MySessionobject")
context.MyListProperty = New List(Of String)
End Sub
The error persist when i try following casts aswell
DirectCast(context.MyListProperty, List(Of String))
CType(context.MyListProperty, List(Of String))
I assume my Casts are wrong in some way, anyone that can point out the error for me and show how I can clear the List?
Always use option strict, then you couldn't access MyListProperty until you haven't casted context from Object to it's actual type. Otherwise VB.NET will try to cast it for you, sometimes with weird results.
So presuming the type of it is MySessionObject:
Dim context As Object = System.Web.HttpContext.Current.Session("MySessionobject")
Dim myObj As MySessionObject = DirectCast(context, MySessionObject)
myObj.MyListProperty.Clear()

Display the list of the Selected items in the checkboxlist

Dim list As New List(Of String)
list = chkparameter.Items
.Cast(Of ListItem)
.AsEnumerable()
.Where(Function(x) x.Selected)
.Select(Function(x) x.Value)
The error i am getting is
Unable to cast object of type 'WhereSelectEnumerableIterator2[System.Web.UI.WebControls.ListItem,System.String]' to type 'System.Collections.Generic.List1[System.String]'.
How can i rectify it.
Thanks
If you want to assign the result to a List(Of String) variable then you need a List(Of String) object. You can all ToList on any enumerable list to create a List(Of T).
Also, your AsEnumerable call is pointless because Cast(Of T) already returns an IEnumerable(Of T).
Finally, declaring a variable on one line and then setting its value is so unnecessarily verbose. It's not wrong but it is pointless. In your case, not only are you declaring a variable but you're also creating an object that you never use. Don't create a New object if you don;t actually want a new object, which you don;t because you're getting an object on the very next line.
Dim list As List(Of String) = chkparameter.Items.
Cast(Of ListItem).
Where(Function(x) x.Selected).
Select(Function(x) x.Value).
ToList()
There's also no need to declare the type of the variable because it will be inferred from the initialising expression, i.e. ToList returns a List(Of String) so the type of the variable can be inferred from that. Not everyone likes to use type inference where it's not completely obvious though, so I'll let you off that one. I'd tend to do this though:
Dim list = chkparameter.Items.
Cast(Of ListItem).
Where(Function(x) x.Selected).
Select(Function(x) x.Value).
ToList()
By the way, notice how much easier the code is to read with some sensible formatting? If you're going to use chained function syntax like that, it's a very good idea to put each function on a different line once you get more than two or three.

ListBox DataSource how to update (VB.NET)

I have an ArrayList with objects, this ArrayList is used as a DataSource to a ListBox. When new objects are added to the listbox, how can i get the listbox to update?
the code for populating the list looks lite this:
'Form1.ExistingArticles = The ArrayList
'Form1.LB_Articles = The Listbox
Public Sub FillArticleList()
If Form1.ExistingArticles.Count = 0 Then
Exit Sub
End If
Form1.LB_Articles.DataSource = Form1.ExistingArticles
Form1.LB_Articles.ValueMember = "ID"
Form1.LB_Articles.DisplayMember = "ListText"
End Sub
When i add a new object to the Form1.ExistinArticles i can see that the new object is there, but it does not update. Calling FillArticleList() again will not work either.
Firstly, if you're targeting .NET 2.0 or later then you shouldn't be using an ArrayList at all. The List(Of T) basically superseded the ArrayList and is what you should use in most cases.In this case, you can either use a List(Of T) and bind it to a BindingSource and bind that to the ListBox or else use a BindingList(Of T). Both the BindingSource and BindingList(Of T) have methods that you can call when you make changes to the list to update and bound control(s).

Cast(Of ?) from UIElement

In silverlight my custom controls are in theUIElementCollection of my StackPanel. I want to get a list of them by a specific value. There only DivElements in the container. It returns Nothing when I know I have one or more. I know I can make a simple loop and cast types inline, but I want to get better with LINQ and Cast(Of TResult). My attempt at casting:
Dim myList = TryCast(spDivs.Children.Where(Function(o) DirectCast(o, DivElement).ElementParent Is bComm).Cast(Of DivElement)(), List(Of DivElement))
The problem is you can't cast into a List(Of DivElement). The collection is a UIElementCollection, not a List(Of T).
You could build a new list, though. This can also be simplified by using OfType instead of casting manually:
Dim myList = spDivs.Children.OfType(Of DivElement)()
.Where(Function(o) o.ElementParent Is bComm)
.ToList()

VB.NET ArrayList to List(Of T) typed copy/conversion

I have a 3rd party method that returns an old-style ArrayList, and I want to convert it into a typed ArrayList(Of MyType).
Dim udc As ArrayList = ThirdPartyClass.GetValues()
Dim udcT AS List(Of MyType) = ??
I have made a simple loop, but there must be a better way:
Dim udcT As New List(Of MyType)
While udc.GetEnumerator.MoveNext
Dim e As MyType = DirectCast(udc.GetEnumerator.Current, MyType)
udcT.Add(e)
End While
Dim StronglyTypedList = OriginalArrayList.Cast(Of MyType)().ToList()
' requires `Imports System.Linq`
Duplicate.
Have a look at this SO-Thread: In .Net, how do you convert an ArrayList to a strongly typed generic list without using a foreach?
In VB.Net with Framework < 3.5:
Dim arrayOfMyType() As MyType = DirectCast(al.ToArray(GetType(MyType)), MyType())
Dim strongTypeList As New List(Of MyType)(arrayOfMyType)
What about this?
Public Class Utility
Public Shared Function ToTypedList(Of C As {ICollection(Of T), New}, T)(ByVal list As ArrayList) As C
Dim typedList As New C
For Each element As T In list
typedList.Add(element)
Next
Return typedList
End Function
End Class
If would work for any Collection object.
I would like to point out something about both the DirectCast and System.Linq.Cast (which are the same thing in the latest .NET at least.) These may not work if the object type in the array is defined by the user class, and is not easily convertable into object types that .NET recognizes. I do not know why this is the case, but it seems to be the problem in the software for which I am developing, and so for these we have been forced to use the inelegant loop solution.