locked
Writing Shared Subroutines in a Class RRS feed

  • Question

  • User-72198832 posted
    The following gives error on both Session() and Server.mappath() functions
    Error is " "Cannot refer to an instance member of a class from within a shared method...."

    Need to write some subroutines that can be called from other forms

    See my code below.

    Any Suggestions?

    Lee

    Public Class CommonFunctions Inherits System.Web.UI.Page Public Shared Sub GetPdfFile(ByVal FileIndex As String, ByVal Index As String) Dim FILENAME As String = Server.MapPath("data\" & Session("Serial") & "\Command.txt")
    Wednesday, August 31, 2011 11:33 PM

Answers

  • User1642208711 posted

    Session and Server properties are instance based. It cannot be accessed from a shared method.

    Use HttpContext.Current property to access those properties.

    Change below code

    Session("XXX")=Server.mappath()

    to

    HttpContext.Current.Session("XXX")=HttpContext.Current.Server.mappath()
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2011 1:24 AM

All replies

  • User2019981500 posted

    Hi I believe it is because of

    Dim FILENAME As String 
    Try to change above statement with below

     Private Shared FILENAME As String

    Refer this article

    http://support.microsoft.com/kb/308371
    Thursday, September 1, 2011 1:02 AM
  • User-72198832 posted

    Sorry, that's not the problem.

    Any other ideas?

    This is valid....

    Public Sub MySub ()

        Session("XXX")=Server.mappath()

    end sub

    This is not....

    Public Shared Sub MySub ()

        Session("XXX")=Server.mappath()

    end sub

    Thursday, September 1, 2011 1:18 AM
  • User1642208711 posted

    Session and Server properties are instance based. It cannot be accessed from a shared method.

    Use HttpContext.Current property to access those properties.

    Change below code

    Session("XXX")=Server.mappath()

    to

    HttpContext.Current.Session("XXX")=HttpContext.Current.Server.mappath()
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2011 1:24 AM
  • User2019981500 posted

    Raja is right..

    Thursday, September 1, 2011 1:28 AM
  • User-72198832 posted

    Raja:

    Works great!

    Thanks

    Lee

    Thursday, September 1, 2011 8:29 AM