By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,232 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

default access type of property?

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

"Harold Hsu" <ha*****@ingenium-tech.com> wrote in message
news:O7**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #2

P: n/a
Thanks for the clarification Imran.

Harold

"Imran Koradia" <no****@microsoft.com> wrote in message
news:Od**************@TK2MSFTNGP09.phx.gbl...
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.

"Harold Hsu" <ha*****@ingenium-tech.com> wrote in message
news:O7**************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #3

P: n/a
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

"Imran Koradia" <no****@microsoft.com> wrote in message
news:Od**************@TK2MSFTNGP09.phx.gbl... 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.

"Harold Hsu" <ha*****@ingenium-tech.com> wrote in message
news:O7**************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #4

P: n/a
Ahh..I see...thanks Jay!

Harold

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #5

P: n/a
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.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
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

"Imran Koradia" <no****@microsoft.com> wrote in message
news:Od**************@TK2MSFTNGP09.phx.gbl...
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.

"Harold Hsu" <ha*****@ingenium-tech.com> wrote in message
news:O7**************@TK2MSFTNGP12.phx.gbl...
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



Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.