VB.Net如何将不同的对象传递给子程序

huangapple go评论61阅读模式
英文:

VB.Net How to pass pass different objects into sub

问题

如何设置这个子程序以便你可以传递不同的对象进去?

Protected Shared Sub exampleSub(Optional ByRef partDescription as DoorDescription = Nothing)
    For Each feat In partDescription.MirrorFeatures
        ...
    Next
End Sub
Public Class DoorDescription
    Public Property MirrorFeatures As New List(Of Feature)
End Class
Public Class WindowDescription
    Public Property MirrorFeatures As New List(Of Feature)
End Class
exampleSub(DoorDescription)
exampleSub(WindowDescription)

希望设置这个子程序以便它能够适用于DoorDescription和WindowDescription。

英文:

How do you setup this sub so that you can pass different objects into it?

Protected Shared Sub exampleSub(Optional ByRef partDescription as DoorDescription = Nothing)
    For Each feat In partDescription.MirrorFeatures
        ...
    Next
End Sub
Public Class DoorDescription
    Public Property MirrorFeatures As New List(Of Feature)
End Class
Public Class WindowDescription
    Public Property MirrorFeatures As New List(Of Feature)
End Class
exampleSub(DoorDescription)
exampleSub(WindowDescription)

Looking to setup this sub so that it will work with DoorDescription and WindowDescription.

答案1

得分: 3

将代码翻译为中文:

创建一个抽象类从中继承WindowDescription和DoorDescription

```vb.net
Public MustInherit Class EntrywayDescription
    Public Property MirrorFeatures As List(Of Feature)
End Class

同样,一个接口也可以工作

Public Interface IEntrywayDescription
    Property MirrorFeatures As IEnumerable(Of Feature)
End Interface

然后你的类可以继承(或在接口的情况下实现)基类或接口

' 抽象类
Public Class DoorDescription
    Inherits EntrywayDescription
End Class
' 接口
Public Class WindowDescription
    Implements IEntrywayDescription
End Class

你可以这样使用它。如果两个类之间有更多的共同功能,你可以将它添加到基类中,并在引用是基类类型时访问它

Public Sub 示例(partDescription As EntrywayDescription)
    For Each feat In partDescription.MirrorFeatures

    Next
End Sub

通常,如果两个类之间有共享的具体功能,你可能会希望使用基类继承方法。例如,

Public MustInherit Class EntrywayDescription
    Public Property MirrorFeatures As List(Of Feature)
    Public ReadOnly Property GetFeaturesString As String
        Get
            Return String.Join(", ", MirrorFeatures.Select(Function(f) f.ToString()))
        End Get
    End Property
End Class

否则,使用接口,但然后你需要在每个实现它的类中单独实现接口成员,例如

Public Class DoorDescription
    Implements IEntrywayDescription

    Private _mirrorFeatures As List(Of Feature)
    Public Property MirrorFeatures As IEnumerable(Of Feature) Implements IEntrywayDescription.MirrorFeatures
        Get
            Return _mirrorFeatures
        End Get
        Set(value As IEnumerable(Of Feature))
            _mirrorFeatures = value.ToList()
        End Set
    End Property
End Class

希望这对你有所帮助。

英文:

Make an abstract class from which both the WindowDescription and DoorDescription inherit

Public MustInherit Class EntrywayDescription
    Public Property MirrorFeatures As List(Of Feature)
End Class

Similarly, an Interface would work

Public Interface IEntrywayDescription
    Property MirrorFeatures As IEnumerable(Of Feature)
End Interface

Then your classes would Inherit (or Implement in the case of the Interface) the base class or Interface

' abstract class
Public Class DoorDescription
    Inherits EntrywayDescription
End Class
' interface
Public Class WindowDescription
    Implements IEntrywayDescription
End Class

You can use it like this. If you have more common functionality between both classes, you can add it to the base class and access when the reference is of the base class type

Public Sub example(partDescription As EntrywayDescription)
    For Each feat In partDescription.MirrorFeatures

    Next
End Sub

Typically you may want to use the base class inheritance method if you have shared concrete functionality between the two classes. For example,

Public MustInherit Class EntrywayDescription
    Public Property MirrorFeatures As List(Of Feature)
    Public ReadOnly Property GetFeaturesString As String
        Get
            Return String.Join(", ", MirrorFeatures.Select(Function(f) f.ToString()))
        End Get
    End Property
End Class

Use the Interface otherwise, but you will then need to implement the interface members individually in each class which implements it, for example

Public Class DoorDescription
    Implements IEntrywayDescription

    Private _mirrorFeatures As List(Of Feature)
    Public Property MirrorFeatures As IEnumerable(Of Feature) Implements IEntrywayDescription.MirrorFeatures
        Get
            Return _mirrorFeatures
        End Get
        Set(value As IEnumerable(Of Feature))
            _mirrorFeatures = value.ToList()
        End Set
    End Property
End Class

huangapple
  • 本文由 发表于 2023年6月8日 23:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定