This documentation is archived and is not being maintained.

How to: Write a Package Scoped Class 

Visual Studio 2005

You can group related classes within an application using the package statement. Classes with the same package name are called package-scoped. Once scoped, the class members in the same package are available to each other. Additionally, callers of the class access the entire scope with a simple import statement.

When working with packages, consider the following:

  • Consider naming a package with several words. By convention, the first word indicates the name of your organization. For example: package mycompany;

  • To nest a package, add a dot separator and the child name. For example:

  • By design, package scoping occurs across assemblies (.exe and .dll files). If you want to block access to your assembly, use the /securescopingcompiler switch.

The following examples show you how to package scope classes. When you create a new Visual J# project, the project name automatically becomes the package name for every class within the project. For existing classes, you will manually have to add or change the package statement.

To automatically group new applications to a package

  1. Create a console application and name the project MyPackage.

  2. Review the Program.jsl.

    Notice that the package statement matches the project name. For example package MyPackage;

  3. From the Solution Explorer, right click on the project name and Add New Item.

  4. Click Class and type AnotherClass as the Name.

  5. Click Add.

  6. Review the AnotherClass.jsl.

    Notice that Visual J# automatically adds the package statement in the jsl. File. Each class that you add contains the same package name. For example package MyPackage;

To group existing classes into a package

  1. Open each .jsl file and add the same package statement followed by a semicolon.

    The package statement must be the first line in the source file that is neither commented nor blank. For example: package mycompany.platform;

  2. Press F5 to compile and run the application.

    When it compiles successfully, the compiler groups the classes into a namespace and sets up a corresponding directory structure.

See Also