Import Class. Not Imports ... ?




In the main root of my web site together with my aspx and aspx.vb files
I have the file global.vb.

This file has a class with all the functions which are used in many
aspx.vb files and it is like this:

Imports System
Namespace Global
Public Class Global
Public Shared Sub Function01()
End Sub
End Class
End Namespace

In my aspx.vb file I am using the function like this:

It's not working. I know I am missing some declaration but the options I
tried are not working. I tried to place in my aspx.vb "Imports Global".




Phillip Ian

You should be getting a message that c:\<path path path>\Global.vb(2):
namespace 'Global' and class 'Global', declared in 'c:\<path path
path>\Global.asax.vb', conflict in namespace '<project name>'.

You can rename your namespace something like GlobalStuff, then change
your imports statement to match. Of course, now your class Global and
the autogenerated class Global will conflict, so you'll have to be
explicit when referring to them, or rename your class as well.



Kevin Spencer

Hi Shapper,

First, you might want to change the name of your namespace and class, as
there is an intrisic class in ASP.Net called "Global" already - hence the
file global.asax. You may be experiencing an ambiguous name reference here,
but you haven't posted your error message, so I can't tell for sure.

The "Imports" statement is a pre-processor statement that allows you to
reference classes and sub-namespaces under a given namespace wihtout having
to type the namespace every time you use it in your code. This can cause an
ambiguous name exception if, for example, you have 2 namespaces with the
same-named class underneath them, and you refer to the class without
refering the namespace in your code. The compiler can't tell which class in
which namespace you're referring to. In your case, as I said, you import
System.Web, and the global.asax file defines a class called "global" that
inherits System.Web.HttpApplication. So, when you refer to
"Global.function1()" the compiler can't tell WHICH Global class you're
referring to.

It is important to understand the difference between namespaces and classes,
and the usage of namespaces. In OOP, you can create a huge number of
classes, such as exist in the CLR. With possibly thousands of classes, it
can become difficult to know when you've given a class the same name as
another one. A namspace is a further identifier for the class, which
references its position in a hierarchy. IOW, a namespace is not a class, but
a container for classes which helps to uniquely identify them.


Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

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