user3116167 user3116167 -4 years ago 189 Question

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?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download