cannot refer to an instance member of a class from within a shared


I am trying to get an AJAX call working from working with VB. However, I am getting the error as per the above with when I am trying to pass an Id to a new method.

the code I am using is

<System.Web.Services.WebMethod()> _
    Public Shared Function writeSingleRecord(ByVal id As Long) As Boolean


            Return True

        Catch ex As Exception
            Return False

        End Try

    End Function

Public Sub PrepareAndPassData(id As Long)
        Dim listOfParameters As New List(Of SqlParameter)

        Dim validatedPaymentsParam As New SqlParameter("@Id's", SqlDbType.BigInt)
        validatedPaymentsParam.Value = getValidatedPayments()

        'Dim dt = DataAccessLayer.GetDataAsDatatable(listOfParameters, _connstr, "usp_GetValidatedRecordsForPayment")
            WriteToExcel(DataAccessLayer.GetDataAsDatatable(listOfParameters, _charterProdConnstr, "usp_GetValidatedRecordsForPayment"))

        Catch ioEx As System.IO.IOException

            PassToErrorHandler(ioEx, System.Reflection.MethodInfo.GetCurrentMethod().ToString(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name)

        Catch ex As Exception
            PassToErrorHandler(ex, System.Reflection.MethodInfo.GetCurrentMethod().ToString(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name)

        End Try
    End Sub

Open in new window

if I make the method a shared method this then causes the same error on my other methods.

In the method of WriteToExcel() if I make this a shared sub then Server.MapPath has this same issue too, along with other methods that are referred to too such as PassToErrorHandler.

And in the GetValidatedPayments method,

using this line

Dim cph As System.Web.UI.WebControls.ContentPlaceHolder = Master.FindControl("MainContent")

Open in new window

causes the same error on Master.

Any and all help very much appreciated.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The issue is that you are attempting to access an *instance* member/method from an otherwise *static* (or shared in the case of VB) member/method.

The way to combat this is to reference an instance of the class that contains the member/method; e.g.:

C# -
using System;

namespace EE_Q28914590
	class Program
		static void Main(string[] args)
			Foo foo = new Foo() { Bar = "This is a test" };

	class Foo
		public string Bar { private get; set; }

		public static string GetBar(Foo foo)
			return foo.Bar;

Open in new window

Module Module1
	Sub Main()
		Dim [foo] = New Foo() With {.Bar = "This is a test"}
	End Sub
End Module

Class Foo
	Private fBar As String

	Public WriteOnly Property Bar() As String
		Set(value As String)
			fBar = value
		End Set
	End Property

	Public Shared Function GetBar([foo] As Foo) As String
		Return foo.fBar
	End Function
End Class

Open in new window

Both of these produce the following output -Capture.JPGSo what do you need to do?  You need to provide an instance that you will be accessing (either by way of parameter as I demonstrated, or by way of an instance field/property).


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SimonPrice3376Author Commented:
Thanks for the quick response, I will try this out tomorrow when I am back in the office.
SimonPrice3376Author Commented:
I had to adapt this slightly but got what I needed from this thank you very much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.