By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

Class casting.

P: n/a
Hi.

I have a asp class A with a public function AA()

----------------A.aspx.cs------------------------
public partial class A : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
public string AA()
{
return "AA";
}
}
---------------------------------------------------

I have also a user control which is on page A

---------------------WebUserConstrol.ascx.cs ------------------
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
string B = ((A) this.Page)).AA();
}
}
-----------------------------------------------------------------

What I am trying to do is to call function AA from user control.

string B = ((A) this.Page)).AA();

but, during compilation I get error that

The type or namespace name 'A' could not be found (are you missing a
using directive or an assembly reference?)

What I am doing wrong?????? Default namespaces. Just simple application
with two pages and user control.

The funny thing is that code in page B compiles without problem.
Of course this type of casting doesn't make sense, but it works.

------------------------B.aspx.cs----------------------------
public partial class B : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string B = ((A)this.Page).AA();

}
}
----------------------------------------------------------

What I am doing wrong??????
Mar 29 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Do you miss the register source in your page, maybe?
<%@ Register Src="../WebUserControl.ascx" TagName="Usercontrol"
TagPrefix="uc1" %>

"Kris" <wi*******@spam.gazeta.pl.invalid> wrote in message
news:e0**********@inews.gazeta.pl...
Hi.

I have a asp class A with a public function AA()

----------------A.aspx.cs------------------------
public partial class A : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
public string AA()
{
return "AA";
}
}
---------------------------------------------------

I have also a user control which is on page A

---------------------WebUserConstrol.ascx.cs ------------------
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
string B = ((A) this.Page)).AA();
}
}
-----------------------------------------------------------------

What I am trying to do is to call function AA from user control.

string B = ((A) this.Page)).AA();

but, during compilation I get error that

The type or namespace name 'A' could not be found (are you missing a using
directive or an assembly reference?)
What I am doing wrong?????? Default namespaces. Just simple application
with two pages and user control.

The funny thing is that code in page B compiles without problem.
Of course this type of casting doesn't make sense, but it works.

------------------------B.aspx.cs----------------------------
public partial class B : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string B = ((A)this.Page).AA();

}
}
----------------------------------------------------------

What I am doing wrong??????

Mar 29 '06 #2

P: n/a
Pipo wrote:
Do you miss the register source in your page, maybe?
<%@ Register Src="../WebUserControl.ascx" TagName="Usercontrol"
TagPrefix="uc1" %>


No, everything is ok with aspx files

The problem is that WebUserControl class doesn't see A class

------------------------A.aspx--------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="A.aspx.cs"
Inherits="A" %>

<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl"
TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />

</div>
</form>

</body>
</html>
----------------------------WebuserControl.ascx-------------------------------------------
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

------------------------------------------------------------------------------------------
Mar 29 '06 #3

P: n/a
Pipo wrote:
Do you miss the register source in your page, maybe?
<%@ Register Src="../WebUserControl.ascx" TagName="Usercontrol"
TagPrefix="uc1" %>

More.

Similar code compiles under VS.net

This one doesn't work with VS 2005
Mar 29 '06 #4

P: n/a
Hi,

What if you use the complete name of A:

((your_web_app_namespace.A) this.Page)).AA();
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Kris" <wi*******@spam.gazeta.pl.invalid> wrote in message
news:e0**********@inews.gazeta.pl...
Hi.

I have a asp class A with a public function AA()

----------------A.aspx.cs------------------------
public partial class A : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
public string AA()
{
return "AA";
}
}
---------------------------------------------------

I have also a user control which is on page A

---------------------WebUserConstrol.ascx.cs ------------------
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
string B = ((A) this.Page)).AA();
}
}
-----------------------------------------------------------------

What I am trying to do is to call function AA from user control.

string B = ((A) this.Page)).AA();

but, during compilation I get error that

The type or namespace name 'A' could not be found (are you missing a using
directive or an assembly reference?)
What I am doing wrong?????? Default namespaces. Just simple application
with two pages and user control.

The funny thing is that code in page B compiles without problem.
Of course this type of casting doesn't make sense, but it works.

------------------------B.aspx.cs----------------------------
public partial class B : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string B = ((A)this.Page).AA();

}
}
----------------------------------------------------------

What I am doing wrong??????

Mar 29 '06 #5

P: n/a
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

What if you use the complete name of A:

((your_web_app_namespace.A) this.Page)).AA();


No, it does't work.
I found some articles in the Internet that this is a problem with
ASP.NET 2.0 because of partial classes.

http://west-wind.com/weblog/posts/3016.aspx
http://odetocode.com/Blogs/scott/arc...9/12/2186.aspx
They show how to solve it between two peges, from page to control, but
not from control to page.

<%@ Reference also doesn't solve the problem, because I get circular
reference

Page registers control, control references to page...
I am still looking for.

Mar 29 '06 #6

P: n/a
Hi,

Thanks for the link !

Your solution is in the first one, in one of the comments:

In 1.1 I accessed these like this: ((MyPage)this.Page).MyPublicMethod().

To do the same in 2.0:
Create an interface in App_Code folder:
public interface IMyPage { void MyPublicMethod(); }
In MyPage.aspx.cs:
public partial class MyPage : Page, IMyPage
{
....
public void MyPublicMethod() { ... }
}

Then this works:
((IMyPage)this.Page).MyPublicMethod().

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Kris" <wi*******@spam.gazeta.pl.invalid> wrote in message
news:e0**********@inews.gazeta.pl...
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

What if you use the complete name of A:

((your_web_app_namespace.A) this.Page)).AA();


No, it does't work.
I found some articles in the Internet that this is a problem with ASP.NET
2.0 because of partial classes.

http://west-wind.com/weblog/posts/3016.aspx
http://odetocode.com/Blogs/scott/arc...9/12/2186.aspx
They show how to solve it between two peges, from page to control, but not
from control to page.

<%@ Reference also doesn't solve the problem, because I get circular
reference

Page registers control, control references to page...
I am still looking for.


Mar 29 '06 #7

P: n/a
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

Thanks for the link !

Your solution is in the first one, in one of the comments:

In 1.1 I accessed these like this: ((MyPage)this.Page).MyPublicMethod().

To do the same in 2.0:
Create an interface in App_Code folder:
public interface IMyPage { void MyPublicMethod(); }
In MyPage.aspx.cs:
public partial class MyPage : Page, IMyPage
{
....
public void MyPublicMethod() { ... }
}

Then this works:
((IMyPage)this.Page).MyPublicMethod().

Thanks.
I did similar thing.
I created a base class and all pages inherit from this base class.
The reason was, that I had different methods in classes.
Inteface forces me to implement all of them in each class.
Virtual methods in base class allow me to implement only needed method,
keeping the rest in base class only.

Mar 29 '06 #8

P: n/a
Hi,

Thanks.
I did similar thing.
I created a base class and all pages inherit from this base class.
The reason was, that I had different methods in classes.


You mean methods with different signature?
Cause if they have the same signature, well that the idea of using an
interface in the first place !
Inteface forces me to implement all of them in each class.
Or you could implement several interfaces, but what I do not understand is
how you call the different methods if they have different signatures.
Virtual methods in base class allow me to implement only needed method,


What about different interfaces?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 29 '06 #9

P: n/a
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,
Thanks.
I did similar thing.
I created a base class and all pages inherit from this base class.
The reason was, that I had different methods in classes.

You mean methods with different signature?
Cause if they have the same signature, well that the idea of using an
interface in the first place !


Yes I know, but it's existing application which I moves to VS2005 and
somebody used different names for each class. I didn't want to changed
it. Maybe later, when I have time

Inteface forces me to implement all of them in each class.

Or you could implement several interfaces, but what I do not understand is
how you call the different methods if they have different signatures.

:)

huge case

:)

Virtual methods in base class allow me to implement only needed method,

What about different interfaces?

To much work. 15 interfaces and one method in each.
it was easier to impelemt one base class with 15 virutal methods.
I had to changed inheritance in 15 classess and casting class in that
huge case.


Mar 30 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.