访问Class的属性的函数

时间:2015-01-30 10:32:27

标签: vb.net

我有一个类,我在其中添加一些文件夹路径并为其提供唯一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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

然后,将您的课程更改为以下内容:

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