:
: _AnonCoward wrote:
: >
: > : > :
: > : I'm trying to figure out what the "Friend" keyword does. I know it
: > : specifies that "elements are accessible from within the same
: > : assembly", but that doesn't help because I don't know what an assembly
: > : is. I know what a project is, and I know what a solution is, but
: > : "assembly" isn't defined in the help file anywhere. Can anyone provide
: > : a simple definition?
: >
: >
: > This is extremely simplistic (and incomplete), but hopefully it will
: > give you a starting off point.
: >
: >
: > Think of an assembly as a block of code that contains one or more
: > classes or structures. Such assemblies can be part of a library (DLL) or
: > part of a stand alone executable (EXE). Typically, the classes and
: > structures will be logically related to one another, but they don't
: > absoulely have to be.
: >
: >
: > So let's say you decide to build a simple little "hello world"
: > executable that contains a single class:
: >
: >
: > File: HelloWorld.vb
: > ============================================================
: > Public Class HelloWorld
: > Public Shared Sub Main(Args() As String)
: > System.Console.Writeline("Hello World!")
: > End Sub
: > End Class
: > ============================================================
: >
: >
: > You can compile this file into and executable by running the following
: > command from a command prompt (be sure the VBC complier is in the path
: >
: >
: > C:\>vbc helloworld.vb
: >
: >
: > This will generate the file "helloworld.exe" which you can then run from
: > the command line. This executable is an assembly. Small and trival
: > granted, but an assembly nonetheless.
: >
: >
: > Let's change this somewhat by creating two new files:
: >
: >
: > File 1: HelloWorld1.vb
: > ============================================================
: > Public Class HelloWorld1
: > Public Shared Sub Main(Args() As String)
: > Dim Hw2 As New HelloWorld2
: > Hw2.Say("Hello World Too!")
: > End Sub
: > End Class
: > ============================================================
: >
: >
: > File 2: HelloWorld2.vb
: > ============================================================
: > Public Class HelloWorld2
: > Public Sub Say(Something As String)
: > Dim Hw3 As New HelloWorld3
: > Hw3.Repeat(Something)
: > End Sub
: > End Class
: >
: > Friend Class HelloWorld3
: > Public Sub Repeat(Something As String)
: > System.Console.WriteLine(Something)
: > End Sub
: > End Class
: > ============================================================
: >
: >
: > If you compile these two files, you will get two assemblies. Piece of
: > cake.
: >
: >
: > Now here is the tricky part...
: >
: >
: > Sub Main() in File1 can access the sub Say() in class HelloWorld2
: > (File2) because that class is Public.
: >
: >
: > However, Sub Main() in File1 cannot access sub Repeat() in class
: > HelloWorld3 (File2) because that class is a Friend Class. Classes
: > HelloWorld1 and HelloWorld3 are two separate assemblies, so class
: > HelloWorld3 cannot be called from class HelloWorld1.
: >
: >
: > On the otherhand, classes HelloWorld2 and HelloWorld3 are in the same
: > assembly, so they are accessible to one another. Therefore, sub Say() in
: > class HelloWorld2 can access sub Repeat() in class HelloWorld3.
: >
: >
: > I'm sure this is as clear as mud, but as I said this will hopefully get
: > you started.
:
: Thank you for the explanation. In your example, the two classes defined
: in the same .vb file belong to the same assembly, but the class defined
: in other .vb file belongs to a different assembly. Is that the
: definition of an assembly, then? Each assembly contains all the code
: defined within a single .vb file?
Not exactly. It is possible to compile two or vb files into the same
assembly. For example, I could type the following at the command line:
C:\>vbc /out:helloWorld.exe helloWorld1.vb helloWorld2.vb
This would produce a single assembly "helloWorld.exe" from the two .vb
files. In that case, all three classes (HelloWorld1, HelloWorld2 and
HelloWorld3) would be in the same assembly and so HelloWorld1 could call
HelloWorld3.Repeat().
The DLL or EXE that a given compile operation outputs (whether from within
the IDE or from the command line) is typically what makes up an assembly.
That compile may use a single .vb file or combine several.
Ralf