2014-02-20 10 views
0

重要信息的人有类似的查询与性能的嵌套类 - 如何使用第一窝的性能在`Set`值第二

答案很简单,你在看这个不能 - 从Plutonix答案如下解释花费1/2的一天谷歌搜索这之后为什么因此它被标记为答案

原始的问题

交白卷!

我有下面的类:

Public Class TestClass 

    Dim _nestedProperty As New NestedProperty() 

    Private Shared Item As String 

    Public Property Nest1a() As NestedProperty 
     Get 
      Return _nestedProperty 
     End Get 
     Set(Value As NestedProperty) 

     End Set 
    End Property 
    Public Property Nest1b() As NestedProperty 
     Get 
      Return _nestedProperty 
     End Get 
     Set(Value As NestedProperty) 

     End Set 
    End Property 

    Public Class NestedClass 

     Property Nest2a As String 
      Get 
       Return sub1() 
      End Get 
      Set(value As String) 
       'the first value in sub 2 needs to be the name of the Nest1 property 
       sub2([how do i get this value],value) 
      End Set 
     End Property 
     Property Nest2b As String 
      Get 
       Return sub1() 
      End Get 
      Set(value As String) 
       'the first value in sub 2 needs to be the name of the Nest1 property 
       sub2([how do i get this value],value) 
      End Set 
     End Property 

    End Class 

    Shared Function sub1() 

    End Function 

    Shared Sub sub2(Nest1 As String, Value As String) 

    End Sub 
End Class 

在现实中有第一窝许多属性,我想知道哪一个是“呼”第二套件。

即如果我使用TestClass.Nest1a.Nest2a = "abc",当从Nest2aSet我需要"Nest1a"代替[how do i get this value]传递主叫Sub2

我尝试在第一个巢的Set中设置'Item'对象,但它不起作用!

从主要形式,我对下面的代码调用这个类:

Dim TC As New TestClass 
TC.Nest1.Nest2a = "xyz" 
'lots more code 
TC.Nest1.Nest2b = "abc" 

我不想每次使用它的时候创建的TestClass一个新的实例!

任何人都可以提出解决方案吗?

+0

我不太明白这个问题。根据上面的示例代码(第二代码块),您希望取代'[我如何获取此值]'? – har07

+0

@ har07 - 查看更新的问题。 – OSKM

+0

'[我怎么得到这个值]'有几次。对于TestClass.Sub2,它只是'TestClass.Sub2',但作为一个子没有'get'的值。有关实际属性,请参见下文。 'TC.Nest1.Nest2a =“xyz”'是无效的,因为它们不是嵌套的道具 - Nest2a属于不同的类。 – Plutonix

回答

1

嵌套类,而不是属性。嵌套类需要有父类,通常在构造

Class Parent 
    Private myChild As Foo 

    Public | Private | Friend Class Foo 
     private myPArent As Parent 

     Public Sub New(p as Parent) 
      myParent = p 
     End Sub 

     ' now the nested class has a reference to the parent 
     Public Function xyz As Integer 
      Return myParent.SomeFunction * 2 
     End Function 
     ... 
    End Class 

    ' parent using child props: 
    Public Function GetFoo As integer 
     return myChild.Somthing 
    End Function 
    ... 
End Class 

过去了,你不需要一个裁判的SHARED潜艇的参考:

Parent.Sub2 

但是,什么可以做在他们的是略有限制(没有参考或使用实例数据)。取决于道具的实际性质,这种继承有时是另一种选择。您需要在别处创建Parent.Child类实例的可能性越大,您应该考虑继承的越多。

接口可能是另一种选择。

编辑

嵌套类通常是非常私人和用作父“助手”类。但是,如果他们是公开的,他们可以创建:

Dim p As New ParentFoo 
Dim c As New ParentFoo.ChildBar 

作为私人助手类,家长可以简单地使用孩子提供结果:

Dim xyz As Integer = p.BarCount 

父类中:

' the parent class may know nothing about Bars 
' and uses a helper to manage them: 
Public Function BarCount As Integer 
    return myChild.BarCount 
End Function 

IF父类公开孩子的参考,你可以做同样的事情你想要什么:

Public Property ChildFoo As Foo 
    Get 
    Return myChildFoo 
    End Get 

现在你可以做p.ChildFoo.BarCount。但是,这只是回答了问题if the nested class is to be consumed externally, why is it nested?外部代码可以将myChild设置为空,直接设置道具并可能会破坏父类!

+0

感谢您的确 - 有助于了解我出错的地方 - 那么没有办法使用'p.childbar.BarCount'? – OSKM

+0

如果孩子的引用被公开为一个属性,你可以这样做 - 参见编辑 – Plutonix

+0

@OSKM另一点:对于直接引用嵌套类的外部代码意味着它必须知道由父代提供哪些服务以及由子代提供哪些服务(仁)。这使得代码混乱,更重要的是要重新考虑你要进入的方向。HTH – Plutonix