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

L

lisa

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
 
R

recoil

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
 
L

lisa

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



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
 
R

recoil

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top