views:

19

answers:

1

I get this error: 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.

Partial Class _Default
    Inherits System.Web.UI.Page

    <WebMethod()> _
    Public Shared Function ParseData() As String
        Dim value as string = GetValue()
    End Function

    Private Function GetValue() as String
        Return "halp"
    End Function
End Class

I know it has something to do with the fact that the first function is shared and the second function should probably be Public as well but I don't fully understand the reason behind it. Probably not relevant but I'm calling the web method from some javascript.

+1  A: 
rockinthesixstring
From what I've read WebMethods have to be shared. Is this my only option?
Radu
I've edited. Basically, yes this is your only option. The error is very clear. Because of the very nature of a "shareed" method, the methods being called from the shared method must also be shared.
rockinthesixstring
I've made another edit. In my edit you'll notice that I create an instance of the `Utilities` Class so that I can call the Public (but not Shared) `GetValues` method.
rockinthesixstring
Thanks, that takes care of the error. Is it possible to namespace it so that I don't have to repeat util for each call?
Radu
sorry, I don't follow your last question. "namespace it so that I don't have to repeat util for each call". You cannot initialize a namespace, only a class. that's why you use `util.GetValue`
rockinthesixstring
I'm just piecing together what I'm reading from various sites. Thank you for your help though!
Radu