122
|
C# & VB.NET Conversion Pocket Reference
Whenever you compile a .NET project, the IDE automati-
cally generates one of these LIBIDs, so even if you do not
have this attribute in your project, your type library will have
a LIBID. However, unless you set this attribute explicitly,
every time you compile you run the risk of the compiler gen-
erating a new number. Having a new GUID every time you
recompile is not desirable. The GUID itself is generated using
an algorithm that takes into consideration the name of the
assembly and the names of all the public classes and inter-
faces, as well as the project’s version number. Project GUIDs
are not guaranteed to be unique. If two people in two differ-
ent machines generate the same types of projects with the
same names for each class and interface, and assign the same
version number, they will both end up with the same LIBID.
Adding this attribute means that you are taking full control
of when that GUID gets changed.
The
AssemblyDelaySign, AssemblyKeyFile, and
AssemblyKeyName attributes (added to C# projects) have to do
with digitally signing your assembly. Before adding an assem-
bly to the global assembly cache (which contains assemblies
that are shared among multiple projects), the assembly must
be digitally signed. These attributes influence how the assem-
bly is signed. Typically, these attributes should be added to
projects whose resulting DLL will be added to the GAC. If
you need to digitally sign an assembly, you must add these
attributes to your source code (in VB by hand). An alterna-
tive to adding these attributes to your code is to compile your
program with the command-line compiler and use the /delay-
sign, /keyfile, and /keycontainer switches.
Default/Root Namespace
A namespace is a prefix that is added to each class name in
order to make the name of the class unique. Many times,
companies use the company name plus the project name as a
namespace name.
IDE Differences
|
123
Both VB.NET and C# enable you to specify a default
namespace through a project setting. To get to this setting in
VB.NET, first locate the Solution Explorer window, right-
click on the project name, and choose Properties from the
popup menu. A dialog appears with project settings; under
Common Properties
General, you will see a field called
Root namespace. Follow the same steps in C#, but the field
in the Common Properties
General dialog is called Default
namespace.
In C#, having a default namespace means that any time you
ask the IDE to add a new class file to your project, the wiz-
ard creates a file that declares the default namespace, then
adds the new class definition within the namespace. For
example, if your default namespace is WidgetsUSA, when
you choose Project
Add Class from the menu, the wizard
creates a source file that looks like the following:
namespace WidgetsUSA
{
public class Class2
{
}
}
If you change the default namespace setting, then any new
files you generate will have the new namespace name; old
files retain the previous namespace name. You could also set
this property to blank, and then the wizard would not add a
namespace declaration.
In VB.NET, when you assign a root namespace to your
project, all classes in the project automatically become part
of the root namespace. The wizard does not add a
namespace definition to the source files; the setting takes
effect at compile time. If you add a namespace declaration to
the source explicitly, the compiler still appends the root
namespace to the name of each class. If you set the root
namespace to WidgetsUSA and write code that defines a
Banking namespace with a class called Account, as follows:
C#

Get C# & VB.NET Conversion Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.