访问 Class 的 属性 的函数

Function that accesses Class's property

我有一个 class,我在其中添加了一些文件夹路径并为它们指定了唯一 ID。 我可以使用 item.DeleteFolder(item.FolderID) 访问我的函数 我想要的是能够做到以下几点:

item.FolderID.DeleteFolder()

有什么方法可以做到这一点?

Imports System.Collections.Generic
' Simple business object. A FolderId is used to identify the type of Folder  
' but the Folder name can change.  
Public Class FoldersBackup
Implements IEquatable(Of FoldersBackup)

Private m_FolderPath As String
Public Property FolderPath() As String
    Get
        Return m_FolderPath
    End Get
    Set(value As String)
        m_FolderPath = value
    End Set
End Property

Private m_FolderId As Integer
Public Property FolderID() As Integer
    Get
        Return m_FolderId
    End Get
    Set(value As Integer)
        m_FolderId = value
    End Set
End Property

Public Function DeleteFolder(FolderPath As String) As Boolean
    If FolderPath Is Nothing Then
        Return False
    End If
    System.IO.Directory.Delete(FolderPath)
    Return True

End Function
End Class
Public Class FoldersBackup
    Implements IEquatable(Of FoldersBackup)

    Public Property FolderPath() As String
    Public Property FolderID() As Integer

    Public Function DeleteFolder() As Boolean
        System.IO.Directory.Delete(Me.FolderPath)
        Return True
    End Function
End Class

您必须将 m_FolderId 的类型更改为您创建的新 class。它必须是这样的:

Public Class FoldersId

Private m_FolderPath As String
Private m_FolderId As Integer

Public Sub New(Optional ByVal path As String = "",Optional ByVal Id as Integer = 0)
    m_FolderId = Id
    m_FolderPath = path
End Sub

Public Property Value() As Integer
    Get
        Return m_FolderId 
    End Get
    Set(value As Integer)
        m_FolderId = value
    End Set
End Property

Public Function DeleteFolder() As Boolean
    If m_FolderPath Is Nothing Then
        Return False
    End If
    System.IO.Directory.Delete(m_FolderPath)
    Return True

End Function

End Class

然后,将您的 class 更改为如下内容:

Imports System.Collections.Generic
' Simple business object. A FolderId is used to identify the type of Folder  
' but the Folder name can change.  
Public Class FoldersBackup
Implements IEquatable(Of FoldersBackup)

Public FolderID As New FoldersId()

Private m_FolderPath As String
Public Property FolderPath() As String
    Get
        Return m_FolderPath 
    End Get
    Set(value As String)
        m_FolderPath = value
        FolderID = New FoldersId(value,FolderID.Value)
    End Set
End Property

End Class

这样,您就可以像这样调用方法:

item.FolderID.DeleteFolder()

但是如果你想获取id值,你现在必须这样获取:

item.FolderID.Value