Delphi Writing constant Unit technique _delphi

Source: Internet
Author: User

Usually we write Delphi constants are directly defined in the use of cell files, a bit of programming habits may be used to put the system constants in a separate file, but also similar to the following way:

Const
Default_user_name = ' ADMIN ';
Default_user_type_code = 0;

Accustomed to Java, it is customary to divide different constants into different classes. This makes it easy to separate system constants, user constants, database constants, and so on. So in Delphi also want to reference a constant unit, and then in the button event and so on inside this use:
Edit1.text: = Constants.default_user_name;
Spinedit1.value: = Constants.default_user_type_code;

But it's hard to find such information on the web, so find out for yourself. It is found that using the structure const can achieve this effect, written as follows:

Unit unitconstants;

Interface

Uses
Classes, Types;

Type
Constantrecord = Packed record
default_user_name:string;
Default_user_type_code:integer;
End

Const
Constants:constantrecord = (
Default_user_name: ' ADMIN ';
default_user_type_code:0
);

Implementation

End.

Then in the quoted unit, you can use Constants.default_user_name and constants.default_user_type_code to facilitate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.