none
About generic type in VB.Net RRS feed

  • Question

  • Public Class Class2 Public Sub ShowMsg(Of T)(ByVal cls As T)

    cls.ShowMsg() 'Error End Sub End Class Public Class Class3 Public Sub ShowMsg() MsgBox("In class 3") End Sub End Class Public Class Class4 Public Sub ShowMsg() MsgBox("In class 4") End Sub End Class

    Hi all,

    For the code above, I want to access ShowMsg member of Class3 and Class4 through Generic Type, but I get error.

    How can I do to reach this goal?

    Thanks

    Tuesday, June 7, 2016 10:03 AM

Answers

  • Hi,

    the generic type does not have a ShowMsg method so you cannot call it. Your Class3 and Class4 have it, but you could also use your generic Class with Object. And Object does not have that method.

    One possible solution for this could be to restrict the class type in your generic class. In your case, you could define an Interface which defines that a class must have a Method ShowMsg. Then Class3 and Class4 could implement that interface. And you could change your generic class to define that T must implement this interface.

    These and a lot more can be found (including examples) in the documentation at:
    https://msdn.microsoft.com/en-us/library/w256ka79.aspx

    With kind regards,

    Konrad

    Tuesday, June 7, 2016 10:14 AM
  • Hi sincos2007,

    I agree with @Konrad Neitzel. I'll help you complete your code based on the reply of @Konrad.

    Public Class Class2
    'And you could change your generic class to define that T must implement this interface. Public Sub ShowMsg(Of T As IShowMsg)(ByVal cls As T) cls.ShowMsg() End Sub End Class 'Then Class3 and Class4 could implement that interface. Public Class Class3 Implements IShowMsg Public Sub ShowMsg() Implements IShowMsg.ShowMsg MsgBox("In class 3") End Sub End Class Public Class Class4 Implements IShowMsg Public Sub ShowMsg() Implements IShowMsg.ShowMsg MsgBox("In class 4") End Sub End Class 'as Konrad said, you could define an Interface which defines that a class must have a Method ShowMsg. Public Interface IShowMsg Sub ShowMsg() End Interface

    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 8, 2016 1:55 AM
    Moderator

All replies

  • Hi,

    the generic type does not have a ShowMsg method so you cannot call it. Your Class3 and Class4 have it, but you could also use your generic Class with Object. And Object does not have that method.

    One possible solution for this could be to restrict the class type in your generic class. In your case, you could define an Interface which defines that a class must have a Method ShowMsg. Then Class3 and Class4 could implement that interface. And you could change your generic class to define that T must implement this interface.

    These and a lot more can be found (including examples) in the documentation at:
    https://msdn.microsoft.com/en-us/library/w256ka79.aspx

    With kind regards,

    Konrad

    Tuesday, June 7, 2016 10:14 AM
  • Hi sincos2007,

    I agree with @Konrad Neitzel. I'll help you complete your code based on the reply of @Konrad.

    Public Class Class2
    'And you could change your generic class to define that T must implement this interface. Public Sub ShowMsg(Of T As IShowMsg)(ByVal cls As T) cls.ShowMsg() End Sub End Class 'Then Class3 and Class4 could implement that interface. Public Class Class3 Implements IShowMsg Public Sub ShowMsg() Implements IShowMsg.ShowMsg MsgBox("In class 3") End Sub End Class Public Class Class4 Implements IShowMsg Public Sub ShowMsg() Implements IShowMsg.ShowMsg MsgBox("In class 4") End Sub End Class 'as Konrad said, you could define an Interface which defines that a class must have a Method ShowMsg. Public Interface IShowMsg Sub ShowMsg() End Interface

    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 8, 2016 1:55 AM
    Moderator