Strange error from: Dim myState As Object() = CType(savedState, Object())

Discussion in 'ASP .Net Building Controls' started by lisa@starways.net, Apr 6, 2005.

  1. Guest

    Every book and every website I've seen that talks about how to save
    state for child controls in a composite webcontrol says to do something
    like the following. But when I do it, I hit the line:

    Dim myState As Object() = CType(savedState, Object())

    ....and I get this error:

    Cannot convert to 'Object ()'.

    I get the same error when I type in ?CType(savedState, Object()) in the
    command window. It's driving me buggy (no pun intended). I checked to
    see if savedState is Nothing, and it isn't.

    What's going on? I can't be the only one who has run into this, but
    everyone seems to just assume that it'll work.

    Thanks!
    Lisa

    Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    If Not savedState Is Nothing Then
    Dim myState As Object() = CType(savedState, Object())
    If Not myState(0) Is Nothing Then
    MyBase.LoadViewState(myState(0))
    If Not (myState(1) Is Nothing) Then CType(_LeftList,
    IStateManager).LoadViewState(myState(1))
    If Not (myState(2) Is Nothing) Then CType(_RightList,
    IStateManager).LoadViewState(myState(2))
    If Not (myState(3) Is Nothing) Then CType(_MoveLeft,
    IStateManager).LoadViewState(myState(3))
    If Not (myState(4) Is Nothing) Then CType(_MoveRight,
    IStateManager).LoadViewState(myState(4))
    End If
    End Sub 'LoadViewState
     
    , Apr 6, 2005
    #1
    1. Advertising

  2. Guest

    Imports System.ComponentModel
    Imports System.Web.UI

    <DefaultProperty("Text"), ToolboxData("<{0}:SampleControl
    runat=server></{0}:SampleControl>")> Public Class SampleControl
    Inherits System.Web.UI.WebControls.WebControl
    Dim gMyNumber As Int32
    Dim gMyDate As DateTime
    Dim _text As String
    Dim WithEvents btnTestPostback As New Button
    Dim WithEvents btnIncrementNumber As New Button

    <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    [Text]() As String
    Get
    Return _text
    End Get

    Set(ByVal Value As String)
    _text = Value
    End Set
    End Property
    <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    [MyNumber]() As Int32
    Get
    Return gMyNumber
    End Get

    Set(ByVal Value As Int32)
    gMyNumber = Value
    End Set
    End Property
    <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    [MyDate]() As DateTime
    Get
    Return gMyDate
    End Get

    Set(ByVal Value As DateTime)
    gMyDate = Value
    End Set
    End Property


    Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    If Not (savedState Is Nothing) Then
    Dim PriorState() As Object = CType(savedState, Object())
    MyBase.LoadViewState(PriorState(0)) ' Calls Base and passes this
    on. Always do this so that inheritance is easy
    MyNumber = CType(PriorState(1), Int32) ' Remember to Load these up
    in the same manner that you saved them.
    MyDate = CType(PriorState(2), DateTime)
    End If
    End Sub

    Protected Overrides Function SaveViewState() As Object
    Dim StateToSave(3) As Object
    StateToSave(0) = MyBase.SaveViewState() ' Calls Base and Gets this.
    Always do this so that inheritance is easy. Also ensures things like
    Enabled etc work properly.
    StateToSave(1) = MyNumber
    StateToSave(2) = MyDate
    Return StateToSave
    End Function

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    Me.Controls.Add(btnTestPostback)
    Me.Controls.Add(btnIncrementNumber)
    btnTestPostback.ID = "btnTestPostback"
    btnTestPostback.Text = "Test Postback"
    btnIncrementNumber.ID = "btnIncrementNumber"
    btnIncrementNumber.Text = "Increment Number"

    End Sub

    Protected Overrides Sub Render(ByVal writer As
    System.Web.UI.HtmlTextWriter)
    writer.Write("Number is " + MyNumber.ToString() + " and date is " +
    MyDate.ToShortDateString() + "<br />")
    MyBase.Render(writer)
    End Sub

    Private Sub btnIncrementNumber_Click(ByVal sender As Object, ByVal e
    As System.EventArgs) Handles btnIncrementNumber.Click
    MyNumber = MyNumber + 1
    End Sub
    End Class
     
    , Apr 7, 2005
    #2
    1. Advertising

  3. Guest

    Um... that was nice. But it doesn't even address the issue I'm having,
    let alone solve it. Can you please explain what your point was?

    You have Dim PriorState() As Object = CType(savedState, Object()),
    which is no different than my Dim myState As Object() =
    CType(savedState, Object()). Not significantly different, at any rate.
    And it's the CType(savedState, Object()) part that's giving me an
    error.

    Does anyone have any idea why I'd be getting an error trying to convert
    savedState into Object()?

    Thanks,
    Lisa



    wrote:
    > Imports System.ComponentModel
    > Imports System.Web.UI
    >
    > <DefaultProperty("Text"), ToolboxData("<{0}:SampleControl
    > runat=server></{0}:SampleControl>")> Public Class SampleControl
    > Inherits System.Web.UI.WebControls.WebControl
    > Dim gMyNumber As Int32
    > Dim gMyDate As DateTime
    > Dim _text As String
    > Dim WithEvents btnTestPostback As New Button
    > Dim WithEvents btnIncrementNumber As New Button
    >
    > <Bindable(True), Category("Appearance"), DefaultValue("")>

    Property
    > [Text]() As String
    > Get
    > Return _text
    > End Get
    >
    > Set(ByVal Value As String)
    > _text = Value
    > End Set
    > End Property
    > <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    > [MyNumber]() As Int32
    > Get
    > Return gMyNumber
    > End Get
    >
    > Set(ByVal Value As Int32)
    > gMyNumber = Value
    > End Set
    > End Property
    > <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    > [MyDate]() As DateTime
    > Get
    > Return gMyDate
    > End Get
    >
    > Set(ByVal Value As DateTime)
    > gMyDate = Value
    > End Set
    > End Property
    >
    >
    > Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    > If Not (savedState Is Nothing) Then
    > Dim PriorState() As Object = CType(savedState, Object())
    > MyBase.LoadViewState(PriorState(0)) ' Calls Base and passes this
    > on. Always do this so that inheritance is easy
    > MyNumber = CType(PriorState(1), Int32) ' Remember to Load these

    up
    > in the same manner that you saved them.
    > MyDate = CType(PriorState(2), DateTime)
    > End If
    > End Sub
    >
    > Protected Overrides Function SaveViewState() As Object
    > Dim StateToSave(3) As Object
    > StateToSave(0) = MyBase.SaveViewState() ' Calls Base and Gets

    this.
    > Always do this so that inheritance is easy. Also ensures things like
    > Enabled etc work properly.
    > StateToSave(1) = MyNumber
    > StateToSave(2) = MyDate
    > Return StateToSave
    > End Function
    >
    > Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    > Me.Controls.Add(btnTestPostback)
    > Me.Controls.Add(btnIncrementNumber)
    > btnTestPostback.ID = "btnTestPostback"
    > btnTestPostback.Text = "Test Postback"
    > btnIncrementNumber.ID = "btnIncrementNumber"
    > btnIncrementNumber.Text = "Increment Number"
    >
    > End Sub
    >
    > Protected Overrides Sub Render(ByVal writer As
    > System.Web.UI.HtmlTextWriter)
    > writer.Write("Number is " + MyNumber.ToString() + " and date is " +
    > MyDate.ToShortDateString() + "<br />")
    > MyBase.Render(writer)
    > End Sub
    >
    > Private Sub btnIncrementNumber_Click(ByVal sender As Object, ByVal e
    > As System.EventArgs) Handles btnIncrementNumber.Click
    > MyNumber = MyNumber + 1
    > End Sub
    > End Class
     
    , Apr 7, 2005
    #3
  4. Guest

    Well if you are unable to copy, paste and implement my "working" sample
    and you wish to continue with your non-working sample then you must
    analyze the error you are getting.
    It is stating that it is unable to convert .. to an object()

    So what would be the plan of attack.
    First check to see if the Object you are converting is null.
    Second check the objects type because obviously if I can convert it in
    my working sample and you can't then that obviously means that the
    object is not of the type you expect it to be.

    Both checks should be able to be done with the following code

    if (savedState is nothing) Page.Response.write("this is null<br />")
    else
    Page.Response.Write(savedState.GetType().ToString())

    This will check to see if the object is empty. if it is then it will
    tell you so else it will give you the actual Object type.

    Alternately you can simply put a breakpoint by the line that errors and
    debug it and in your watch window add the savedState to it. The Watch
    window will tell you what what value and what object type this variable
    is.
     
    , Apr 11, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    1
    Views:
    452
    Steve C. Orr, MCSD
    Jul 23, 2003
  2. Denis Palas
    Replies:
    1
    Views:
    501
  3. Florian Kaufmann
    Replies:
    1
    Views:
    379
  4. DaveF

    dim Name redefined error

    DaveF, Apr 14, 2004, in forum: ASP General
    Replies:
    8
    Views:
    634
    Aaron Bertrand [MVP]
    Apr 14, 2004
  5. Markus Dehmann
    Replies:
    9
    Views:
    219
    Tad McClellan
    Mar 23, 2005
Loading...

Share This Page