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

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!
 
G

Gandalf

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

DJ Dev

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top