具有共享的VB.NET范围:子和函数以及Web方法

时间:2013-09-20 14:11:44

标签: vb.net scope

.NET Gurus,

我已经选择了其他开发人员的工作,并创建了Class Level Public Property boolean isNewValue

但是,我无法从Property"Shared" Subs FunctionsPublic <WebMethods> 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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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 Description of shared properties,详细了解shared关键字的确切内容。