在VB.NET中附加自定义对象

时间:2015-10-14 14:33:05

标签: vb.net class object

我很好奇是否有可能"追加"自定义类对象?我创建了一个旨在经常更新的对象,为了便于阅读,最好这样做:

Dim nwObject As New CustomObject
nwObject += anotherCustomObject
nwObject += yetAnotherCustomObject
'' ... So on and so forth

我已经开始覆盖ToString()功能,那么是否有允许此类功能的类似过程?

修改

忘了提及anotherCustomObjectyetAnotherCustomObject已定义"孩子" nwObject类的。{目前已经有一系列重载函数允许"追加" nwObject在处理良好的函数中有多个定义良好的对象。这些功能类似于以下内容:

Public Overloads Sub AppendObject(ByVal aObject As AnotherObject)
  '' Use the correct properties to append the base class properties
End Sub
Public Overloads Sub AppendObject(ByVal yaObject As YetAnotherObject)
  '' Use the correct properties to append the base class properties
End Sub

(这是我的头脑,所以请原谅基本的错误。你应该得到图片;)

1 个答案:

答案 0 :(得分:1)

我在this MSDN页面上找到了我要找的内容。结果我需要覆盖“+”运算符函数,就像我使用ToString()函数一样。这是我使用的代码(使用问题中的示例):

Public Overloads Shared Operator +(ByVal nwObject As CustomObject,ByVal aObject As AnotherObject) As CustomObject
  nwObject.MainProperty = nwObject.MainProperty.Replace("Some String",aObject.SpecificProperty)
  Return nwObject
End Operator
Public Overloads Shared Operator +(ByVal nwObject As Custom Object, ByVal yaObject As YetAnotherObject) As CustomObject
  nwObject.MainProperty = nwObject.MaintProperty.Replace("Some String",yaObject.AnotherSpecificProperty)
  Return nwObject
End Operator