开发者

VB.net "'Me' is valid only within an instance method" inside PageMethods

开发者 https://www.devze.com 2023-04-13 10:00 出处:网络
I\'m getting \"Me\' is valid only within an instance method\" error when I convert function to a PageMethods.

I'm getting "Me' is valid only within an instance method" error when I convert function to a PageMethods.

Private Shared objDT As System.Data.DataTable
Private Shared objDR As System.Data.DataRow

<WebMethod()> Public Shared Function addstn(itemID As String) As String
  For Each Me.objDR In objDT.Rows
        开发者_高级运维    If objDR("ProductID") = ProductID Then
                If objDR("Options") = desc Then
                    objDR("Quantity") += 1
                    blnMatch = True
                    Exit For
                End If
            End If
  Next

End Function

If I remove Me, I get different error. I got this shopping cart script somewhere online and I'm not sure what to replace "Me.objDR" with something else.

Thank you in advance


The Me keyword provides a way to refer to the specific instance of a class or structure in which the code is currently executing.

In a shared context there is no instance. You can reference your variable directly or via ClassName.VariableName.

This could work(i'm not sure wherefrom the other variables are):

For Each objDR In objDT.Rows
        If ProductID.Equals(objDR("ProductID")) _
           AndAlso desc.Equals(objDR("Options"))Then
                Dim q = CInt(objDR("Quantity"))
                objDR("Quantity") = q + 1
                blnMatch = True
                Exit For
        End If
Next

http://msdn.microsoft.com/en-us/library/20fy88e0%28v=vs.80%29.aspx


Me cannot be used since its a shared method, shared methods are not accessed through an instance.

What is the other error you receive?


Public Shared Function addstn(...

The "Shared" means it's not an instance method. If you want to use "Me", you can't use "Shared". Use the name of the class/module instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号