2013-10-19 76 views
0

我堅持用TE隨訪。 創建類的一些屬性字段如下:類和屬性VB.NET

Public Class Room1 
    Private _AdultNumber As Byte 
    Property AdultNumber() As Byte 
     Get 
      Return _AdultNumber 
     End Get 
     Set(value As Byte) 
      _AdultNumber = value 
     End Set 
    End Property 

當我調用這個類和一些值傳遞給物業,我希望新類的構造函數,我創建空的屬性字段類的新實例,但是,我得到了同樣的類,其中包含我之前通過的屬性值。

這裏如何我呼籲pageLoad的類。

Dim r1 As New Room1 
r1.AdultNumber = CByte(ddlAdRoom1.SelectedItem.Value) 

然後我需要從其他類讀取這個值。

Dim r1 As New Room1 
Dim x As Byte = r1.AdultNumber 

我應該如何更改代碼,得到這樣的結果:
1.設置屬性值與空屬性值新的類的實例? 2.從任何其他類或頁面方法存儲的房間1類值?

+1

如果CR吃掉一個新的對象,你不會得到以前賦值給屬性,你必須使用同一個對象。 – Magnus

+0

是的,但是當我打電話類這樣的:通過r1.AdultNumber昏暗R1作爲房間1,並試圖獲得財產也說,使用可變R1分配前值。 –

+0

這是因爲那不是同一個對象。這僅僅是一個名稱相同(未分配) – Magnus

回答

0

我最後通過以下解決了這個:

Imports Microsoft.VisualBasic 
Imports System.ComponentModel 
Public Class Rooms 
    Public Class Room1 
     Public Sub New() 
      Room1.AdultNumber = Nothing 
      Room1.ChildNumber = Nothing 
      Room1.Ages = Nothing 
     End Sub 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property AdultNumber As Byte = 0 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property ChildNumber As Byte = 0 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property Ages As String() = Nothing 
    End Class 
    Public Class Room2 
     Public Sub New() 
      Room2.AdultNumber = Nothing 
      Room2.ChildNumber = Nothing 
      Room2.Ages = Nothing 
     End Sub 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property AdultNumber As Byte = 0 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property ChildNumber As Byte = 0 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property Ages As String() = Nothing 
    End Class 
    Public Class Room3 
     Public Sub New() 
      Room3.AdultNumber = Nothing 
      Room3.ChildNumber = Nothing 
      Room3.Ages = Nothing 
     End Sub 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property AdultNumber As Byte = 0 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property ChildNumber As Byte = 0 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property Ages As String() = Nothing 
    End Class 
    Public Class Room4 
     Public Sub New() 
      Room4.AdultNumber = Nothing 
      Room4.ChildNumber = Nothing 
      Room4.Ages = Nothing 
     End Sub 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property AdultNumber As Byte = 0 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property ChildNumber As Byte = 0 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property Ages As String() = Nothing 
    End Class 
    Public Class Room5 
     Public Sub New() 
      Room5.AdultNumber = Nothing 
      Room5.ChildNumber = Nothing 
      Room5.Ages = Nothing 
     End Sub 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property AdultNumber As Byte = 0 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property ChildNumber As Byte = 0 
     <Bindable(True), DefaultValue(False)> 
     Public Shared Property Ages As String() = Nothing 
    End Class 
End Class 
1

在主類中,聲明Room1類作爲Public(以外的任何程序):

Public class MainClass 

    ... 
    Public r1 As New Room1 
    ... 

    Private Sub SetMember() 
     ... 
     Me.r1.AdultNumber = CByte(ddlAdRoom1.SelectedItem.Value) 
     ... 
    End Sub 

End Class 

在您的輔助類,你可以檢索先前設置的成員如下:

Public Class Class2 

    ... 
    Private x As Byte = MainClass.r1.AdultNumber 
    ... 

    Private Sub GetMember() 
     ... 
     Dim x As Byte = MainClass.r1.AdultNumber 
     ... 
    End Sub 

End Class 
+1

至少使用公共'property',不要暴露域變量。例如,像這樣:'作爲Room1的公共財產R1 = New Room1()' – Styxxy