C# - Object Inheritance? (Web Dev.)

Coders forum. Discuss techniques, swap code snippets, ask for help etc. relating to all forms of software development.
Post Reply
User avatar
moosedude
Baaaaaaa!
Baaaaaaa!
Posts: 6209
Joined: Fri Nov 19, 2004 1:19 pm 0
Location: Craggy Island
Contact:

C# - Object Inheritance? (Web Dev.)

Post by moosedude »

i'm writing a website using .NET 1.1 and C#, but I've come across I few really annoying snags, I just wondered if anybody else had?

Right, I have two class, both in seperate .aspx files, both classes inherit from System.Web.UI.Page and each of them can contain public classes inside of those classes.

Now if I wanted to use the class from fileA, inside fileB - how on earth can I do this? I don't really want to pre-compile everything I write into a library.dll file just for the sake of some inheritance and I don't think putting the classes into .CS files will work either ...

is there anyway around this? I could kind of guess why it is designed like this, but it seems a massive oversight and a missed oppotunity in the C# language and for website development - am I missing something?

any suggestions greatfully received ...

01000101 01000100 01000111 01000101

http://www.redbubble.com/people/fotofill/ buy my photos!
Trufun101
Baa!
Baa!
Posts: 40
Joined: Mon Sep 19, 2005 6:44 am 0
Contact:

Re: C# - Object Inheritance? (Web Dev.)

Post by Trufun101 »

moosedude wrote:i'm writing a website using .NET 1.1 and C#, but I've come across I few really annoying snags, I just wondered if anybody else had?

Right, I have two class, both in seperate .aspx files, both classes inherit from System.Web.UI.Page and each of them can contain public classes inside of those classes.

Now if I wanted to use the class from fileA, inside fileB - how on earth can I do this? I don't really want to pre-compile everything I write into a library.dll file just for the sake of some inheritance and I don't think putting the classes into .CS files will work either ...

is there anyway around this? I could kind of guess why it is designed like this, but it seems a massive oversight and a missed oppotunity in the C# language and for website development - am I missing something?

any suggestions greatfully received ...
If you're wanting both aspx pages to be able to share the same functionality, you can try a couple of things:

1. Create your own class that inherits from System.Web.UI.Page, and add whatever functionality you'd like. Then have you aspx pages inherit from "MyPage" versus System.Web.UI.Page.

2. Create a seperate CS file, and assign it a namespace. Then you should be able to reference that namespace in your aspx files and access those additional functions / objects.

Hope that helps, if not lemme know.
-Chris B.
User avatar
moosedude
Baaaaaaa!
Baaaaaaa!
Posts: 6209
Joined: Fri Nov 19, 2004 1:19 pm 0
Location: Craggy Island
Contact:

Post by moosedude »

Thanks for your suggestion, in the end I just put the classes I needed into a pre-compiled library.

anyway, I not sure what you meant in (2), of your suggestions, would you mind explaining in a bit more detail please?

I guess what I wanted to do was simular to C++'s #include statement, where I have the freedom to reference any code file I want and inherit from the classes within.

My aspx standard template is to create a aspx file with a codebehind attribute at the top to reference a .cs file. I would use the aspx file for the GUI, and the .cs file for the functionality. This setups ok, but I still want to reference more than one file in this codebehind statement ..?

I already have a "master page" class, from which all pages inherit from, which is actually a cracking idea and makes life easier in the long run... :)

01000101 01000100 01000111 01000101

http://www.redbubble.com/people/fotofill/ buy my photos!
Trufun101
Baa!
Baa!
Posts: 40
Joined: Mon Sep 19, 2005 6:44 am 0
Contact:

Post by Trufun101 »

moosedude wrote:anyway, I not sure what you meant in (2), of your suggestions, would you mind explaining in a bit more detail please?
Sure, what I'm talking about is this: (for example)

-- Math.cs --

Code: Select all

namespace MathFunctions
{
	public class Math
	{
		public Math()
		{
		}

		static public int Add(int x, int y)
		{
			return x + y;
		}
	}
}
-- Inside your aspx.cs --

Code: Select all

using MathFunctions;

...

private void Page_Load(object sender, System.EventArgs e)
{
	txtAnswer.Text = Math.Add(10, 2).ToString();
}
So, within any of your codebehind pages within your project, just include "using MathFunctions" and you'll have access to all classes within that namespace.
-Chris B.
Post Reply