2016-06-28 57 views
0

我有两个不同的类,想在Notify函数中使用类作为参数。 ====代码======================
'邮件类通知参数以类作为参数的通用方法

Friend Class MailClass 
     Friend NotifyHost As String 
     Friend NotifyPort As String  
End Class 

' FTP类通知参数

Friend Class FtpClass  
    Friend NotifyHost As String  
    Friend NotifyPort As String   
    End Class  


Friend Class ProcessNotification  
    'Notify on FTP Specified Email    
    Private Sub btnMailNotiFy_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs)  
    Notify (ByRef mailAccount as MailClass)  
    End Sub 

    'Notify on FTP Specified Email 
    Private Sub btnFTPNotiFy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Notify (ByRef ftpAccount as FtpClass) 
    End Sub 

    ' Generic method to use for multiple class 
    Private Sub Notify(NO Idea ??) 
    ' ## Please Help ### 
    End Sub 
End Class 

如何使通知方法泛型?

回答

0

如果使用内存,generics自2.0版以来一直是.Net的一部分。
你在找这样的事吗?

Private Sub Notify(Of T as Class)(ByVal agr as T) 
' do stuff 
End Sub 

更新

继我们在评论的讨论,你需要创建一个将指定所有你想你的泛型方法中使用的属性和方法的接口,然后用它作为通用的限制:你发这种方法

Friend Interface IMyInrteface 
    NotifyHost As String 
    NotifyPort As String 
End Interface 

Private Sub Notify(Of T as IMyInrteface)(ByRef agr as T) 
' do stuff 
End Sub 

当然,所有的类必须实现的接口:

Friend Class MailClass 
    Implements IMyInrteface 
    Friend NotifyHost As String 
    Friend NotifyPort As String  
End Class 

Friend Class FtpClass  
    Implements IMyInrteface 
    Friend NotifyHost As String  
    Friend NotifyPort As String   
End Class 
+0

我试过这个,但得到错误说'NotifyHost不是T的成员' – monikapatel

+0

请编辑你的问题,包括你的代码吧。另外,我看到Notify消息应该接受T ByRef而不是ByVal。 –

+0

但如果我使用通知(的T作为类)(ByRef帐户作为T),不能使用account.NotifyHost,得到错误说NotifyHost不是T.的成员我想用多个类的泛型方法作为参数如何我可以使用你的代码访问类属性? – monikapatel