473,406 Members | 2,356 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,406 software developers and data experts.

Class casting.

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
9 1777
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: David Sobey | last post by:
hi Here's some code: void SomeFunc(childClass arg); .... parentClass a; a=new childClass(); SomeFunc(a);
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
0
by: Michel | last post by:
Hi all! I created a new class, which has the UserControl as a base class. Now I want to create an instance of my new class, by creating a new usercontrol and casting it to my new class using...
10
by: Brett Romero | last post by:
Say I have a class inheriting some base class: BaseClass { void Foo() { Update(); } }
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
12
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...
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.