BC30369: 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
You have tried to refer to a non-shared member of a class from within a shared procedure. The following example demonstrates such a situation:
Class Sample
Public x as Integer
Public Shared Sub SetX()
x = 10
End Sub
End Class
In the preceding example, the assignment statement x = 10
generates this error message. This is because a shared procedure is attempting to access an instance variable.
The variable x
is an instance member because it is not declared as Shared. Each instance of class Sample
contains its own individual variable x
. When one instance sets or changes the value of x
, it does not affect the value of x
in any other instance.
However, the procedure SetX
is Shared
among all instances of class Sample
. This means it is not associated with any one instance of the class, but rather operates independently of individual instances. Because it has no connection with a particular instance, setX
cannot access an instance variable. It must operate only on Shared
variables. When SetX
sets or changes the value of a shared variable, that new value is available to all instances of the class.
Error ID: BC30369
To correct this error
Decide whether you want the member to be shared among all instances of the class, or kept individual for each instance.
If you want a single copy of the member to be shared among all instances, add the
Shared
keyword to the member declaration. Retain theShared
keyword in the procedure declaration.If you want each instance to have its own individual copy of the member, do not specify
Shared
for the member declaration. Remove theShared
keyword from the procedure declaration.
See also
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for