Delphi Static Class
Is it in Delphi (Win32) possible to declare a whole class (not only a function of the class) as static?
8 Answers
I assume you mean static classes like in .net (and not 'static' as in traditional Delphi/Native) - and the answer to that is no.
Looks like user search for 'class functions':
This is like static method, so, call it:
I would use an abstract class (not to be confused with an abstract method in a class) to prevent it from being instantiated instead of demoting the constructor to protected:
That will enforce the singleton pattern and prevent any instantiations period.
I am not quite sure what you mean by a 'static class'. You can declare a class, that has only class methods, so these methods can be called without instantiating the class.
Is that what you want?
Not natively.
Find clip studio serial number. It saves your time to write story and create animation character for animations.
Depending on what you need it for, if for the purposes of your code, in some use cases you could replace it with a Singleton Pattern object.
For walkthrough on implementing this I'd recommend this guide which, covers almost any version of delphi, but if you're using Delphi 2010 you could also use the new class Constructors/Destructors for improved results.
You could create a class that contains nothing but static methods. If you have to maintain some sort of state, then the state variables should be passed as var parameters. There is no way to 'properly' access static variables other than having a set of global variables in the implementation section of the class OUTSIDE the scope of the class, for example:
You can also create a new unit called uDG_Utils for example, define a class, define a global variable for that class and in the initialization and finalization section you manage the class constructor and destructor.Now all you need to do is call it like mySuperDuperClass.SuperMethod..
EDITI have edited this post to remove it. The answer is admittedly bad and deserves the down-votes. I don't want it to remain here to confuse or mislead anyone further.
Static variables and methods in Delphi 6 Hi everyone, Can anyone tell if Delphi 6 has anything like the 'static' keyword in C++. -- Colin 'Falling down is a mistake, not getting up is failure' Helen Keller |
Re:Static variables and methods in Delphi 6QuoteColin Shortland wrote: are the contexts I'm aware of: Used on a variable inside a subroutine so the variable keeps its value Used on a variable, constant, or subroutine to make it only available Used on a method of a class to make the method callable without having Used on a field of a class for the same purpose. Delphi has no -- |
Re:Static variables and methods in Delphi 6QuoteColin Shortland wrote in message .. (including methods). Just forget about it, it's impossible. But a field in a class behaves almost like one. Obviously, having Groetjes, |
Re:Static variables and methods in Delphi 6Quote'Maarten Wiltink' <maar..@kittensandcats.net> wrote in message Quote> Yes and no. You _can't_ have them in procedures and functions {$WRITEABLECONST ON} in version 6): {$WRITEABLECONST ON} procedure RetainKnowledge; procedure Test; You can simulate class-level static vars by declaring a variable after the Alistair Keys |
Re:Static variables and methods in Delphi 6QuoteAlistair Keys wrote in message .. Quote>> Yes and no. You _can't_ have them in procedures and functions Can you see the restraint that went into that sentence? Groetjes, |
Re:Static variables and methods in Delphi 6Quote> In my programs, constants don't behave that way. distinction there too. |
Re:Static variables and methods in Delphi 6QuoteMaarten Wiltink wrote: way I have configured the compiler on my system.' The qualification is important. The fact is, you *can* have local static variables in procedures and functions (including methods). -- |
Re:Static variables and methods in Delphi 6Quote'Maarten Wiltink' <maar..@kittensandcats.net> wrote in message constants have been in the compiler since TP 1 (I believe). I'll grant you that the name should have been changed to something like Static for ObjectPascal, but it wasn't. |
Re:Static variables and methods in Delphi 6QuoteOn Tue, 13 Aug 2002 22:56:22 +0000, Bruce Roberts wrote: how Java uses static. The only advantage I could see with delphi's consts, and their only use, Mark |
Re:Static variables and methods in Delphi 6QuoteBruce Roberts <b..@bounceitattcanada.xnet> wrote in message Quote
programming. However there are times when the double-take confusion caused by declaring a variable as constant (a comment helps) is not as great as hiding the variable in a field of the class, though it has taken me some years to accept this amiable little inconsistancy of such a strongly typed language. Dave |
Re:Static variables and methods in Delphi 6Quote'David Reeve' <drscienti..@powerup.com.au> wrote in message Quote> True.... but it hurts!!! What's in a name.... everything if you're would like to see is 1. the addition of a 'Static' or 'Persistent' modifier for local variables procedure Foo; var x : persistent integer = 0; or perhaps persistent x : integer = 0; 2. the addition of 'Class' as a modifier for class fields: tFoo = class (tObject) 3. the addition of 'Class' as a property modifier: published |
Re:Static variables and methods in Delphi 6Im Artikel <aj9nsr$72..@gnamma.connect.com.au>, 'Colin Shortland' <colic..@mydesk.net.au> schreibt: Quote>Can anyone tell if Delphi 6 has anything like the 'static' keyword in C++. In C the 'static' keyword is used to mark procedures and variables as invisible There exists another difference, which is not related to 'static', but often No real equivalent exists for static variables in subroutines. I'm don't know No direct equivalent exists for static variables in classes, at least not in type implementation var function MyClass.getStaticVar: integer; procedure MyClass.setStaticVar(value: integer); Often it's easier to give the static variable an unique name, and move it out DoDi |
Re:Static variables and methods in Delphi 6Thankyou you all, I didn't mean to start a war on semantics :) although now I think about it the question was a little ambiguous. However, the answer obviously and what I feared is a NO to all contexts which the word 'static' is used in C/C++ and Java. Once again thanks Colin 'Falling down is a mistake, not getting up is failure' Helen Keller |
Re:Static variables and methods in Delphi 6QuoteColin Shortland wrote in message .. 'Static' has several meanings in C, but since all people ever ask about is the equivalent of static local variables, I skipped the others. They aren't asked about because they _do_ have (obvious) equivalents. And even static local variables have a Turbo/Object Pascal equivalent. Groetjes, |
Re:Static variables and methods in Delphi 6QuoteColin Shortland wrote: I gave an answer of YES for three of the four contexts I'm aware of in C++. (You didn't say anything about C or Java before.) And the fourth context has a workaround that's nearly as good as the real thing, so I think your fear is unwarranted. -- |
1. Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)
2. class variables and static variables
3. Q. Static variables in classes in Delphi?
4. Are there static variables in Delphi functions?
5. Static variables in Delphi?
6. overiding static methods in Delphi 1.0
7. static methods in Delphi 1.0
8. static variables
9. 'Static' variables
10. Static variables???