lordofduct
Well-known member
- Joined
- Jun 24, 2010
- Messages
- 71
- Programming Experience
- 3-5
So say I define some interface, and that interface has members that need to be implemented under some idea, and I then implement this interface on a class that already has those members defined. How do I NOT receive errors about having to implement said members despite them already being implemented (because I didn't type the oh so ridiculous 'implements IMyInterface.foo').
For example say I have an interface that defines the event KeyPress, and then I have a custom Form that implements this interface of mine. It throws an error.
VB is the 5th language I've worked in that uses interfaces... and up until now they've all treated interfaces relatively the same. This is the first time I've seen this not allowed. What perplexes me more, is it IS allowed in other .Net languages. Just not VB.
Code example of what I'm talking about:
works fine in C#
fails in VB:
For example say I have an interface that defines the event KeyPress, and then I have a custom Form that implements this interface of mine. It throws an error.
VB is the 5th language I've worked in that uses interfaces... and up until now they've all treated interfaces relatively the same. This is the first time I've seen this not allowed. What perplexes me more, is it IS allowed in other .Net languages. Just not VB.
Code example of what I'm talking about:
VB.NET:
using System;
using System.Windows.Forms;
namespace DylanTest1
{
public partial class Form1 : Form, ITest
{
private ITest _obj;
public Form1()
{
InitializeComponent();
_obj = this;
_obj.KeyPress += this.foo;
}
private void foo(Object sender, EventArgs e)
{
Console.WriteLine(_obj.Name);
}
}
interface ITest
{
event KeyPressEventHandler KeyPress;
string Name { get; set; }
}
}
works fine in C#
VB.NET:
Imports System
Imports System.Windows.Forms
Public Class Form1 : Inherits Form : Implements ITest
Private _obj As ITest
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_obj = Me
AddHandler _obj.KeyPress, AddressOf Me.Foo
End Sub
Private Sub Foo(ByVal sender As Object, ByVal e As KeyPressEventArgs)
Console.WriteLine(_obj.Name)
End Sub
End Class
Public Interface ITest
Event KeyPress As KeyPressEventHandler
Property Name() As String
End Interface
fails in VB:
Error 1 Class 'Form1' must implement 'Event KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)' for interface 'ITest'. I:\DylanDocs\Dylan\Documents\Visual Studio 2008\Projects\DylanVBTest1\DylanVBTest1\Form1.vb 4 49 DylanVBTest1
Error 2 Class 'Form1' must implement 'Property Name() As String' for interface 'ITest'. Implementing property must have matching 'ReadOnly' or 'WriteOnly' specifiers. I:\DylanDocs\Dylan\Documents\Visual Studio 2008\Projects\DylanVBTest1\DylanVBTest1\Form1.vb 4 49 DylanVBTest1