Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > 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.

Reply
Thread Tools

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.

 
 
DJ Dev
Guest
Posts: n/a
 
      02-07-2004
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!
 
Reply With Quote
 
 
 
 
Gandalf
Guest
Posts: n/a
 
      02-07-2004
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: om...
> 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!



 
Reply With Quote
 
 
 
 
DJ Dev
Guest
Posts: n/a
 
      02-08-2004
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: om...
> > 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!

 
Reply With Quote
 
Gandalf
Guest
Posts: n/a
 
      02-08-2004
There's always a way out, although maybe not the exact one you want.

"DJ Dev" <> wrote in message
news: om...
> 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: om...
> > > 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!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why can't an instance instantiated within a class method access aprotected instance method? Greg Hauptmann Ruby 9 06-16-2008 10:16 AM
instance method adding another instance method to the class Raj Singh Ruby 2 05-29-2008 10:09 PM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
inner class, explicit outer class method call Yamin Java 4 10-24-2004 06:17 AM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57