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

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

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

    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

    lisa@starways.net Guest

  2. Similar Questions and Discussions

    1. Mysterious Error: Object reference not set to an instance of an object
      Hi There! I'm having a mysterious error right after I login using Forms Authentication in my ASP.NET app. Below is the error... Exception...
    2. #26121 [Opn->WFx]: Object defined before object - error
      ID: 26121 Updated by: sniper@php.net Reported By: closer at netnitco dot net -Status: Open +Status: ...
    3. #26121 [NEW]: Object defined before object - error
      From: closer at netnitco dot net Operating system: Redhat Linux 9.0 / Apache 2.0.46 PHP version: 5.0.0b2 (beta2) PHP Bug Type: ...
    4. HELP! Error Loading ASPX : Object Reference not set to an instance object
      Hello, When i run my aspx i get this weird error: System.NullReferenceException: Object reference not set to an instance of an object. at...
    5. Object reference not set to an instance of an object. .......... Error .... Please Help
      Hi, I am facing a problem, and i don't know why the error is being generated. I am updating a datagrid with an XML file. My code is ...
  3. #2

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

    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

    recoil@community.nospam Guest

  4. #3

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

    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



    [email]recoil@community.nosp[/email]am 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
    lisa@starways.net Guest

  5. #4

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

    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.

    recoil@community.nospam Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139