'True' could not be set on property

P

PJ6

I get this design-time error (below) when setting thus property in my
inherited web control to 'True'. I've debugged the code running at
design-time to double check to see that no exception was being thrown. While
there are other people that have run into this exception, what I've found so
far hasn't been helpful.

What is the proper way to declare custom Boolean properties that need to be
persisted in attributes through the Web Forms designer?

Paul

There was an error rendering the control.
'True' could not be set on property 'ShowEmptySelection'.

<DefaultValue(False), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
_
PersistenceMode(PersistenceMode.Attribute), _
Bindable(True), Category("Appearance")> _
Public Overridable Property ShowEmptySelection() As Boolean
Get
Dim ret As Boolean = False
Dim o As Object = Me.ViewState("ShowEmptySelection")
If Not o Is Nothing Then
ret = CBool(o)
End If
Return ret
End Get
Set(ByVal value As Boolean)
Me.ViewState("ShowEmptySelection") = value
End Set
End Property
 
S

Scott M.

PJ6 said:
I get this design-time error (below) when setting thus property in my
inherited web control to 'True'. I've debugged the code running at
design-time to double check to see that no exception was being thrown.
While there are other people that have run into this exception, what I've
found so far hasn't been helpful.

What is the proper way to declare custom Boolean properties that need to
be persisted in attributes through the Web Forms designer?

Paul

There was an error rendering the control.
'True' could not be set on property 'ShowEmptySelection'.

<DefaultValue(False), _

DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
_
PersistenceMode(PersistenceMode.Attribute), _
Bindable(True), Category("Appearance")> _
Public Overridable Property ShowEmptySelection() As Boolean
Get
Dim ret As Boolean = False
Dim o As Object = Me.ViewState("ShowEmptySelection")
If Not o Is Nothing Then
ret = CBool(o)
End If
Return ret
End Get
Set(ByVal value As Boolean)
Me.ViewState("ShowEmptySelection") = value
End Set
End Property

I'm not sure, but if you don't mind, may I suggest you change your Get code?
What you have there is very unneccessary.

Public Overridable Property ShowEmptySelection() As Boolean
Get
If Me.ViewState("ShowEmptySelection") IsNot Nothing Then Return
True
Return False
End Get
Set(ByVal value As Boolean)
Me.ViewState("ShowEmptySelection") = value
End Set
End Property
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Scott said:
I'm not sure, but if you don't mind, may I suggest you change your Get
code? What you have there is very unneccessary.

Public Overridable Property ShowEmptySelection() As Boolean
Get
If Me.ViewState("ShowEmptySelection") IsNot Nothing Then
Return True
Return False
End Get
Set(ByVal value As Boolean)
Me.ViewState("ShowEmptySelection") = value
End Set
End Property

But that will make the property always return true if there is a value
in view state, regardless of the actual value.
 
S

Scott M.

But that will make the property always return true if there is a value in
view state, regardless of the actual value.

It seems like that's what the OP wants here since all he was doing in his
original code was casting the value to a Boolean. Wouldn't any value cause
the cast to result in True, but no value would cause False?
 
S

Scott M.

To follow up...

If the OP wanted the "value" of the ViewState item cast as a boolean, should
he not have casted the ViewState item to a string and cast that, rather than
cast the ViewState "item" to an object?
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top