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. Advertising

  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. Advertising

  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. Advertising

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. 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:
    839
    In therapy
    Aug 23, 2008
  2. 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:
    162
    In therapy
    Aug 23, 2008
  3. Web Search Store

    refer to a public shared class in app_code folder

    Web Search Store, Jul 3, 2008, in forum: ASP .Net Web Services
    Replies:
    5
    Views:
    187
    In therapy
    Aug 23, 2008
  4. Raj Singh
    Replies:
    2
    Views:
    186
    Rick DeNatale
    May 29, 2008
  5. Greg Hauptmann
    Replies:
    9
    Views:
    233
    Loren Segal
    Jun 16, 2008
Loading...

Share This Page