Cannot refer to an instance member of a class from within a shared method or shared member initializ

Discussion in 'ASP .Net' started by DJ Dev, Feb 7, 2004.

  1. DJ Dev

    DJ Dev Guest

    I have a shared sub which does some stuff on an ASP Table
    (<asp:Table>). The code is
    Public Class MyClass Inherits System.Web.UI.Page

    Public WithEvents myTable As System.Web.UI.WebControls.Table

    Public shared sub Doit()
    'Do some stuff
    myTable.Rows.Add(r)
    End Sub

    When I call this shared sub from another class, I get an error :

    Cannot refer to an instance member of a class from within a shared
    method or shared member initializer without an explicit instance of
    the class.

    I know that the problem is that when I call it, the calling function
    (from the other class) doesnt know in what reference myTable is. But I
    dont know how to fix this. Please help. :(

    Thanks!
     
    DJ Dev, Feb 7, 2004
    #1
    1. Advertisements

  2. DJ Dev

    Gandalf Guest

    Change it to...

    Public sub Doit()
    'Do some stuff
    myTable.Rows.Add(r)
    End Sub

    Shared subs don't require the class to be instantiated. However, myTable
    won't exist until the class is instantiated since it is itself an instance
    of the Table class. Thus the rule is: whereas you can reference shared items
    from an instance sub, you cannot reference instance items from a shared sub.

    --

    "DJ Dev" <> wrote in message
    news:...
    > I have a shared sub which does some stuff on an ASP Table
    > (<asp:Table>). The code is
    > Public Class MyClass Inherits System.Web.UI.Page
    >
    > Public WithEvents myTable As System.Web.UI.WebControls.Table
    >
    > Public shared sub Doit()
    > 'Do some stuff
    > myTable.Rows.Add(r)
    > End Sub
    >
    > When I call this shared sub from another class, I get an error :
    >
    > Cannot refer to an instance member of a class from within a shared
    > method or shared member initializer without an explicit instance of
    > the class.
    >
    > I know that the problem is that when I call it, the calling function
    > (from the other class) doesnt know in what reference myTable is. But I
    > dont know how to fix this. Please help. :(
    >
    > Thanks!
     
    Gandalf, Feb 7, 2004
    #2
    1. Advertisements

  3. DJ Dev

    DJ Dev Guest

    So u mean that there s no way out??

    "Gandalf" <> wrote in message news:<eD#>...
    > Change it to...
    >
    > Public sub Doit()
    > 'Do some stuff
    > myTable.Rows.Add(r)
    > End Sub
    >
    > Shared subs don't require the class to be instantiated. However, myTable
    > won't exist until the class is instantiated since it is itself an instance
    > of the Table class. Thus the rule is: whereas you can reference shared items
    > from an instance sub, you cannot reference instance items from a shared sub.
    >
    > --
    >
    > "DJ Dev" <> wrote in message
    > news:...
    > > I have a shared sub which does some stuff on an ASP Table
    > > (<asp:Table>). The code is
    > > Public Class MyClass Inherits System.Web.UI.Page
    > >
    > > Public WithEvents myTable As System.Web.UI.WebControls.Table
    > >
    > > Public shared sub Doit()
    > > 'Do some stuff
    > > myTable.Rows.Add(r)
    > > End Sub
    > >
    > > When I call this shared sub from another class, I get an error :
    > >
    > > Cannot refer to an instance member of a class from within a shared
    > > method or shared member initializer without an explicit instance of
    > > the class.
    > >
    > > I know that the problem is that when I call it, the calling function
    > > (from the other class) doesnt know in what reference myTable is. But I
    > > dont know how to fix this. Please help. :(
    > >
    > > Thanks!
     
    DJ Dev, Feb 8, 2004
    #3
  4. DJ Dev

    Gandalf Guest

    There's always a way out, although maybe not the exact one you want. ;-)

    "DJ Dev" <> wrote in message
    news:...
    > So u mean that there s no way out??
    >
    > "Gandalf" <> wrote in message

    news:<eD#>...
    > > Change it to...
    > >
    > > Public sub Doit()
    > > 'Do some stuff
    > > myTable.Rows.Add(r)
    > > End Sub
    > >
    > > Shared subs don't require the class to be instantiated. However, myTable
    > > won't exist until the class is instantiated since it is itself an

    instance
    > > of the Table class. Thus the rule is: whereas you can reference shared

    items
    > > from an instance sub, you cannot reference instance items from a shared

    sub.
    > >
    > > --
    > >
    > > "DJ Dev" <> wrote in message
    > > news:...
    > > > I have a shared sub which does some stuff on an ASP Table
    > > > (<asp:Table>). The code is
    > > > Public Class MyClass Inherits System.Web.UI.Page
    > > >
    > > > Public WithEvents myTable As System.Web.UI.WebControls.Table
    > > >
    > > > Public shared sub Doit()
    > > > 'Do some stuff
    > > > myTable.Rows.Add(r)
    > > > End Sub
    > > >
    > > > When I call this shared sub from another class, I get an error :
    > > >
    > > > Cannot refer to an instance member of a class from within a shared
    > > > method or shared member initializer without an explicit instance of
    > > > the class.
    > > >
    > > > I know that the problem is that when I call it, the calling function
    > > > (from the other class) doesnt know in what reference myTable is. But I
    > > > dont know how to fix this. Please help. :(
    > > >
    > > > Thanks!
     
    Gandalf, Feb 8, 2004
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    5,665
    Thomas Weidenfeller
    Oct 12, 2005
  2. Web Search Store

    refer to a public shared class in app_code folder

    Web Search Store, Jul 3, 2008, in forum: ASP .Net
    Replies:
    5
    Views:
    1,012
    In therapy
    Aug 23, 2008
  3. marekw2143
    Replies:
    3
    Views:
    1,568
    marekw2143
    Jul 25, 2009
  4. Web Search Store

    refer to a public shared class in app_code folder

    Web Search Store, Jul 3, 2008, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    334
    In therapy
    Aug 23, 2008
  5. David Garamond
    Replies:
    5
    Views:
    435
    Ara.T.Howard
    Jun 8, 2004
  6. Raj Singh
    Replies:
    2
    Views:
    335
    Rick DeNatale
    May 29, 2008
  7. Greg Hauptmann
    Replies:
    9
    Views:
    474
    Loren Segal
    Jun 16, 2008
  8. Chuck Remes
    Replies:
    3
    Views:
    376
    hemant
    Aug 12, 2010
Loading...