Question on access modifier of interface members in VB.NET And C#

P

prakash

Dear Friends

The following Code Works Well....
------------------------------------------------------------
Public Interface IHello
Sub SayHello()
End Interface

Public Class Hello
Implements IHello

Private Sub SayHello() Implements IHello.SayHello
MessageBox.Show("Say Hello On the Hello Class")
End Sub
End Class

Dim HelloObject As New Hello
'HelloObject.SayHello() -> Not Accessible

Dim IHelloObject As IHello
IHelloObject = CType(HelloObject, IHello)
IHelloObject.SayHello()
-------------------------------------------------------
This code would not compile...

public interface IHello
{
void SayHello();
}

public class Hello : IHello
{
private void SayHello()
{
MessageBox.Show("Say Hello On the Hello Class");
}
}

-----------------------------------------------------------
VB.NET allows the modifers friend,public,private for interface
implementars
C# allows only public...

Why this inconsistency ??

Regards
Prakash
 
M

Mattias Sjögren

-----------------------------------------------------------
VB.NET allows the modifers friend,public,private for interface
implementars
C# allows only public...

Why this inconsistency ??


Different languages have different features and syntax.

To achieve private (explicit) implementation in C# you write

void IHello.SayHello()
{
}



Mattias
 
P

prakash

Hi Mattias

That works !!!!!!!!!!!!!!!!!!!

what is term (explicit ) implementation meant by??

What is the difference b/w this two ??

public void SayHello()

void IHello.SayHello()

regards
prakash
 
J

Jon Skeet [C# MVP]

prakash said:
That works !!!!!!!!!!!!!!!!!!!

what is term (explicit ) implementation meant by??

What is the difference b/w this two ??

public void SayHello()

void IHello.SayHello()

Look up "explicit interface implementation" in the MSDN index, and
you'll find "Explicit Interface Implementation Tutorial" - that takes
you through it, and has links to the relevant sections of the C# spec.
 
P

prakash

Thanks Jon Skeet...

I learn the explict and implicit interface declarations. They are
interesting topics...

I think default implementation of VB.NET is explicit interface
implementation.

How can we implement an interface member using "implicit interface
implementation" in VB.NET??

Ragards
prakash
 
J

Jon Skeet [C# MVP]

prakash said:
I think default implementation of VB.NET is explicit interface
implementation.

Not really - it depends on what access modifier you provide.
How can we implement an interface member using "implicit interface
implementation" in VB.NET??

Look at this:

Imports System

Public Class ShortTest
Implements IDisposable

Public Overridable Sub Dispose Implements IDisposable.Dispose
Console.WriteLine ("Hello")
End Sub

Shared Sub Main
Dim x as New ShortTest()
x.Dispose()
End Sub

End Class

As far as I can tell, that's equivalent to:

using System;

public class ShortTest : IDisposable
{
public void Dispose()
{
Console.WriteLine ("Hello");
}

static void Main()
{
new ShortTest().Dispose();
}
}
 
P

prakash

Thank u very much Jon Skeet

Two interfaces with same member I am used the following syntax.
-------------------------------------------------------------------------------------
Public Interface IHello
Sub SayHello()
End Interface

Public Interface IHello1
Sub SayHello()
End Interface

Public Class Hello
Implements IHello, IHello1

Public Sub SayHello() Implements IHello.SayHello, IHello1.SayHello
MessageBox.Show("Say Hello On the Hello Class")
End Sub
End Class
 

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