Vb.NET Interface implementation

G

Guest

Hi

i have a base class (marked 'MustInherit') with a public property called IsValid and a public event called Vali
i have a class (lets call it 'Client') that inherits this base class
so the Client class now has a public event called valid and a public property called IsVali

the client class implements an interface called IListEditorItem, this interface declares two things... (can you guess?

a public event called Vali
and a public property called IsVali

The client obviously has these already (from the base class), but vb.net still says it doesnt have them as the implements keyword must be used and mapped to the corresponding methods etc.

C# would pick this up automatically (as it maps by name for interfaces), how do i get round this issue in vb.net
do i have to write another property called IsValid2 which returns the base class property and implements the interface property
or is there a way i can get the interface to know that the property is already there in the base class
 
A

Armin Zingler

Guy said:
Hi,

i have a base class (marked 'MustInherit') with a public property
called IsValid and a public event called Valid i have a class (lets
call it 'Client') that inherits this base class. so the Client class
now has a public event called valid and a public property called
IsValid

the client class implements an interface called IListEditorItem, this
interface declares two things... (can you guess?)

a public event called Valid
and a public property called IsValid

The client obviously has these already (from the base class), but
vb.net still says it doesnt have them as the implements keyword must
be used and mapped to the corresponding methods etc..

No, unless the base class implements the interface, but it doesn't.
C# would pick this up automatically (as it maps by name for
interfaces), how do i get round this issue in vb.net? do i have to
write another property called IsValid2 which returns the base class
property and implements the interface property? or is there a way i
can get the interface to know that the property is already there in
the base class?

In VB.NET you are free in naming the members implementing the Interface
members. You must use the Implements keyword to "connect" the class member
to the interface member. Consequently it must be placed in the derived
class, and the derived class must call the base members. I hope I'm not
wrong...
 
J

Jay B. Harlow [MVP - Outlook]

Guy,
In addition to Armin's comments (which are correct Armin).
C# would pick this up automatically (as it maps by name for interfaces),
how do i get round this issue in vb.net?
As Armin stated, All members of an interface must be implemented (via
Implements keyword) in the current class.
do i have to write another property called IsValid2 which returns the base
class property and implements the interface property?
Yes, Normally what I do is something like:

Private Property IListEditorItem_IsValid() as Boolean Implements
IListEditorItem.IsValid
Get
Return MyBase.IsValid
End Get
Set(ByVal value As Boolean)
MyBase.IsValid = value
End Set
End Property


Unfortunately the event is going to give you problems! I'm really not sure
of any way to accurately implement it...

Have you considered implementing the interface in the base class instead of
the derived class?

Hope this helps
Jay

Guy said:
Hi,

i have a base class (marked 'MustInherit') with a public property called
IsValid and a public event called Valid
i have a class (lets call it 'Client') that inherits this base class.
so the Client class now has a public event called valid and a public property called IsValid

the client class implements an interface called IListEditorItem, this
interface declares two things... (can you guess?)
a public event called Valid
and a public property called IsValid

The client obviously has these already (from the base class), but vb.net
still says it doesnt have them as the implements keyword must be used and
mapped to the corresponding methods etc..
C# would pick this up automatically (as it maps by name for interfaces),
how do i get round this issue in vb.net?
do i have to write another property called IsValid2 which returns the base
class property and implements the interface property?
or is there a way i can get the interface to know that the property is
already there in the base class?
 
P

Peter Huang

Hi,

Thanks for post in the community.
I am researching the issue, if I have any new information I will update
with you ASAP.

Have a nice day.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
P

Peter Huang

Hi,

Thanks for your quickly reply!

It seems that VB.NET will not do the same the thing as the C#.
For a workaround ,you may try the code below.
Module Module1
Public Interface IListEditorItem
Property IsValid() As Boolean
Event Valid()
End Interface
Public MustInherit Class BaseClass
Dim bVal As Boolean
Public Property IsValid() As Boolean
Get
Return bVal
End Get
Set(ByVal Value As Boolean)
bVal = Value
End Set
End Property
Public Event Valid()
Public Sub Fired()
RaiseEvent Valid()
End Sub
End Class
Public Class Client
Inherits BaseClass
Implements IListEditorItem
Public Shadows Property IsValid() As Boolean Implements
IListEditorItem.IsValid
Get
Return MyBase.IsValid
End Get
Set(ByVal Value As Boolean)
MyBase.IsValid = Value
End Set
End Property
Public Shadows Event Valid() Implements IListEditorItem.Valid
Public Shadows Sub Fired()
RaiseEvent Valid()
End Sub
End Class
Public Sub Test()
Console.WriteLine("Hello")
End Sub
Public Sub Main()
Dim o As New Client
AddHandler o.Valid, AddressOf Test
o.Fired()
End Sub
End Module

If you have any concern on this issue,please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top