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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Antony

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

    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!




  • Fergus Cooney

    #2
    Re: Problem in declaring a constant of user-defined (class) type

    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


    Comment

    • Antony

      #3
      Re: Problem in declaring a constant of user-defined (class) type

      Yes, that's exactly what I wanted. Thanks a lot Fergus!

      "Fergus Cooney" <filter-1@tesco.net> wrote in message
      news:%23vu9YYyg DHA.1200@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > 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
      >[/color]


      Comment

      • Parker Zhang [MSFT]

        #4
        RE: Problem in declaring a constant of user-defined (class) type

        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.

        --
        Parker Zhang
        Microsoft Developer Support

        This posting is provided "AS IS" with no warranties, and confers no rights.

        Comment

        Working...