Class members being lost in session state

Last post 10-07-2008 12:37 PM by bladewing2. 4 replies.

Sort Posts:

  • Class members being lost in session state

    10-06-2008, 4:56 PM
    • Member
      point Member
    • bladewing2
    • Member since 09-25-2008, 10:43 AM
    • Posts 10

    Hey,

        I'm sure this is something simple I'm not realizing has to be done for session state, but here is my problem.  I have a file class that has some general information and then it has a List(Of fileProperty) where fileProperty is another sub class.   Before this I was using List(Of Array List).  With the arraylist it went into the session state and came back out no problem.  But after switching array list to my own file property class the sub class of file property is being lost.  For instance the user will instantiate a file and add some properties.  Then throw it in session state. Upon pulling the file out of session state I can access the basic properties such as name, size, ext, etc. just fine.  But if I go to access the List of properties it returns nothing since the list is now empty.   It's pretty much the fileProperty class doesn't know it should follow the file class into session state and I don't know what to use to make that happen.  I'm guessing this is where serializable or something like it comes into play, but I'm not a hundred percent sure.  Thanks!

  • Re: Class members being lost in session state

    10-06-2008, 10:09 PM
    • Participant
      861 point Participant
    • chrisrock
    • Member since 08-03-2004, 8:54 AM
    • Posts 176

    Do you have a code example? I wrote a quick example with a fileproperties list and the session stored the list properly... 

    http://www.rocksthoughts.com
    http://www.jumpstarttv.com - Free Technical Videos!
  • Re: Class members being lost in session state

    10-07-2008, 8:52 AM
    • Member
      point Member
    • bladewing2
    • Member since 09-25-2008, 10:43 AM
    • Posts 10

    Here's the file class:

     

    Imports Microsoft.VisualBasic

    Imports System.Collections.ArrayList

    Imports System.Collections.Generic

    Imports fileProperty

    Public Class LoadedFile

    Private fileName As String = String.Empty

    Private fileExt As String = String.Empty

    Private fileSize As String = String.Empty

    Private fileProps As List(Of fileProperty) = New List(Of fileProperty)Public Sub New(ByVal name As String, ByVal size As Integer)

    fileName = name

    Dim nameParts() As String = name.Split(".")

    fileExt = nameParts(1)

    fileSize = size

    End Sub

    Public Sub New()

    fileName = "Empty"

    End Sub

    Public Sub addProperty(ByVal prop As String, ByVal value As String, ByVal type As String)

    fileProps.Add(New fileProperty(prop, value, type))

    End Sub

    Public Sub clear()

    fileProps.Clear()

    End Sub

    ReadOnly Property count() As Integer

    Get

    Return fileProps.Count

    End Get

    End Property

    ReadOnly Property name() As String

    Get

    Return fileName

    End Get

    End Property

    ReadOnly Property size() As String

    Get

    Return fileSize

    End Get

    End Property

    ReadOnly Property ext() As String

    Get

    Return fileExt

    End Get

    End Property

    ReadOnly Property getProps() As StringCollection

    Get

    Dim tempStringCollection As New StringCollection

    For i As Integer = 0 To fileProps.Count - 1

    tempStringCollection.Add(CType(fileProps(i), fileProperty).name)

    Next

    If tempStringCollection.Count > 0 Then

    Return tempStringCollection

    Else

    Return Nothing

    End If

    End Get

    End Property

    ReadOnly Property getPropFromValue(ByVal givenValue As String) As String

    Get

    For i As Integer = 0 To fileProps.Count - 1

    If CType(fileProps(i), fileProperty).value = givenValue Then

    Return CType(fileProps(i), fileProperty).name

    End If

    Next

    Return Nothing

    End Get

    End Property

    ReadOnly Property getValuesFromProp(ByVal prop As String) As StringCollection

    Get

    Dim tempStringCollection As New StringCollection

    For i As Integer = 0 To fileProps.Count - 1

    If CType(fileProps(i), fileProperty).name = prop Then

    tempStringCollection.Add(CType(fileProps(i), fileProperty).value)

    End If

    Next

    If tempStringCollection.Count > 0 Then

    Return tempStringCollection

    Else

    Return Nothing

    End If

    End Get

    End Property

    ReadOnly Property prop(ByVal index As Integer) As fileProperty

    Get

    Return CType(fileProps(index), fileProperty)

    End Get

    End Property

    ReadOnly Property isPropsEmpty() As Boolean

    Get

    If fileProps.Count = 0 Then

    Return True

    Else

    Return False

    End If

    End Get

    End Property

    End Class

     

     

    And here's the file property class:

     

    Imports Microsoft.VisualBasic

    Public Class fileProperty

    Private propName As String = String.Empty

    Private propValue As String = String.Empty

    Private propType As String = String.Empty

    Public Sub New(ByVal name As String, ByVal value As String, ByVal type As String)

    propName = name

    propValue = value

    propType = type

    End Sub

    Public Sub New()

    End Sub

    ReadOnly Property name() As String

    Get

    Return propName

    End Get

    End Property

    ReadOnly Property value() As String

    Get

    Return propValue

    End Get

    End Property

    ReadOnly Property type() As String

    Get

    Return propType

    End Get

    End Property

    End Class

     

    Now in my program I have a list of the loadedFile class types.  When I throw it into session state and bring it back out I can access the basic types of the class, but when I go to access the list of the fileproperty class the list is empty and the references to the fileproperty class have been lost.  Weird thing is when I used the same code with .net Array list it worked just fine.  I switched it so anyone using the classes could use LoadedFile.prop(i).name instead of LoadedFile.prop(i) returning an array list they then had to know something about.  Only thing I can figure is in the array list class they're using serializable or something like that to bring it in.  Not really sure what I'm missing, but I'm sure I'll want to kick myself once I find out Stick out tongue

  • Re: Class members being lost in session state

    10-07-2008, 9:33 AM
    • Participant
      861 point Participant
    • chrisrock
    • Member since 08-03-2004, 8:54 AM
    • Posts 176

     Are you using Visual Studio 2008 or 2005? I'm running this same code in vs 2008 and it's working...

    I ran this code in default.aspx

    Dim lc As New LoadedFile("test.jpg", 1)
    lc.addProperty("prop", "value", "type")
    lc.addProperty("prop1", "value1", "type1")

    Session("test") = lc

    Ran this code in default.aspx and was able to access the file properties:

    Dim lc As LoadedFile = CType(Session("test"), LoadedFile)

     

    http://www.rocksthoughts.com
    http://www.jumpstarttv.com - Free Technical Videos!
  • Re: Class members being lost in session state

    10-07-2008, 12:37 PM
    • Member
      point Member
    • bladewing2
    • Member since 09-25-2008, 10:43 AM
    • Posts 10

    Thanks for the replies and I found what was happening.  *kicks himself*  It was coder error.  I was using a clear function which still exists with the array list path I was going down and I missed a call to the clear function that still existed in the code.  It's always something simple and usually stupid [:-P] .    Thanks for the replies and sorry for wasting time.

    Bladewing2

Page 1 of 1 (5 items)