locked
Cannot create an instance of my class RRS feed

  • Question

  • User1984700640 posted

    Hi all,

    I am working on an ASP.Net 3.5 solution that I did not originally build.  I added a class file named Incident.vb to the App_Code folder like I always do.  But in my code behind of a web page, I usually create an instance of a class like:

    Dim oIncident as New Incident

    But after I type "New", normally I would see my class file he intellisense but I do not.  So it does not seem to be able to find Incident.vb.  I have not seen this behavior before.  Trust me, my class is correct.  I have created many class files like this.  What would cause this?  Like I said, I did not originally create this solution. 

     

    Thanks,

     

    Bob

     

     

    Wednesday, February 24, 2010 5:34 PM

Answers

  • User2130758966 posted

    Well in c# if you forgot the keyword public before the class definition it would hide it. I am not sure what the equiv is for vb.net off the top of my head?


    Also the other reason is that if you add a class that is in a namespace you will need an imports / using statement at the top to import that namespace before you can use it.


    Another possible reason is that the file isn't compiling correctly.


    A final long shot is that you typed it too fast before the intellisense had updated its internal index and found the new class you made.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2010 6:32 PM

All replies

  • User2130758966 posted

    Well in c# if you forgot the keyword public before the class definition it would hide it. I am not sure what the equiv is for vb.net off the top of my head?


    Also the other reason is that if you add a class that is in a namespace you will need an imports / using statement at the top to import that namespace before you can use it.


    Another possible reason is that the file isn't compiling correctly.


    A final long shot is that you typed it too fast before the intellisense had updated its internal index and found the new class you made.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2010 6:32 PM
  • User1984700640 posted

    Thanks for the reply.

    I'm not using a namespace.  The solution compiles.  I do use Public for my class.  I still cannot access this class in my code.  Here is my code for this simple class:

    Imports System
    Imports System.Configuration
    Imports System.Data
    Imports System.Data.SqlClient
    Imports Microsoft.VisualBasic
    Imports Microsoft.ApplicationBlocks.Data

    Public Class IncidentRegisterBSI

        Protected mstrDBConnectionString As String

        Public Sub New()

        End Sub

        Public Sub New(ByVal DBConnectionString As String)
            Try
                mstrDBConnectionString = DBConnectionString
            Catch ex As Exception
            End Try
        End Sub

       End Class

    Thursday, February 25, 2010 8:12 AM
  • User2130758966 posted

    Hmm ok one final guess, and bear with me because I am c# not vb.

    With c# if you make a website application project (wap) instead of a website project (wsp) you have to actually add | existing file to get the project into the solution. its not enough to just drop the file in the folder.

    oh I just thought... you wouldn't have an app_code if it was a wap.


    im afraid i am now officially stumped... sorry!

    Thursday, February 25, 2010 8:35 AM
  • User1984700640 posted

     

    Yeah, this application has been a nightmare.  The original programmers are long gone so I cannot ask them.  Everything in VS 2008 works differently than what I am used to. 

    I have discovered some interesting things.  There are a few other class files in the app_code folder and they seem to work.  But something is odd.  If I open one of the old class files in VS and start typing Imports at the top of my code, the intellesence kicks in.  Then if I click the space bar after Imports I see all my namespaces as usual.  But in my new class, no intellesense at all and none of the namespaces are there after I finish typing Imports.  Yet if I enter a valid namespace like System,Data it does not create an error.  The old files were created as VB Class files and so was my new class file.  It's like my new class file is not recognized by the solution.  The old developers must have changed some setting but I have no idea what they did.

    Thanks,

    Bob

    Thursday, February 25, 2010 8:58 AM
  • User-379429695 posted

    I know in the past when I created a class, it was not (for one reason or another, possibly due to lack of understanding on my part) avaialble to my web form.  What I was told to do by a fellow coligue was to make it a Public Module like the code below.  The Module name must be the name of your file in the Solution Explorer.  Then within that module, you create your class.  After I did this, it was instantly avaialble.  Give it a shot and let me know the result, it doesnt take long to make this change :)

    Imports System
    Imports System.Configuration
    Imports System.Data
    Imports System.Data.SqlClient
    Imports Microsoft.VisualBasic
    Imports Microsoft.ApplicationBlocks.Data
    
    Public Module IncidentRegisterBSI
    Public Class Incident
    
        Protected mstrDBConnectionString As String
    
        Public Sub New()
    
        End Sub
    
        Public Sub New(ByVal DBConnectionString As String)
            Try
                mstrDBConnectionString = DBConnectionString
            Catch ex As Exception
            End Try
        End Sub
    
       End Class
    End Module


     

    Thursday, February 25, 2010 8:59 AM
  • User1984700640 posted

    Thanks for the suggestion.  I tried the module but that did not work either. 

     

    Bob 

    Thursday, February 25, 2010 9:26 AM
  • User-952121411 posted

    Be careful regardless of using a 'module' in VB.NET.  I typically do not recommend it.  It the equivalent of a 'Public' class with 'Shared' methods which means it does not have to be instantiated and does not fall into the lines of good OOP.  There are valid situations for needing a class that has all methods Shared (like a UI utility class with no behavior other than to support the UI), and in those cases, mark the class Public and then elevate the needed properties or methods to 'Public Shared' so they can be accessed directly.  The Module type is there in .NET IMO as a grandfathered file type from the VB6 days to help make programmers more comfortable making the transition to .NET.

    Now back to your issue... If you feel there is something intrinsically wrong the the class file containing the code try the following.  (Backup your entire project 1st)Copy out all of the code from the class, delete the class from the project, Goto Solution Explorer right-click and select Add->New Item, select Class, give it the proper name, and then paste back in all of your code.

    Essentially what you will have done is recreated the exact same class, but by using a class created in VS.NET 2008 instead of a file that may have been originally created in an older version of VS.NET.  Now I have seen this solution be quite helpful for .aspx pages that have the .designer, and .vb files attached, but have not seen it to be an issue to often with just plain class files, but it is worth a try, and at least will confirm there is nothing wrong with the file.

    Also, confirm that you do have VS.NET 2008 and SP1 installed.  SP1 is a must, because when you being to talk about IDE Intellisense issues, etc it may be beyond the project.

    Friday, February 26, 2010 10:11 AM
  • User1984700640 posted

    I did create my class the way you said.  I always create my classes this way.  I did it again though and I get the same results.   This is very strange.  There must be some setting in VS 2008 that the original developers must have set to cause such behavior.  Not sure if it matters but I am using VS 2008 Express Edition.

    Tuesday, March 2, 2010 7:59 AM
  • User-668710364 posted

    I had a similar problem in VS 2010.  The new classes I added were disconnected from the project.  Intellisense did not work and they did not show up in the namespace when I used the object browser.  The solution is to open the Properties window and change the Build Action from Content to Compile.  Like magic - it was fixed.

    Wednesday, November 16, 2011 5:38 PM