Inherit a class which is in a separate file ?

M

mister.mwa

Hello,

I have the following problem:
I have a class Foo, and a class Bar.

I want Foo to inherit from Bar, but i want to put them in separate
files Foo.vb and Bar.vb. Then i will use the Foo Class in a code behind
separate file called CodeBehind.vb

Then i will use this CodeBehind normally in mypage.aspx.

I don't know the syntax to do this. Do i need to include something
somewhere, or what ? How to link thoses items together without putting
everything in the same file ?
1- How to link foo.vb and bar.vb ?
2- How to link Codebehind.vb and foo.vb ?
I am asking please the concrete syntax to achieve this.

I have converted a huge application from asp to asp.net, and now i need
code behind to be able to compile it. Includes lead me to multiple page
attributes errors, this is why i am asking this.

Thanks for your precious help.
 
D

David Browne

Hello,

I have the following problem:
I have a class Foo, and a class Bar.

I want Foo to inherit from Bar, but i want to put them in separate
files Foo.vb and Bar.vb. Then i will use the Foo Class in a code behind
separate file called CodeBehind.vb

Then i will use this CodeBehind normally in mypage.aspx.

I don't know the syntax to do this. Do i need to include something
somewhere, or what ?

No. Foo.vb, Bar.vb and CodeBehind.vb just need to be in the same project.
When you build the project, all the .vb files in the project (including
code-behind files) are compiled into a .dll. Any public class in the .dll
will be accessable from any of your pages.

David
 
M

mister.mwa

Thank you, but i am using notepad to program, not visual studio. And i
want to debug and run the software prior to compiling.

Anyone can answer my first question please ? If it is impossible to do,
please tell me.

Thanks.
 
D

David Browne

Thank you, but i am using notepad to program, not visual studio. And i
want to debug and run the software prior to compiling.

Anyone can answer my first question please ? If it is impossible to do,
please tell me.

Thanks.

Ok. You should have said so.

First, tool up:

Here's a tool to do your builds
http://nant.sourceforge.net/

Here's a free development environment for ASP.NET
http://asp.net/webmatrix/

But to finally answer your question:

Foo.vb
---------------------
option explicit
option strict on

class foo

end class



Bar.vb
------------------
option explicit
option strict on
class bar
inherits foo
end class


CodeBehind.vb
--------------------
option explicit
option strict on
class codebehind
Inherits System.Web.UI.Page


public shared sub foo
dim b as new bar()
end sub

end class


To build these three classes into a library run (from the directory
containing these files):

vbc /out:foo.dll /target:library /r:System.Web.dll /r:System.dll
/recurse:*.vb

the VB compiler is in the .NET framework directory so it needs to be in your
path.

Also see the .NET framework SDK for many more examples of command-line
compilation.


Then in your aspx pages you reference foo.dll by adding an @assembly
directive
eg

<%@ Assembly Src="bin\foo.dll" %>

This directs ASP.NET to add a reference to this dll (see /r:xxx.dll above)
to the VBC command like when it compiles the ASPX file into a VB file.

You can find details of that process in the

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\MyApp

folder. You can find there the VB file into which the aspx file is
transformed, and the vbc command line used to compile it.




David
 
M

mister.mwa

Thank you David, it works great. However i would like to use the
assembly directive without compiling first. On MSDN documentation it is
written that i can use the assembly directive to link to a source .vb
file.

Your sniplet works fine if i pre-compile a part of my program, and it
is great for testing purpose. But i have to work on a huge asp.net
application, and this is why i would like to be able to run all the
program using Code Behind and assembly directive "on the fly", i mean
without any pre-compilation. I have tried to use the <%@ assembly
src="..." %> directive but it doesn't seem to work the way i am using
it.

Here is my test with 3 files in the same folder : Include2.vb
HelloWorld2.vb HelloWorld.aspx

Include2.vb
----------------------------------
Imports System
Imports System.Web.UI

Public Class MyInclude2
Inherits Page

Shared Function MyStrToUpper(byval strText as string)
MyStrToUpper = Microsoft.VisualBasic.Strings.Ucase(strText)
End Function

End Class


HelloWorld2.vb
-----------------------------------------
Imports System
Imports System.Web.UI

Public Class MyHelloWorld2
Inherits MyInclude2

Private sub Page_Load()
System.Web.HttpContext.Current.Response.write(MyStrToUpper("Hello
World !"))
End Sub

End Class

HelloWorld.aspx
-----------------------------------------------------
<%@ Assembly src="Include2.vb" %>
<%@ Page Inherits="MyHelloWorld2" src="HelloWorld2.vb" %>



This is the result i get when i run my page HelloWorld.aspx in Internet
Explorer 6
----------------------------------------------------------------------------------------------------------------
Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

Compiler Error Message: BC30002: Type 'MyInclude2' is not defined.

Source Error:



Line 3:
Line 4: Public Class MyHelloWorld2
Line 5: Inherits MyInclude2
Line 6:
Line 7: Private sub Page_Load()


Source File: D:\dev\test\HelloWorld_Include_CodeBehind\HelloWorld2.vb
Line: 5



Show Detailed Compiler Output:


c:\windows\system32\inetsrv>
"c:\windows\microsoft.net\framework\v1.1.4322\vbc.exe" /t:library
/utf8output /R:"c:\windows\microsoft.net\framework\v1.1.4322\temporary
asp.net
files\root\feae588f\4f1f1709\assembly\dl2\b219fab7\e08a7a39_6310c501\helloworld_.dll"
/R:"c:\windows\assembly\gac\system.web.mobile\1.0.5000.0__b03f5f7f11d50a3a\system.web.mobile.dll"
/R:"c:\windows\microsoft.net\framework\v1.1.4322\temporary asp.net
files\root\feae588f\4f1f1709\assembly\dl2\54786a7b\40bf0fcf_1a10c501\mypage.dll"
/R:"c:\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll"
/R:"c:\windows\assembly\gac\system.data\1.0.5000.0__b77a5c561934e089\system.data.dll"
/R:"c:\windows\assembly\gac\system.enterpriseservices\1.0.5000.0__b03f5f7f11d50a3a\system.enterpriseservices.dll"
/R:"c:\windows\assembly\gac\system.web\1.0.5000.0__b03f5f7f11d50a3a\system.web.dll"
/R:"c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll"
/R:"c:\windows\assembly\gac\system.web.services\1.0.5000.0__b03f5f7f11d50a3a\system.web.services.dll"
/R:"c:\windows\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll"
/R:"c:\windows\microsoft.net\framework\v1.1.4322\temporary asp.net
files\root\feae588f\4f1f1709\assembly\dl2\6f380172\e0538477_1510c501\quote.dll"
/out:"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET
Files\root\feae588f\4f1f1709\1c4gcic1.dll" /D:DEBUG=1 /debug+
"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET
Files\root\feae588f\4f1f1709\1c4gcic1.0.vb"


Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322.573
Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.

D:\dev\test\HelloWorld_Include_CodeBehind\HelloWorld2.vb(5) : error
BC30002: Type 'MyInclude2' is not defined.

Inherits MyInclude2
~~~~~~~~~~
D:\dev\test\HelloWorld_Include_CodeBehind\HelloWorld2.vb(8) : error
BC30451: Name 'MyStrToUpper' is not declared.

System.Web.HttpContext.Current.Response.write(MyStrToUpper("Hello
World !"))
~~~~~~~~~~~~






Show Complete Compilation Source:


Line 1: Imports System
Line 2: Imports System.Web.UI
Line 3:
Line 4: Public Class MyHelloWorld2
Line 5: Inherits MyInclude2
Line 6:
Line 7: Private sub Page_Load()
Line 8:
System.Web.HttpContext.Current.Response.write(MyStrToUpper("Hello
World !"))
Line 9: End Sub
Line 10:
Line 11: End Class





--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573;
ASP.NET Version:1.1.4322.573



====> I have tried to do as indicated in MSDN, and i know the
Include2.vb is processed (because i have tried to put an obvious
mistake in it and it was detected at first). But when it comes to
inheritance in HelloWorld2.vb, the class Include2 seems completely
ignored. Do you have an idea of what's going on ?

Thank you for your great help.
 
D

David Browne

Thank you David, it works great. However i would like to use the
assembly directive without compiling first. On MSDN documentation it is
written that i can use the assembly directive to link to a source .vb
file.

Your sniplet works fine if i pre-compile a part of my program, and it
is great for testing purpose. But i have to work on a huge asp.net
application, and this is why i would like to be able to run all the
program using Code Behind and assembly directive "on the fly", i mean
without any pre-compilation. I have tried to use the <%@ assembly
src="..." %> directive but it doesn't seem to work the way i am using
it.

Here is my test with 3 files in the same folder : Include2.vb
HelloWorld2.vb HelloWorld.aspx

Include2.vb
----------------------------------
Imports System
Imports System.Web.UI

Public Class MyInclude2
Inherits Page

Shared Function MyStrToUpper(byval strText as string)
MyStrToUpper = Microsoft.VisualBasic.Strings.Ucase(strText)
End Function

End Class


HelloWorld2.vb
-----------------------------------------
Imports System
Imports System.Web.UI

Public Class MyHelloWorld2
Inherits MyInclude2

Private sub Page_Load()
System.Web.HttpContext.Current.Response.write(MyStrToUpper("Hello
World !"))
End Sub

End Class

HelloWorld.aspx
You might try:
<%@ Assembly src="Include2.vb" %>
<%@ Assembly src="HelloWorld2.vb" %>
<%@ Page Inherits="MyHelloWorld2" %>

But when you do this the pages will each have their own copy of Include2 and
HelloWorld2. If you have 500 pages, you will have 500 different copies of
Include2.vb compiled and loaded into memory. They won't actually inherit
the same type, and they won't be able, for instance, so share data stored in
shared scope in the referenced assemblies.

In order to actuall share code among all your pages, the shared code must be
pre-compiled.

David
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top