Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Delphi
  4. [SOLVED] Access a const from a class

[SOLVED] Access a const from a class

Scheduled Pinned Locked Moved Delphi
questionhelp
5 Posts 3 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    hello guys... I have a type TClassFoo in which I have a const like this

    unit ClassFoo;
    interface
    uses ClassBase;

    type TClassFoo = class
    public const NUMBER = 100;
    end;

    I have two types TClassBase and TClassChild. TClassFoo and TClassBase have references of each other. I want to access the constant from TClassFoo in TClassChild like this..

    //*************************** Base Class ******************************//
    unit ClassBase;

    interface

    type TClassBase = class
    end;

    implmentation

    uses ClassFoo // had to put it here..otherwise it raises 'circular reference error'.
    var objFoo : TClassFoo;

    //*************************** Child Class ******************************//
    unit ClassChild;

    interface

    type TClassChild = class(TClassBase)
    // here I want to access TClassFoo.NUMBER
    end;

    So how can I do that ? Thanks for any input.

    This world is going to explode due to international politics, SOON.

    C S 2 Replies Last reply
    0
    • A AmbiguousName

      hello guys... I have a type TClassFoo in which I have a const like this

      unit ClassFoo;
      interface
      uses ClassBase;

      type TClassFoo = class
      public const NUMBER = 100;
      end;

      I have two types TClassBase and TClassChild. TClassFoo and TClassBase have references of each other. I want to access the constant from TClassFoo in TClassChild like this..

      //*************************** Base Class ******************************//
      unit ClassBase;

      interface

      type TClassBase = class
      end;

      implmentation

      uses ClassFoo // had to put it here..otherwise it raises 'circular reference error'.
      var objFoo : TClassFoo;

      //*************************** Child Class ******************************//
      unit ClassChild;

      interface

      type TClassChild = class(TClassBase)
      // here I want to access TClassFoo.NUMBER
      end;

      So how can I do that ? Thanks for any input.

      This world is going to explode due to international politics, SOON.

      C Offline
      C Offline
      Corinna John
      wrote on last edited by
      #2

      You could add a static read-only property that returns the constant.

      This statement is false.

      1 Reply Last reply
      0
      • A AmbiguousName

        hello guys... I have a type TClassFoo in which I have a const like this

        unit ClassFoo;
        interface
        uses ClassBase;

        type TClassFoo = class
        public const NUMBER = 100;
        end;

        I have two types TClassBase and TClassChild. TClassFoo and TClassBase have references of each other. I want to access the constant from TClassFoo in TClassChild like this..

        //*************************** Base Class ******************************//
        unit ClassBase;

        interface

        type TClassBase = class
        end;

        implmentation

        uses ClassFoo // had to put it here..otherwise it raises 'circular reference error'.
        var objFoo : TClassFoo;

        //*************************** Child Class ******************************//
        unit ClassChild;

        interface

        type TClassChild = class(TClassBase)
        // here I want to access TClassFoo.NUMBER
        end;

        So how can I do that ? Thanks for any input.

        This world is going to explode due to international politics, SOON.

        S Offline
        S Offline
        smags13
        wrote on last edited by
        #3

        What compiler do you use?

        TClassFoo.NUMBER

        should work.

        A 1 Reply Last reply
        0
        • S smags13

          What compiler do you use?

          TClassFoo.NUMBER

          should work.

          A Offline
          A Offline
          AmbiguousName
          wrote on last edited by
          #4

          smags13 wrote:

          TClassFoo.NUMBER

          This is exactly what I tried but it did not work. I am using Embercadero Delphi XE2 version. I am new to delphi.

          This world is going to explode due to international politics, SOON.

          S 1 Reply Last reply
          0
          • A AmbiguousName

            smags13 wrote:

            TClassFoo.NUMBER

            This is exactly what I tried but it did not work. I am using Embercadero Delphi XE2 version. I am new to delphi.

            This world is going to explode due to international politics, SOON.

            S Offline
            S Offline
            smags13
            wrote on last edited by
            #5

            class const is available since Delphi 7 (or Delphi 2009?) (http://edn.embarcadero.com/article/34324[^]), so your compiler is good. What type of error/complain did you get? Did it happen at compilation time or run time? I think what did not work may not relate to class const. You could try a simplified codes where only one class is defined with a class const, and then access it from a different unit, and see if it works, which I think it should.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups