• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 674
  • Last Modified:

Imports for aspx page that Inherits: Reusing code

I have this at the top of my aspx page which works fine
<%@ Page Inherits="GettingStarted" Src="002-GettingStarted.vb" Language="VB" %>

But in the code file
002-GettingStarted.vb

I have an Inports to another class that is in the file MySqlApi.vb
Imports MySqlApi

But, when I run the aspx page, it complains about the Imports line.

How do I let it see the class?
0
rcagley
Asked:
rcagley
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Justin_WCommented:
Use:
   <%@ Import namespace="MySqlApi" %>

Import statements only affect the file they are located within.  So you have to either fully qualify all code or import in both files.
0
 
ajitanandCommented:
hello,

You can only Import NameSpaces, and NOT Classes directly!

So if you have the class MySqlApi in MySqlApi.vb, there are 2 possible ways out:

1. If the class MySqlApi belongs to another NameSpace (for e.g. as MyClasses.MySqlApi), you need to import MyClasses namespace, in your vbcode, to use the class. And if this NameSpace exists in another Assembly (.Net dll), then you need to add its reference in your VB Project that contains your 002-GettingStarted.vb file.

2. If the MySqlApi exists in the same NameSpace as your GettingStarted class, and in the same project, you need not specify ANY Imports statement in your vb code. It is available directly in your VB Project in VS.Net

------------------------

Let me know if this helps / does not help.

rgds,
Ajit Anand
0
 
rcagleyAuthor Commented:
Sorry, I'm not quite there. I'm using webmatrix and not VS.NET (which I'm more used to).

I guess generally I don't understand how to use classes that are in separate files. I can include one file with this
<%@ Page Inherits="TSIC.GettingStarted" Src="002-GettingStarted.vb" Language="VB"  %>

so then in 0002-GettingStarted.vb I want to access a class in a separate file MySqlApi.vb *and* this file wants to access another class which is in a dll i.e.
Imports CoreLab.MySql

Ajit, you're right normally I would just add the reference in my VS.NET project but I don't know how to do that with just my .vb and .aspx files. Any ideas?

thanks for the help
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ajitanandCommented:
In these cases:

Step 1. Here you just need this command to compile your vb files. (It compiles all you vb files in current directory):

vbc /r:CoreLab.dll /r:System.dll /r:System.Web.dll /target:library /out:TSIC.dll /imports:CoreLab.MySql *.vb

[considering that CoreLab.MySQL.* is your assembly name. i.e. a class here will be like CoreLab.MySql.Class1, else you may simply try with /imports:CoreLab]

/r - to add a reference to a dll
/imports - to import a namespace from a dll, used in your .vb files


Step 2. In your ASPX files, no need to use the Src="" attribute. If you place the dll in your bin folder, just specify the Inherits attribute properly, it will work. Take this as example. It works, if you want to follow the code behind concept in Webmatrix as well.

<%@ Page Language="VB" Inherits="StudioWebControls.SmartPage" %>
<%@ Register TagPrefix="uc1" Namespace="MyWebControls" Assembly="MyWebControls" %>
<%@ import Namespace="System.Resources" %>
<%@ import Namespace="System.Globalization" %>


------------------------------

Hope this helps.

rgds,
Ajit Anand
0
 
Justin_WCommented:
As long as the DLL is in the BIN directory of your web app, your web app will see the DLL.  Then you just need to fully qualify, like this:
   CoreLab.MySql.SomeClass.SomeFunction()
or import, as I showed above.
0
 
rcagleyAuthor Commented:
Thanks. Got it to work. I now know a ton more about namespaces/dlls and how to use them in my webpage.
0
 
cmgarnettCommented:
Here's a  voice from the past for you!

Justin, you seem to be well versed in the ways of dlls and vbc, I was wondering if you cold clarify something for me.

I have been working through some examples in an attempt to get to grips with ASP.NET and have tripped on a fairly early hurdle.

I created a .vb file called database.vb which defined a namespace called database and a public class called database. I compiled this using
"vbc /debug /nologo /t:library /r:System.dll,System.Xml.dll,System.Web.dll,System.Data.dll %1.vb" where parameter %1 = database.vb. I used the resultant .dll in a .aspx web page without any trouble.

The next stage goes on to create a new .vb file called baseclass.vb which must import the earlier database.vb file, and here the fun begins.

Is this the correct way to compile my baseclass.vb file
"vbc /debug /nologo /t:library /imports:%2.dll /r:System.dll,System.Xml.dll,System.Web.dll,System.Data.dll %1.vb" where parameter %1 = baseclass and parameter %2 = database.database?

Any help would be appreciated. I have raised a question which is still awaiting an answer http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21597265.html
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now