?
Solved

static classes

Posted on 2005-05-16
16
Medium Priority
?
317 Views
Last Modified: 2013-11-23
Hi,
Is there is a way to create static classes in delphi?
The documentation does not talk about them but about static methods.
Thanks
0
Comment
Question by:oalawna
  • 8
  • 5
  • 2
  • +1
16 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 14011535
What sort of class object do you want to make?

type
  TMyClass = class
  private
    iValue: Integer;
    sValue: String;
  public
  end;

Something like that?
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 14011538
 TSomeClass = class
  private
  public
    class procedure Test();
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  TSomeClass.Test
end;

{ TSomeClass }

class procedure TSomeClass.Test;
begin
  ShowMessage('hello from static procedure')
end;
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 14011562
>>The documentation does not talk about them but about static methods.
never mind
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:geobul
ID: 14011575
Hi,

What do you understand by 'create static classes' ? What visibility/behaviour you're trying to achieve?

Regards,Geo
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 14011577
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 14011580
basically, you declare your properties with a "setter" and "getter" that are both static:

TMyObject = Class
  private
    class function GetClassInt: integer;
    class procedure SetClassInt(const Value: integer);
  public
    property ClassInt : integer read GetClassInt
                                write SetClassInt;
  end;
0
 

Author Comment

by:oalawna
ID: 14011682
thank you all. Static classes do not have to be instantiated. the may hold program-wide constants.
check this page and find "static class" there.
http://www.gekko-software.nl/DotNet/Art01.htm

I want constant that can be accessed from all units. And these constents are actually record constants.
I asked about static classes because i could not figure out how to create record constents and access them from everywhere.

If there is no real static classes then how can we create constent records and make them visible everywhere? I tried declaring them in the interface in a unit but that did not work

0
 

Author Comment

by:oalawna
ID: 14011704
Constent=constant:-)
0
 

Author Comment

by:oalawna
ID: 14011715
Here is something from c#

A static class has only constant properties and static (class) methods. No objects will be created of a static class. It can have a static constructor to initialize the class, this will be executed when the program loads. A static class is a good place to house constants which will be used by several other classes.

class SharpGuids

{
/* Guid as string  */
public const string intfguid = "E03D715B-A13F-4cff-92F1-0319ADB3DE5F";
public static readonly System.Guid idintf;
/* Static constructor to init the Guid based on the string */
static SharpGuids()
    {    
        idintf = new System.Guid(intfguid);
    }
}
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 14011760
right... static classes don't exist in Delphi (at least until version 7)

you can mimic some of it's behaviour, but by definition they don't exist
0
 

Author Comment

by:oalawna
ID: 14011776
Thanks BlackTigerX. Do you know how to declare record constants and make them public?
0
 
LVL 17

Assisted Solution

by:geobul
geobul earned 800 total points
ID: 14011790
Hi,

I asked that because static classes have different meaning in Java and C#. So, your goal is C# definition, obviously.

My answer is that Delphi doesn't need static classes because it has regular functions and procedures which aren't members of any class. You may use initialization and finalization sections of the unit instead of constructor and destructor.

To be able to use such functions/procedures just add the unit name where they are declared to the 'uses' clause of the unit where you want to use them.

Regards, Geo
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 14011821
>>Do you know how to declare record constants and make them public?

just put them in any unit in the interface section, and they will be available to any unit that uses that unit, same as any functions and procedures that you declare there as geobul explained
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 14011841
oh... but record constants... you could initialize them in the initialization section
0
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 1200 total points
ID: 14011866
or using arrays:

  TsomeRecord = record
    I:Integer;
    Name:string;
  end;


const
  SomeRecord:array[0..0] of TSomeRecord=((I:0; Name:'hello'));
0
 

Author Comment

by:oalawna
ID: 14011892
Thank you all.
Very much:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question