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

SimonPrice3376
SimonPrice3376 used Ask the Experts™
on
Hi

I am trying to get an AJAX call working from asp.net 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

        Try
            PrepareAndPassData(id)

            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()
        listOfParameters.Add(validatedPaymentsParam)

        'Dim dt = DataAccessLayer.GetDataAsDatatable(listOfParameters, _connstr, "usp_GetValidatedRecordsForPayment")
        'WriteToExcel(dt)
        Try
            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.

Thanks

Simon
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
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" };
			Console.WriteLine(Foo.GetBar(foo));
			Console.ReadLine();
		}
	}

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

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

Open in new window


VB.NET -
Module Module1
	Sub Main()
		Dim [foo] = New Foo() With {.Bar = "This is a test"}
		Console.WriteLine(EE_Q28914590_VB.Foo.GetBar([foo]))
		Console.ReadLine()
	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).

-saige-

Author

Commented:
Thanks for the quick response, I will try this out tomorrow when I am back in the office.

Author

Commented:
I had to adapt this slightly but got what I needed from this thank you very much.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial