2013-05-15 28 views
1

在Javascript中的每個對象攜帶this,其指的是本身。如何創建類中的字段來引用包含它的對象?對象如何引用自身?

- 編 -

澄清,我的意思是,如果我宣佈:

Class xc 
    Private i As Integer 
End Class 

然後進行參考:

Dim x As New xc() 
x.Me 

我得到的錯誤:

'Me' is not a member of 'MyProject.xc'. - \x...\test.vb(3) - Source Line: x.Me

順便提一下,問題出現在以下,相關問題:How to refer to an object created by "with" within the construct?

回答

0

哈哈。這似乎工作。

Public Class XC 
    Public Self As XC = Me 
End Class 

Dim x As New XC() 
Dim y As XC = x.Self 
4

VB.NET有Me關鍵字。

Me.Name = "Name" 
+0

這不就是「我」是指形成的? – matzone

+0

@matzone表格是一個類 – Steve

+0

如果代碼位於表單上,是的。 :-) –

0

您不需要引用類的實例,因爲您的實例是引用。 所以你的代碼x.Me將只是x

我(VB.NET)或本(C#)只內這種情況下

From MSDN的參考類的實例:

A class is a reference type. When an object of the class is created, the variable to which the object is assigned holds only a reference to that memory.

但是,如果你真的想擁有一流的memeber ,那麼就創建類型的類的memeber併爲它分配是這樣的:

Public MyPreference as YourClass 

,然後分配給它

Me.MyReference = Me