views:

87

answers:

5

This may seem like an obvious answer, but I can't seem to find an answer. I have this code in VB.NET:

Public Interface ITestInterface
    WriteOnly Property Encryption() As Boolean
End Interface

And I also have this class and implementation in VB.NET:

Partial Public Class TestClass
    Implements ITestInterface

    Public WriteOnly Property EncryptionVB() As Boolean Implements ITestInterface.Encryption
        Set(ByVal value As Booleam)
             m_Encryption = value
        End Set
    End Property
End Class

I am trying to convert this over to C#. I have the C# Interface converted over just fine, like so:

public interface ITestInterface
{
    bool Encryption { set; }
}

The problem is, how to convert the implementation over. I have this:

public partial class TestClass
{
    public bool Encryption 
    {
         set { m_Encryption = value; }
    }
}

The problem with this is that in C#, it would seem you have to name the function the same as the interface function you are implementing. How can I call this method EncryptionVB instead of Encryption, but still implement the Encryption property?

A: 

This is not possible in C# - when implementing an interface, members must have the name defined in the interface.

Oded
+4  A: 

C# doesn't support interface member aliasing as does VB.NET.

The best match would be something like this:

public partial class TestClass : ITestInterface{
  bool ITestInterface.Encryption {
    set { m_Encryption = value; }
  }

  public bool EncryptionVB {
    set { ((ITestInterface)this).Encryption = value; }
  }
}
Jordão
I have not checked, but this **might** even be how VB implements it. As implemented interface methods are normally just virtual overrides.
Dykam
It is basically an implementation of Adaptive Pattern. :)
abhishek
@Dykam: Yeah, I suspect it is. But regardless whether or not it's how VB *implements* it, the result is identical, as I point out in my answer.
Dan Tao
VB.NET does it differently. It emits only one member, and it uses the `.override` CIL directive (just like C# with explicit implementations).
Jordão
+3  A: 

The closest way I can think of is to use explicit implementation:

public partial class TestClass : ITestInterface
{
    public bool EncryptionVB
    {
         ((ITestInterface)this).Encryption = value;
    }

    bool ITestInterface.Encryption { set; }
}

Now, on the surface this might seem like "not the same thing." But it really is. Consider the fact that in VB.NET, when you name a member that implements an interface member something different from what the interface defines, this "new name" only appears when you know the type at compile time.

So:

Dim x As New TestClass
x.EncryptionVB = True

But if x in the above code were typed as ITestInterface, that EncryptionVB property would not be visible. It would be accessible only as Encryption:

Dim y As ITestInterface = New TestClass
y.Encryption = True

This is, in fact, behaving exactly the same as explicit interface implementation in C#. Take a look at the equivalent code:

TestClass x = new TestClass();
x.EncryptionVB = true;

ITestInterface y = new TestClass();
y.Encryption = true;
Dan Tao
+2  A: 

Perform the change using protection level

public bool EncryptionVB {
    set { m_Encryption = value; }
}
bool ITestInterface.Encryption {
    set { EncryptionVB = value; }
}
Joel Etherton
A: 

VB.NET has lot of capabilities and few of them does not follow OOPS totally. One of such is this. C# does not allow to implement an interface using another name. I don't know why it is supported to VB.NET, may be because of backward compatibility.

abhishek