473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unable To Cast

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)?

Oct 5 '06 #1
2 2078
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!
*************************************************
<rn**@rediffmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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)?

Oct 6 '06 #2
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
Cowboy (Gregory A. Beamer) wrote:
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!
*************************************************
<rn**@rediffmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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)?
Oct 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pankaj Jain | last post by:
Hi All, I have a class A which is derived from ServicesComponent to participate in automatic transaction with falg Transaction.Required. Class A is exposed to client through remoting on Http...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
8
by: | last post by:
I have the following class : Public Class MyTreeNode Inherits TreeNode Dim mystring As String End Class Now, when I try to do this : ''''''''''''nodes is a TreeNodeCollection, s is string
0
by: sam | last post by:
Hi: I am not sure if this is the right place to post this question. Please let me know if it is not and I appreciate if someone could point me in the right direction. I am getting this error...
0
by: hlyall1189 | last post by:
Hi, I recently started upgrading some of my old vs 2003 apps to vs 2005 and used the conversion tool but now i get the following error after building the page. I have typecasted the lines as...
3
by: keithb | last post by:
What could be causing this? this code: String Com = ""; if (Com != (String)rw.ItemArray) fails at runtime with the error message: Unable to cast object of type 'System.Int32' to type...
4
by: =?Utf-8?B?TWF0dCBMb3Zl?= | last post by:
If I extend the WebBrowser class like this: public class CustomWebBrowser : System.Windows.Forms.WebBrowser { public string myProperty() { return "myProperty"; } }
1
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I have a master page and a content page to that master. When I try to work with them I get an inconsistent error of casting ability and it happens irregularly, which means sometimes it can work...
2
by: Bigi | last post by:
Hi, Please help, this has been driving me nuts for nearly 2 days now. This vb6 code works: Public oEng As New ebizEngine Public oMsg As ebizMessage Function EbizGetFromQueue() As String
1
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.