Class casting.

K

Kris

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();

}
}
 
P

Pipo

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

Kris

Pipo said:
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" %>

------------------------------------------------------------------------------------------
 
K

Kris

Pipo said:
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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

What if you use the complete name of A:

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

Kris

Ignacio said:
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/archive/2005/09/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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

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().
 
K

Kris

Ignacio said:
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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

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

Kris

Ignacio said:
Hi,





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

:)
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.
 

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

Similar Threads


Top