Shared property works in VS2003, does not in VS2005

I'm moving an asp.net program made in Visual Studio 2003 over to VS2005. I'm just creating the pages in VS2005 and copying & pasting code. I'm finding my Shared variables are not working in VS2005, as I get the error "Name 'Errors' is not declared". In the example below, I have the Class "Errors" (a web page, "Errors.aspx") which sets the property. Then I show the Sub addFootnoteToPDF() which is on another page (open.aspx); this makes a reference to the property in Errors.aspx. Can you see why this works in VS2003 and not in VS2005 Thank you!

Partial Class Errors
Inherits System.Web.UI.Page
Private Shared m_ReferringURL As String

Public Shared Property ReferringURL() As String
Get
Return m_ReferringURL
End Get
Set(ByVal Value As String)
m_ReferringURL = Value
End Set
End Property
End Class

Partial Class Open
Public Sub addFootnoteToPDF()
Errors.ReferringURL = Request.Url.AbsoluteUri 'Says "Error 2 Name 'Errors' is not declared".
End Sub
End Class



Answer this question

Shared property works in VS2003, does not in VS2005

  • Daniel Krebs

    Well, if I take out any reference to "Errors" class, it compiles fine. 

    I just cannot see why these Shared properties can't be found by my other class (Open).

    Did you see my comment a couple of messages above asking how to "extend" the Errors class

    I changed the definition of the Errors class to "Public Class Errors" and that didn't help either.

    If I take the Errors.aspx code and stick the whole class inside of a class module that is located in the App_Code folder, then it finds the shared properties like it should.  I guess I have to find some information on how this is supposed to work in VS2005.

    What I will do for now is to put that code in a class file instead of aspx file.  Then I will perform the rest of the code in the Open class.



  • Bruce H

    Yes, your console code works fine for me. I made the console application easily with your code.

    I'm running into all sorts of weird stuff in VS2005. I tried to make a Global.asax and it looks like a html screen like if I went to HTML view in VS2003 instead of looking like a code-behind page.

    Maybe I just need to start with a clean slate.


  • Batya

    Also, where is the rest of these classes Somewhere you should have a "Public Class Errors" and then the partial one extends it.

  • CET PRG455

    rkimble, I missed this comment and this may be the problem.

    What do you mean I should have a "Public Class Errors" and then the partial one extends it

    How can I "extend" it

    Here's what my Errors class looks like:

    Partial Class Errors
    Inherits System.Web.UI.Page
    'My code is in here...
    End Class


  • johnny_no1_boy

    Shared variables seem to work OK for me....

    Created a console application, this seemed to work as expected for the shared property and variable.

    In you project is the Errors class visible to the the Open Class. Is it in the same project with friend or public scoping. If its private it wouldnt be visible and if its defined as friend or left as default in another project/class library then it wouldnt be visible.

    Module Module1

    Sub Main()
    Dim y As New Open

    y.addFootnoteToPDF()

    MsgBox(Errors.ReferringURL)
    Errors.ReferringURL = "Set In Main"
    MsgBox(Errors.ReferringURL)

    y.addFootnoteToPDF()
    MsgBox(Errors.ReferringURL)

    Errors.ReferringURL = "" '//Reset to original


    Dim z As New Open
    z.addFootnoteToPDF()

    MsgBox(Errors.ReferringURL)
    Errors.ReferringURL = "Set In Main"
    MsgBox(Errors.ReferringURL)
    z.addFootnoteToPDF()
    MsgBox(Errors.ReferringURL)

    End Sub

    End Module

    Partial Class Errors
    Private Shared m_ReferringURL As String

    Public Shared Property ReferringURL() As String
    Get
    Return m_ReferringURL
    End Get
    Set(ByVal Value As String)
    m_ReferringURL = Value
    End Set
    End Property
    End Class


    Partial Class Open
    Public Sub addFootnoteToPDF()
    Errors.ReferringURL = "Set In addFootnoteToPDF"
    End Sub
    End Class


  • Sepp8181

    rkimble, I didn't have to declare it in VS2003. Since my properties are Shared, I don't create an object instance, but refer to the class directly. What's happening to me is very strange.

    I don't know if it's due to these "partial" classes or what.


  • Jimmy_fingers

    No, rkimble, I copied the code a function and property at a time from VS2003 to a new VS2005 project. I did this for both classes and noticed in the Open class that it had an underline with the "not declared" message. I tried building it for grins and of course it failed

    I will comment out the references to Errors in Open just to see if it tells me anything.

    I appreciate your help!


  • Philski

    Well, you would normally only have a "Partial Class" definition if there is already a definition in another code file. If this is the only place that you have code for the Error Class then change it's definition to "Public Class Errors" and forget about the Partial keyword.



  • Alan M.

    The code you posted does not show a declaration of an instance of the Errors class in the Open class. So the error makes sense. You need to declare a variable Errors as Errors somewhere in Class Open and then set Errors variable to an instance of the Errors class.

  • Littletommy

    Oh - right.

    Did you try building the project after defining the Errors class but before accessing it in the Open class



  • Chuck Heatherly

    So if you create a new simple console application exactly using my code pasted in - does this work or not on VS2005 ....

    I'm using VS 2005 and VB Express and it works fine on both.


  • Arun Choudhary

    This is so strange and annoying. As I see it, my code should work fine. I looked at yours and your's works similar to mine. I should not be getting this "not declared" message. I don't know where to go from here.

    Thank you for looking at this! I'll look deeper.


  • Shared property works in VS2003, does not in VS2005