Results 1 to 7 of 7

Thread: comPLETELY stumped: referencing class located within vb.net web application

  1. #1
    kenton311
    Guest

    Angry comPLETELY stumped: referencing class located within vb.net web application

    Hello All,
    I am in the process of learning .NET....I created a simple class in my ASP.NET web application (using VB.NET) to access my database to return a list of states to populate a user control I made to display these states. Yet when I try to Import this class and then create a new instance of it, the parser returns an error like this :

    Namespace or type 'Class1' for the Imports 'WebApplication1.MyClassNameSpace.Class1' cannot be found

    the program line on which this error occurs is :

    Imports WebApplication1.MyClassNameSpace.Class1

    this line is at the very top of the code-behind file for my aspx page, AND I can drag & drop that line from the Class View which tells me the project "knows" about my class, yet somehow can't find it at runtime...

    i've created smaller web projects with the sole intention of just getting a class defined by myself to import into another .vb file and simply CAN'T get it to work, and neither can my co-workers....also, i have successfully created this class in a separate project, compiled it, and referenced it within this web app, so i am left with thinking that you must do something special to a class you define within a web application in order to make it accessible to other classes in the same web app.

    does anyone have any insight into this problem??

    Thank you very much for your time,
    Kenton Taylor

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    is the class wrapped in said namespaces
    ???
    Code:
    Namespace WebApplication1
         Namespace MyClassNameSpace
    really though, I dont think you need the namespaces if you are using a straight forward class..just reference the class as is if it is added as part of the application

    Dim A As New Class1

    you would want to use the namespace if the class in a seperate dll.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I haven't got much experience with .NET (yet), but this is what I read somewhere:
    If the class is in a seperate dll, the dll must be placed in the same directory, or a subdirectory of your web application, or the dll must be installed in the global assembly cache.

  4. #4
    kenton311
    Guest

    reply to cander

    the namespaces don't make any difference...
    here's my code:


    Class1.vb located within WebApplication1

    Public Class Class1

    Public MyMember As String

    Sub New()
    MyMember = "constructor worked"
    End Sub

    End Class



    WebForm2.aspx.vb located within WebApplication1

    Imports WebApplication1.Class1

    Public Class WebForm2
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here

    Dim ThisWontWork As Class1

    ThisWontWork = New Class1()

    End Sub

    End Class


    the exact error msg is:
    Compiler Error Message: BC30466: Namespace or type 'Class1' for the Imports 'WebApplication1.Class1' cannot be found.

    Line 2: Imports WebApplication1.Class1


    can anyone else get this simple example to work?? Just a simple class within a web application, where an aspx.vb file can import and dim an instance of that class. completely stumped.

    -Kenton

  5. #5
    Thelonius
    Guest
    You shouldn't have to make any references as the class is really part of your project. It's very similar to how you could create a class in VB6 and all references are "created" when you add that module to your project. If you created a dll, you would have to reference it, but if it's your standard run of the mill class, no referencing or importing is necessary.

  6. #6
    Junior Member
    Join Date
    Feb 2002
    Location
    London UK
    Posts
    29
    Remove the class reference from the end of the imports line.

    It is trying to find Class1 in WebApplication.Class1

    The imports line should be

    imports WebApplication

    hope this helps

    cheers
    Simon

  7. #7
    kenton311
    Guest

    I figured it out

    so for any who want to know - the solution to this problem is this :
    in order to reference a class that exists within a .net web application at runtime, you first have to build the solution before running an aspx or aspx.vb file, otherwise it will come back type not found, even though the project is aware of that class at design time.

    case closed.
    kenton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width