Reference to a non-shared member requires an object reference

The error message Reference to a non-shared member requires an object reference in VB.NET indicates that you are trying to access a non-shared (instance) member of a class or object without creating an instance of the class first. Non-shared members are specific to instances of the class and cannot be accessed through the class name itself. You need to create an object of the class and use it to access instance members.

How to fix?

To fix this error, you must first create an instance of the class and then call the non-shared member on the instance.

For example, the following code will cause the error:

' MyClass is a class with a non-shared member called MyMethod(). MyMethod()

To fix the error, you must first create an instance of the MyClass class and then call the MyMethod() method on the instance:

' Create an instance of the MyClass class. Dim myClass As New MyClass() ' Call the MyMethod() method on the instance. myClass.MyMethod()

You can also avoid this error by using shared members. Shared members can be called without first creating an instance of the class.

For example, the following code will not cause the error:

' MyClass is a class with a shared member called SharedMethod(). SharedMethod()

Here's a detailed explanation with examples:

Non-Shared (Instance) Members

Non-shared members are associated with an instance of a class and are used to store and manipulate data unique to each instance. These members can't be accessed directly through the class itself but require an instance of the class.

Class MyClass Public Property MyProperty As Integer End Class ' Attempting to access an instance member directly results in the error Dim value As Integer = MyClass.MyProperty ' Error: Reference to a non-shared member requires an object reference ' To access the instance member, create an object of the class Dim obj As New MyClass() obj.MyProperty = 55 ' This is how you access the instance member

Shared (Static) Members

Shared members are associated with the class itself and can be accessed using the class name. They are shared among all instances of the class.

Class MyClass Public Shared Property MyProperty As Integer End Class ' Accessing a shared member directly through the class name MyClass.MyProperty = 55 Dim value As Integer = MyClass.MyProperty

Resolving the Error

To resolve the error, you need to create an object of the class and use it to access non-shared (instance) members. Shared members can be accessed using the class name.

Dim obj As New MyClass() obj.MyProperty = 55 ' Accessing an instance member MyClass.MyProperty = 55 ' Accessing a shared member directly through the class name

Conclusion

This error typically occurs when you mistakenly attempt to access instance members as if they were shared members or when you forget to create an instance of the class. It's essential to understand the distinction between shared and non-shared members to use them correctly in your VB.NET code.