Unable To Cast

R

rn5a

Suppose I have this property in a user control (which is named
Address.ascx):

<script runat="server">
Public Property Caption() As Object
Get
Caption = lblCaption.Text
End Get
Set(ByVal value As Object)
lblCaption.Text = value
End Set
End Property
</script>

<asp:Label ID="lblCaption" runat="server"/>

When I try to use the above property in an ASPX page with the following
code:

<%@ Register TagPrefix="NConnect" TagName="Address" Src="Address.ascx"
%>

<script runat="server">
Sub Page_Load(.....)
CType(ncBilling.Caption, Label).Text = "BILLING ADDRESS"
CType(ncShipping.Caption, Label).Text = "SHIPPING ADDRESS"
End Sub
</script>

<form runat="server">
<NConnect:Address ID="ncBilling" runat="server"/><br>
<NConnect:Address ID="ncShipping" runat="server"/>
</form>

then ASP.NET generates the error:

Unable to cast object of type 'System.String' to type
'System.Web.UI.WebControls.Label'.

pointing to the first line in the Page_Load sub.

Can someone please explain me what am I doing wrong here?

Please note that I am very much aware that if the property type in
Address.ascx is changed to String, then in the ASPX page

ncBilling.Caption = "BILLING ADDRESS"
ncShipping.Caption = "SHIPPING ADDRESS"

will resolve the issue but I would like to know why does the error get
generated if I opt for the former code (when the type of the property
named Caption is set to Object & not to String)?
 
C

Cowboy \(Gregory A. Beamer\)

Here's your trouble:

CType(ncBilling.Caption, Label).Text = "BILLING ADDRESS"

Can you figure out why. Look at this closely:

Public Property Caption() As Object
Get
Caption = lblCaption.Text
End Get
Set(ByVal value As Object)
lblCaption.Text = value
End Set
End Property

Does it make sense yet? How about if I ask "is an object necessarily a
label?"

WHat you have done is set a property that sets an "internal" value (the
label's text). The label is not exposed externally, so casting the property
to a label is not a good thing. And, it blows up ... as expected.

Make the Caption property a String, not an object. Then change your load to:

ncBilling.Caption = "BILLING ADDRESS"

That will set the string from external. NOTE: You can also set this up in
the ASPX tags and bind the string at design time.

BTW, I like your thinking here. Great reuse. If more people would treat user
controls as objects, it would make things a lot easier. It also makes it
easy to convert the control to a compiled library and use the designer, but
that is a topic for another day. :)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com/

*************************************************
Think Outside the Box!
*************************************************
 
R

rn5a

Gregory, could you please explain what do you mean by "The Label is not
exposed externally"? Are you using the term "externally" because the
Label has been encapsulated within a user control & not within a normal
ASPX page?

Also, you have opined that the property code doesn't make any sense.
Could you please explain me why doesn't the property code make any
sense?

To be honest, I am a newbie in all these things & hence would like to
strengthen my fundamentals on ASP.NET. I would be highly obliged if you
could please help me do that (of course, I can't expect you to explain
me each & every thing in ASP.NET but at least this part of ASP.NET that
my post is based on).

Thanks
 

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

Top