我不确定这是否可行,但我想将类名引用与共享成员方法/属性/变量相关联。考虑:
Public Class UserParameters
Public Shared Reference As Object
Public Shared Function GetReference() As Object
Return Reference
End Function
End Class
在程序的另一部分中,我想简单地调用UserParameters并让它通过别名GetReference或变量直接返回Reference。
我正在尝试模拟Application,Request或Session变量: Session(0)= Session.Item(0)
任何建议都将不胜感激。
答案 0 :(得分:1)
您不能直接从静态方法返回实例成员(静态方法无法访问实例成员,因为它没有与类的其余部分实例化,只存在一个静态方法的副本)。 / p>
如果您需要以可以从静态方法返回实例的方式设置类,则需要执行类似以下操作:
Public Class SampleClass
Private Sub New()
'Do something here
End Sub
Public Shared Function GetSample() As SampleClass
Dim SampleClass As SampleClass
SampleClass = New SampleClass
SampleClass.Sample = "Test"
Return SampleClass
End Function
Private _SampleString As String
Public Property Sample As String
Get
Return _SampleString
End Get
Private Set(ByVal value As String)
_SampleString = value
End Set
End Property
End Class
Public Class SampleClass2
Public Sub New()
'Here you can access the sample class in the manner you expect
Dim Sample As SampleClass = SampleClass.GetSample
'This would output "Test"
Debug.Fail(Sample.Sample)
End Sub
End Class
此方法用于CLR中的各个位置。比如System.Net.WebRequest class。在使用中以这种方式实例化的地方:
' Create a request for the URL.
Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")