'Friend' vs. 'Protected Friend'

K

K. Shier

Friend variables "are accessible from within the program that contains their
declaration and from anywhere else in the same assembly." according to the
VS.Net help. This sounds like a pretty broad scope!

So why, when attempting to inherit a form from a base form class, is
'Friend' inadequate to give inherited forms 'public'-style access to
inherited controls? I.E. - i have a base form with two group boxes on it.
When they are declared 'Friend', i can't drag and drop controls into them in
inherited forms in the visual form designer. When they are declared
'Protected Friend', they work as expected.

Getting back to the definition of Friend - isn't any inherited form within
the same project going to be considered "...within the program... [or] ...in
the same assembly."? By now, my empirical evidence indicates that this not
the case, but i am wondering WHY?!

TIA! =)
 
T

Tom Spink

Sometimes declaring something as Friend is illegal as it exposes a public
object.

Example:

Friend Enum Foo
Alpha
Beta
Gamma
End Enum

Public Class Bar
Public Sub Baz(ByVal Moo As Foo)
'
End Sub
End Class

Baz has Moo declared as Foo for it's parameter. Foo is a friend type, and
Bar is a public class. It is invalid because A public type cannot expose a
friend type.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"


: Friend variables "are accessible from within the program that contains
their
: declaration and from anywhere else in the same assembly." according to the
: VS.Net help. This sounds like a pretty broad scope!
:
: So why, when attempting to inherit a form from a base form class, is
: 'Friend' inadequate to give inherited forms 'public'-style access to
: inherited controls? I.E. - i have a base form with two group boxes on it.
: When they are declared 'Friend', i can't drag and drop controls into them
in
: inherited forms in the visual form designer. When they are declared
: 'Protected Friend', they work as expected.
:
: Getting back to the definition of Friend - isn't any inherited form within
: the same project going to be considered "...within the program... [or]
....in
: the same assembly."? By now, my empirical evidence indicates that this
not
: the case, but i am wondering WHY?!
:
: TIA! =)
:
:
 
K

K. Shier

Tom Spink said:
Sometimes declaring something as Friend is illegal as it exposes a public
object.

Example:

Friend Enum Foo
Alpha
Beta
Gamma
End Enum

Public Class Bar
Public Sub Baz(ByVal Moo As Foo)
'
End Sub
End Class

Baz has Moo declared as Foo for it's parameter. Foo is a friend type, and
Bar is a public class. It is invalid because A public type cannot expose a
friend type.

hmm ok well that helps but according to the wording in the M$ help, i still
am not sure why the example i gave doesn't work.

it says 'Friend's can be accessed from "the program that contains their
declaration or from anywhere in the same assembly." there is only one
program in my project! and is not my compiled .exe considered one assembly?

my example:

Public Class DataEntryForm
Friend WithEvents grpDataEntry as System.Windows.Forms.GroupBox
....

now in this case, 'system.windows.forms.groupbox' is a public type, so why
does grpDataEntry appear locked on forms inherited from DataEntryForm?

as always, thanks & TIA! =)

--------------------
"Maybe it's a game called 'Punish the User'"

"Maybe", you say?! >=)
 
A

Alexandre Moura

If the class that derives from DataEntryForm is in the same assembly (same
exe or same dll) as DataEntryForm, any friend member on DataEntryForm
should be accessible by it - can you show us the code that tries to access,
and what is the error it gives?
 

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