473,769 Members | 6,286 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="NCon nect" TagName="Addres s" Src="Address.as cx"
%>

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

<form runat="server">
<NConnect:Addre ss ID="ncBilling" runat="server"/><br>
<NConnect:Addre ss 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.Lab el'.

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.Capti on = "BILLING ADDRESS"
ncShipping.Capt ion = "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 2094
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.Capti on = "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**@rediffmai l.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.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="NCon nect" TagName="Addres s" Src="Address.as cx"
%>

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

<form runat="server">
<NConnect:Addre ss ID="ncBilling" runat="server"/><br>
<NConnect:Addre ss 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.Lab el'.

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.Capti on = "BILLING ADDRESS"
ncShipping.Capt ion = "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.Capti on = "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**@rediffmai l.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.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="NCon nect" TagName="Addres s" Src="Address.as cx"
%>

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

<form runat="server">
<NConnect:Addre ss ID="ncBilling" runat="server"/><br>
<NConnect:Addre ss 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.Lab el'.

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.Capti on = "BILLING ADDRESS"
ncShipping.Capt ion = "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
7389
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 channal hosting into IIS. There is a class B which is also available through remoting hosted on IIS on the same URI. B creates new of A inside a function. It succeed and able to create instance of A inside B first time. But it failes in 2nd attempt when...
3
13006
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'. here is the code that I used to create a table and then add columns to it later, later I populate the rows in the table.
8
6512
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
1578
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 after converting to .NET 2.0. Unable to cast object of type 'Oracle.DataAccess.Client.OracleCommand' to type 'System.Data.Common.DbCommand'
0
1630
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 follows: ((StyleSheetProvider)this.Page).GetStyleSheetPath(); Is there some different way of typecasting that needs to be done in vs2005? Thanks in advance.
3
10258
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 'System.String'.
4
3886
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
3754
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 and sometimes not, without making any changes to the code. Is this a bug concern? Or do you have any advice for this issue? Here is the error message I get:
2
6542
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
2345
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 web.config has been updated with the necessary section. Using System.Web.Configuration.WebConfiguration.GetSection(), the config information is returned without any issues when the GetSection is set to an object. When the object is casted explicitly...
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.