Ask a Question related to ASP.NET Building Controls, Design and Development.
-
lisa@starways.net #1
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
-
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... -
#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: ... -
#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: ... -
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... -
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 ... -
recoil@community.nospam #2
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
-
lisa@starways.net #3
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:Property> 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("")>up> [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 thesethis.> 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> 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 Classlisa@starways.net Guest
-
recoil@community.nospam #4
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



Reply With Quote

