.NET Constants Class Library

  • Thread starter Thread starter Alan Sloan
  • Start date Start date
A

Alan Sloan

I need to create a .net class library (dll) that contains nothing but a
bunch of constants. It doesn't matter if it's VB.NET or C#, but it must be
exposed to COM so that a legacy VB6 project can reference it and use the
constants contained within the dll.

Let's see if I can explain this clearly. I have created a C# dll which
contains several classes and within those classes are the constants. Such
as:

using System; namespace MyConstantLib {
public sealed class ItemConstants : CommonConstants
{
public static readonly Int32 CLASS_ITEM_BASE_CLASS = 901;
public static readonly Int32 CLASS_DOCUMENTS_CLASS = 9000;
public static readonly Int32 CLASS_DOCUMENT = 9141;
.....
}

The VB app can "See" and reference the DLL, and It will Intellisense all the
way to the class in the dll. For example, I can do this in VB

Dim dllRef as MyConstLib.ItemConstants

But I cannot get to the individual constants in the "object" (i.e.
CLASS_ITEM_BASE_CLASS const). What am I missing?

Even better yet would be that once my dll is referenced in VB these
constants would show up at the first level of the Intellisense list (right
there when you press ctrl+space) without needing to Dim an object to get to
them.

Any help would be greatly appreciated.

Thanks in advance,
Alan
 
Alan Sloan said:
I need to create a .net class library (dll) that contains nothing but a
bunch of constants. It doesn't matter if it's VB.NET or C#, but it must be
exposed to COM so that a legacy VB6 project can reference it and use the
constants contained within the dll.

Let's see if I can explain this clearly. I have created a C# dll which
contains several classes and within those classes are the constants. Such
as:

using System; namespace MyConstantLib {
public sealed class ItemConstants : CommonConstants
{
public static readonly Int32 CLASS_ITEM_BASE_CLASS = 901;
public static readonly Int32 CLASS_DOCUMENTS_CLASS = 9000;
public static readonly Int32 CLASS_DOCUMENT = 9141;
.....
}

The VB app can "See" and reference the DLL, and It will Intellisense all
the way to the class in the dll. For example, I can do this in VB

Dim dllRef as MyConstLib.ItemConstants

But I cannot get to the individual constants in the "object" (i.e.
CLASS_ITEM_BASE_CLASS const). What am I missing?

Even better yet would be that once my dll is referenced in VB these
constants would show up at the first level of the Intellisense list (right
there when you press ctrl+space) without needing to Dim an object to get
to them.

Any help would be greatly appreciated.

Thanks in advance,
Alan

If you make sure that your VB project, not only references the C# .dll, but
also includes:

Imports MyConstLib.ItemConstants

then it will work (it did for me). You will be able to access your
constants with no qualification as you desire.

-Scott
 
The VB app can "See" and reference the DLL, and It will Intellisense all
the way to the class in the dll. For example, I can do this in VB

Dim dllRef as MyConstLib.ItemConstants

But I cannot get to the individual constants in the "object" (i.e.
CLASS_ITEM_BASE_CLASS const). What am I missing?

Even better yet would be that once my dll is referenced in VB these
constants would show up at the first level of the Intellisense list (right
there when you press ctrl+space) without needing to Dim an object to get
to them.

Hi,

Not sure why you inherit from a CommonConstants class but I would just use
Enums. It should solve both problems and seems more natural for the usage
you seems to have..

If for some reason you want to keep using static, I would try to explicitely
define the class as being static (or perhaps is this just that is not
supported on the COM/VB6 side)...
 
Thanks Scott, however this dll will be referenced by a VB6 app, which has no
Import like VB.NET
 
Patrice, using enums worked great, except that some of the constants that I
need to expose to COM are strings and enums can't "do" strings. Any ideas
on how to accomplish the same result with strings?

Thanks,
Alan
 
Patrice, using enums worked great, except that some of the constants that
I need to expose to COM are strings and enums can't "do" strings. Any
ideas on how to accomplish the same result with strings?

Never tried, but my first try would be to expose them as constants (using
the "const" keyword)...
 
Back
Top