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

  • Thread starter Thread starter Antony
  • Start date Start date
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!
 
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
 
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.
 
Back
Top