ASP.Net Class Import problems

C

chrisd

Heya,
I might be going about this in completely the wrong fasion, I am more
of a PHP developer, but I am trying to build a collection of classes in
ASP.Net to implement in some aspx pages but also in other classes.
I can make an aspx page reference a class Class1 in namespace
NameSpace1 using

<%@ Assembly Src="../App_Code/NameSpace1.vb" %>

I also have a class in another file called DBConn which is in
NameSpace2. Intellisense lets me add 'Imports NameSpace2' or 'Imports
NameSpace2.DBConn' but when I reload the page I get

BC30466: Namespace or type 'NameSpace2' for the Imports 'NameSpace2'
cannot be found.

OR

'BC30466: Namespace or type 'DBconn' for the Imports
'NameSpace2.DBconn' cannot be found.

What am I doing wrong, what is the best way of includign one class
within another.. any help would be much appreciated!!
 
H

Hans Kesting

chrisd said:
Heya,
I might be going about this in completely the wrong fasion, I am more
of a PHP developer, but I am trying to build a collection of classes
in ASP.Net to implement in some aspx pages but also in other classes.
I can make an aspx page reference a class Class1 in namespace
NameSpace1 using

<%@ Assembly Src="../App_Code/NameSpace1.vb" %>

I also have a class in another file called DBConn which is in
NameSpace2. Intellisense lets me add 'Imports NameSpace2' or 'Imports
NameSpace2.DBConn' but when I reload the page I get

BC30466: Namespace or type 'NameSpace2' for the Imports 'NameSpace2'
cannot be found.

OR

'BC30466: Namespace or type 'DBconn' for the Imports
'NameSpace2.DBconn' cannot be found.

What am I doing wrong, what is the best way of includign one class
within another.. any help would be much appreciated!!

The standard way of adding code to an aspx (or ascx) is to use a "codebehind" file.
This makes for a better separation of UI and code.

But this alone will not help you.

Are those classes in a separate project? If so, you need to add a "reference"
to that (class library?) project to you (web) project.
The "Imports" only provides a shorthand (if is't the same as "using" in C#):
you don't have to type the complete namespace for classes within the
"imported" namespace. The compiler will still want to know what is
in that namespace: therefore you need the reference.
(right click on the project, "add reference", ..)

By the way: you don't "include" a class within some other class. You
either create a instance of that class (and use it's instance methods)
or use "static" (sorry, don't know the VB term) methods directly.

Hans Kesting
 
C

chrisd

Hi Hans,
Thanks for taking time to reply.
How do I create a 'codebehind' file?
The two classes are in the same folder (App_Code) .
I thought that Visual Studio would only allow intellisense auto filling
of namespaces if they are correctly referenced..
In any case, when I click on 'Add Reference' I have no idea what to
do.. the browse section is looking for dll's or exes (mine are VB.Net)
and it is not inutitive at all..

With regards to the 'includes' thing. This is just different
terminology, since in PHP, you include the physical file where the
class is found, and then add an object seperately.

In my current situation I have a public static class Class1 and a
public class DBconn..

Regards,
Chris Darke
 
H

Hans Kesting

chrisd said:
Hi Hans,
Thanks for taking time to reply.
How do I create a 'codebehind' file?

I don't know: when I add an aspx/ascx in VS2003, the aspx.cs/ascx.cs
(or I imagine, aspx.vb/ascx.vb) file gets added autimatically. From
what I have heard, there are more options in VS2005.
The two classes are in the same folder (App_Code) .
I thought that Visual Studio would only allow intellisense auto
filling of namespaces if they are correctly referenced..
In any case, when I click on 'Add Reference' I have no idea what to
do.. the browse section is looking for dll's or exes (mine are VB.Net)
and it is not inutitive at all..

Are those classes in a project (separate from your webproject) but in the
same solution? Then there is a "Projects" tab where you can select from
your projects (again, VS2003, but VS2005 shouldn't be too different here).

But you mention the App_Code directory. This is a directory in the
web-project (isn't it?) so you wouldn't need a reference to access
these classes. You can use them by using the correct namespace/classname
(or, with "Imports", just with the classname). I don't think you need
to specify any assembly, as it all works within your web-project assembly.
 
C

chrisd

heya,
the classes are within the same solution, but here is where things get
hazy.
I know that VS2005 referes to 'projects', 'solutions' and 'web sites'.
Now the web site is all there, and was originally built in ASP. I added
on the ASP.Net and VB.Net functions for the new functionality, with the
idea of porting the rest of the site over later (it is an insurance
management system)... so I never actually 'made a project' as such. I
am not sure if this has implications in terms of the structure, but I
just added class files by choosing 'add new item' and it auto created
the App_Code directory.
The fact is that thought the two class files are in the same App_Code
folder, and Intellisense picks them up fine, they dont see each other
at runtime.

Do you think it is the fact that they have to be built over a
'project'? If so, how is it done when you code it by hand without using
VS?

Thanks in advance!
 
L

Larry

Hi Chris:

If I were you I would start a new project completely seperate from your
website. Put the common code you need to reference from your pages
here. This is the general idea though you will have to do some
searching to do this manually. (Why do you have to do it manually? I
see above you're using VS2005 and looking for intellisense).

You need to build a dll which is of output type "class library." In
VS2005 this is easy enough to do: create a new project of this type.
Once you build your project into a dll, add a reference to the dll from
your website.

The reference needs to go in the bin directory of your website. If
there is no bin directory, create one underneath the root (sibling to
app_code) and put your dll there. You should be able to browse to the
directory where your dll was built.

At that point , you should be able to reference your classes and bring
em in with an import or using statement.

Regards,

Larry
 
C

chrisd

Hi Larry,
Thanks for your reply..
So basically you are saying I need to build a seperate project from the
ground up for my asp.net code and I can then reference it from the site
files?
I guess I can do that.. just thought it would be possible to do that
without compiling a DLL.. since my aspx code can reference my vb class
and it all works fine to that point it seems a bit strange that for one
vb class to reference another i suddenly have to start building DLLs!
Oh microsoft..
And why do it manually? Well Visual Studio is just a tool for writing
the code is it now? just like with any other scripting language I just
thought it would be possible to write on a text editor and hence there
might be a cleaner, more manual way of doing it (i guess thats my
PHP/Perl mentality coming out)

Anyways thanks for all the feedback guys!
 

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,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top