Problem in declaring a constant of user-defined (class) type

A

Antony

When I declared a constant in a class like:

public Const attr As ClassB = nothing

(ClassB is the user-defined Class)

I get the compiler error: Constants must be an intrinsic or enumerated type,
not a class, structure, or array type.

So how can I declare a constant value of type ClassB in this case? Thanks!
 
F

Fergus Cooney

Hi Antony,

You can use a ReadOnly Field.

Public Shared ReadOnly oConstClassB As New ClassB
Public Shared ReadOnly oNullClassB As ClassB = Nothing

What do you want it for ?

Regards,
Fergus
 
P

Parker Zhang [MSFT]

Hi Antony,

Since constants can only be defined for primitive types in .NET CLR, there
is no way to declare a constant value of a type which it isn't a primitive
type.

Primitive types include Boolean, Char, Byte, SByte, Decimal, Int16, Uint16,
Int32, UINT32, Int64, Single, Double, String

However, as metioned by Fergus, you can declare a readonly class instance.

Would you please let me know why you need a constant object?

If you have any Qs, please reply to this post.
 

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