0

.NET 大师,

我接手了另一个开发人员的工作,并创建了一个Class Level Public Property boolean isNewValue

但是,我无法Property"Shared" Subsor Functionsthat are Publicor <WebMethods>that are访问此内容Public Shared Functions.

我不明白为什么,有人可以解释吗?

' Public Property scoped as Public in the Public Class Class_Name
Public Property isNewValueCode As Boolean
    Get
        Return _isNewValueCode
    End Get
    Set(ByVal value As Boolean)
        _isNeValuewSrvCode = value
    End Set
End Property

稍后在代码中我有:

Shared Function GetDataItem(ByRef db As myEntities) As CodeDataItem


    THIS IS WHERE I NEED TO BE ADD AN IF
    If isNewValueCode Then 'Cannot see the isNewValueCode

          Dim data As New CodeDataItem
          Dim code = db.tbl_services.FirstOrDefault(Function(x) x.id = TargetID)
          If (Not code Is Nothing) Then
                data = New CodeDataItem(code)
          End If
    Else
         'New code going against different db.tables in Entity Context
    End If


    Return data
End Function



enter code here
4

2 回答 2

3

如果没有该类的实例,您将无法从共享函数访问非共享类成员。因此,为了能够访问isNewValueCode您需要该属性所属的任何类的实例。
根据您的要求,您可以将属性更改为shared也,但它将不再是该类的任何实例的成员。

于 2013-09-20T14:19:01.743 回答
0

我想你有类似的东西:

Private _isNewValueCode As Boolean
Private _isNeValuewSrvCode As Boolean

Public Property isNewValueCode As Boolean
    Get
        Return _isNewValueCode
    End Get
    Set(ByVal value As Boolean)
        _isNeValuewSrvCode = value
    End Set
End Property

你有几个选项,你可以使 isNewValue 和它的私有构造函数共享,如下所示:

Private Shared _isNewValueCode As Boolean
Private Shared _isNeValuewSrvCode As Boolean

Public Shared Property isNewValueCode As Boolean
    Get
        Return _isNewValueCode
    End Get
    Set(ByVal value As Boolean)
        _isNeValuewSrvCode = value
    End Set
End Property

或者假设 isNeValuewSrvCode是复制粘贴错误并且您真的想将其分配回同一个变量,您可以简单地执行以下操作:

Public Shared Property isNewValueCode As Boolean

查看共享属性的 MSDN 描述,了解更多关于shared关键字的作用的信息。

于 2013-09-20T15:18:23.360 回答