default access type of property?

H

Harold Hsu

Hi all,

What's the default access type of a property declared in an interface? The
one I'm looking at is IBindingList:

Public Interface IBindingList
....
ReadOnly Property AllowEdit As Boolean
....
End Public

I have a class that implements IBindingList, but AllowEdit is implemented as
Protected:

Protected ReadOnly Property AllowEdit() As Boolean Implements
System.ComponentModel.IBindingList
....
End Property

If there is a method that takes an object implementing IBindingList as an
argument, wouldn't accessing AllowEdit causes an error? Why doesn't the VB
compiler complains when AllowEdit is implemented as Protected (or even
Private)? The C# compiler does complain. What am I missing here?

Thanks in advance,
Harold
 
I

Imran Koradia

No access modifier can be specified for methods/properties in an interface.
If you access the property/method through the interface, you will always be
able to access the property/method even if the property/method is declared
private as long as you can Dim a variable of that interface (By casting the
object of the implementing class to this variable - this was the only way
one could access interface methods in VB6) . If you directly access the
property/method from the object that implements the interface, then the
access level is determined by the access modifier specified for the
property/method in the class.

here's what I mean:

interface myinterface
function myprop() as boolean
end interface

class class1
implements myinterface
protected function prop() as boolean implements myinterface.myprop
return true
end function
end class

sub test()
dim o1 as new class1
o = o1
' although the prop method is declared protected,
' you can still access it via the interface
messagebox.Show(o.myprop.ToString)
' however, you cannot directly access the protected
' method via the object. hence, the line below wont
' even compile.
' messagebox.show(o1.prop.ToString)
end sub

However, I'm not sure why would there be a difference between the C# and VB
compilers. Maybe thats the way MS intended it to be.

hope that helps..
Imran.
 
H

Harold Hsu

Thanks for the clarification Imran.

Harold

Imran Koradia said:
No access modifier can be specified for methods/properties in an interface.
If you access the property/method through the interface, you will always be
able to access the property/method even if the property/method is declared
private as long as you can Dim a variable of that interface (By casting the
object of the implementing class to this variable - this was the only way
one could access interface methods in VB6) . If you directly access the
property/method from the object that implements the interface, then the
access level is determined by the access modifier specified for the
property/method in the class.

here's what I mean:

interface myinterface
function myprop() as boolean
end interface

class class1
implements myinterface
protected function prop() as boolean implements myinterface.myprop
return true
end function
end class

sub test()
dim o1 as new class1
o = o1
' although the prop method is declared protected,
' you can still access it via the interface
messagebox.Show(o.myprop.ToString)
' however, you cannot directly access the protected
' method via the object. hence, the line below wont
' even compile.
' messagebox.show(o1.prop.ToString)
end sub

However, I'm not sure why would there be a difference between the C# and VB
compilers. Maybe thats the way MS intended it to be.

hope that helps..
Imran.

implemented
 
J

Jay B. Harlow [MVP - Outlook]

Imran & Harold,
However, I'm not sure why would there be a difference between the C# and
VB
compilers. Maybe thats the way MS intended it to be.
The C# (& Java & C++) compiler implicitly associate a method in the class
with a method in the Interface by matching on name & signature.

The VB compiler explicitly associates a method in the class with a method in
the Interface via the Implements keyword.

This allows the name & scope to be anything you want in VB.NET, while in C#
(& Java & C++) it has to be public & the same name.

C# does allow you to use "Explicit interface member implementation" where
you don't give a scope and name the method "theInterface.theMethod" in the
implementing class, VB.NET accomplishes the same thing via the Implements
keyword (by allowing you to name this method anything you want & changing
the scope).

I prefer the VB.NET way of implementing interfaces over the C# method, as
its more flexible!

Hope this helps
Jay
 
H

Harold Hsu

Ahh..I see...thanks Jay!

Harold

Jay B. Harlow said:
Imran & Harold,
The C# (& Java & C++) compiler implicitly associate a method in the class
with a method in the Interface by matching on name & signature.

The VB compiler explicitly associates a method in the class with a method in
the Interface via the Implements keyword.

This allows the name & scope to be anything you want in VB.NET, while in C#
(& Java & C++) it has to be public & the same name.

C# does allow you to use "Explicit interface member implementation" where
you don't give a scope and name the method "theInterface.theMethod" in the
implementing class, VB.NET accomplishes the same thing via the Implements
keyword (by allowing you to name this method anything you want & changing
the scope).

I prefer the VB.NET way of implementing interfaces over the C# method, as
its more flexible!

Hope this helps
Jay
 
I

Imran Koradia

Jay,

Thanks for the clarification. I have no experience at all with C# (or rather
haven't really bothered to look in that direction :)). That definitely
explains the OP's situation.

Imran.
 

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