Question

I created a public class and a public sub that contains all the code needed to save my application, and I want to be able to use this sub in other classes. But when I type Call Save.Save() (the first save is the class name, the second the sub name), I get this error: "Reference to a non-shared member requires an object reference."

What am I doing wrong? This worked for public variables...

Thanks in advance!

Here's the formating of my code...

THE SUB:

Public Class Save
    Public Sub Save()
        My.Settings.Variable = Class.Variable
        My.Settings.Variable2 = Class.Variable2
        [...]
        My.Settings.Save()
        MessageBox.Show("Save successful", "Save")
    End Sub

CALLING THE SUB:

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Call Save.Save()
    End Sub
Was it helpful?

Solution

Either mask the class and function Shared or you have to make an instance of the class.

Instance;

Dim _save As New SaveClass
_save.Save()

Or Shared version;

Public Shared Class SaveClass
  Public Shared Function Save(....

Usage;

SaveClass.Save(...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top