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

  • Thread starter Thread starter prakash
  • Start date Start date
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
 
-----------------------------------------------------------
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
 
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
 
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.
 
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
 
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();
}
}
 
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
 
Back
Top