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

  • Thread starter Thread starter DJ Dev
  • Start date Start date
D

DJ Dev

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!
 
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.
 
So u mean that there s no way out??

Gandalf said:
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.
 
Back
Top