Fann en lösning: Istället för att göra varje enskild variabel shared kan du ge din klass en defaultinstans:Cannot ... class from within a shared method or shared member ..
Behöver lite hjälp :) Vill kunna använda mig av propertys från min min class Data och kunna sätta propertys från aspxsidan, men när jag anropar min property får jag detta:
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.
Här är mina oopkunskaper begränsade :) Så här ser klassen Data ut:
Namespace
Public Class Data
Inherits Control ' <--- hmm?
Public Shared Function GetCategoryDS(Optional ByVal sortitem As String = "category") As DataSet
Dim mConnection As New clsConnection
Dim strSql As String = ""
If ListTopItem > -1 Then
strSql = "select top " & ListTypeItem & " * from category order by " & sortitem
Else
strSql = "select * from category order by " & sortitem
End If
Return mConnection.GetNewDataSet(strSql)
mConnection = Nothing
End Function
Public Overridable Property ListTypeItem() as integer
Get
If Not ViewState("ListNrItems") Is Nothing Then
Return CType(ViewState("ListNrItems"), integer)
Else
Return - 1
End If
End Get
Set(ByVal value As integer)
ViewState("ListNrItems") = value
End Set
End Property
end namespaceSv: Cannot ... class from within a shared method or shared member ..
Public Class Data
Inherits Control
Private Shared sharedInt As Integer ' <--
getDs function .....
Shared Property ListTypeItem() As Integer
Get
Return sharedInt '<----
End Get
Set(ByVal value As Integer)
sharedInt = value
End Set
End Property Sv:Cannot ... class from within a shared method or shared member ..
Public Class Foo
Private Shared myInstance As Foo ' Håller min instans
Private Shared mySyncLockObject As Object
Private myBar As Integer
Private myBaz As String
' Privat konstruktor
Private Sub New()
myBar = 42
myBaz = "Baz!"
End Sub
Public Shared Function GetDefaultInstance() As Foo
' Skapa en instans av klassen om det inte finns nån
If myInstance Is Nothing Then
SyncLock mySyncLockObject
If myInstance Is Nothing Then
myInstance = New Foo
End If
End SyncLock
End If
' Returnera instansen
Return myInstance
End Function
Public Property Bar() As Integer
Get
Return myBar
End Get
Set(ByVal value As Integer)
myBar = value
End Set
End Property
End Class
I det här fallet kommer du då åt Bar "utan" att instansiera objektet genom att skriva
Foo.GetDefaultInstance.Bar = 43