1

I am a .NET developer and very beginner with Delphi.

I want to create a static class and use the properties like this

MyVariable := MyStaticClass.MyProperty;

I don't know how to do that.

2 Answers 2

2

Use Class Properties for this;

 type
    TMyClass = class
      strict private
        class var         // Note fields must be declared as class fields
           FRed: Integer;
           FGreen: Integer;
           FBlue: Integer;
        public             // ends the class var block
           class property Red: Integer read FRed write FRed;
           class property Green: Integer read FGreen write FGreen;
           class property Blue: Integer read FBlue write FBlue;
    end

In this example, properties named Red, Green and Blue can be used without an object reference. For document; Class Properties

To declare a readonly class (static) property;

  TMyClass = class
    private
      const FRedValue:Integer = 10;
    public
      class property Red: Integer read FRedValue;
  end;

To use this;

aColorVariable := TMyClass.Red;
Sign up to request clarification or add additional context in comments.

12 Comments

how can i implement that ?should i create unit ? and i had a lot of errors
No you do not have to create a new unit, you can declare multiple class in a unit. Just copy the code except type keyword, and paste it in a unit under existing type keyword. But to have a clean code, splitting objects into different units can be a good practice.
i want to set static data for that and use it on other class . how can i change [ class property Red: Integer read FRed write FRed;] to class property myprop: Integer read '10' write '10';
@sddk You can have multiple type keywords in one unit!
@sddk Yes It Must Be ReadOnly
|
0

Tips : Unit somhow equals to Class on .NET Finally i resolve my problem by add those properties to my unit like below :

  1. create a unit by right click on the solution name and add > unit

  2. declare a const like below :

    const myPropertyName:Integer = 0;

  3. add it to your form who you want to use it by

    uses YOUR_UNIT_NAME;

After

interface

section on the form

  1. And all i need to do is type my property name who i declared on the unit( myPropertyName)

3 Comments

No, this is not a static property!
What you defined is called a global constant.
All I need is property who I can call everywhere I want on entire solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.