Calling a function from within a shared function

H

Heath P. Dillon

Hi,

New to this.

I have a FileSystemWatcher that monitors for a file change/create event.
Once this filesystem watch is called then it executes a private shared
sub...

Within that sub I need to call another sub(that is called in many other
places in my code) that does some other processing, but when I try to call
that sub from within the shared sub, I get the following error?

Is there away around this error ??


Error 70 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.
 
T

Tim Patrick

Visual Basic does not allow a "shared" member to call an "instance" member
directly. A shared member, declared using the "Shared" keyword, is shared
among all instances of your class--as the name implies. Non-shared members
exist as if each instance of your class has its very own version of that
member. When you try to call the instance member from the shared member,
Visual Basic doesn't know which of the potentially millions of instances
of that member should be used.

There are a few different solutions.

1. Turn the calling shared method into an instance method by removing the
Shared keyword from its definition.

2. Turn the called instance method into a shared method by adding the Shared
keyword to its definition.

3. Call your shared method, passing it a specific instance of your class
as an argument. Then the shared method can use that parameter to access the
specific instance's version of the called method. Here is some code that
shows this variation.
 

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

Top