Board index » delphi » Global methods to Class methods

Global methods to Class methods


2003-07-31 12:35:41 AM
delphi132
I have a CommonUtils unit with several global methods. I am considering
making a TCommonUtils class and making these global methods class methods. I
would never instantiate this class, only call it is class methods. it is not a
big deal, but it seems cleaner than globals and could avoid name conflicts.
Anyone doing this ?
Danny
 
 

Re:Global methods to Class methods

"Daniel Mauric" <danny at neobee dot net>writes
Quote
I have a CommonUtils unit with several global methods. I am considering
making a TCommonUtils class and making these global methods class methods. I
would never instantiate this class, only call it is class methods. it is not a
big deal, but it seems cleaner than globals and could avoid name conflicts.
Anyone doing this ?
Note that class methods come with the extra burden of passing the class
reference as an argument so Self works within the method. That extra argument
may or may not matter to you in terms of performance. However, since nothing is
really served by this, I'd advise you not to do it. If you want to avoid
name clashes, you can always fully qualify the functions by its unit name.
sm
 

Re:Global methods to Class methods

Quote
Note that class methods come with the extra burden of passing the class
reference as an argument so Self works within the method.
Not with class methods. You can not use Self in a class method, since
this would force the need for an instance.
Regards from Germany
Franz-Leo
 

Re:Global methods to Class methods

"Franz-Leo Chomse" <XXXX@XXXXX.COM>writes
XXXX@XXXXX.COM...
Quote
>Note that class methods come with the extra burden of passing the class
>reference as an argument so Self works within the method.

Not with class methods. You can not use Self in a class method, since
this would force the need for an instance.
Actually you can, but self refers to the class type itself, and not an
instance.
Cheers,
Ignacio
 

Re:Global methods to Class methods

"Franz-Leo Chomse" <XXXX@XXXXX.COM>writes
Quote

>Note that class methods come with the extra burden of passing the class
>reference as an argument so Self works within the method.

Not with class methods. You can not use Self in a class method, since
this would force the need for an instance.
You are mistaken. Self refers to the class reference of the class the class
method is contained in. that is a lot of occurences of 'class' but I hope it
still makes sense.
sm
 

Re:Global methods to Class methods

"Sebastian Moleski" <XXXX@XXXXX.COM>writes
Quote
"Franz-Leo Chomse" <XXXX@XXXXX.COM>writes
news:XXXX@XXXXX.COM...
>
>>Note that class methods come with the extra burden of passing the class
>>reference as an argument so Self works within the method.
>
>Not with class methods. You can not use Self in a class method, since
>this would force the need for an instance.

You are mistaken. Self refers to the class reference of the class the
class
method is contained in. that is a lot of occurences of 'class' but I hope
it
still makes sense.
Franz-Leo is not entirely wrong as Self behaves differently in a Class vs.
Object context:
Concerning "Self" in class methods:
<quote from Delphi7 help>
In the defining declaration of a class method, the identifier Self
represents the class where the method is called (which could be a descendant
of the class in which it is defined). If the method is called in the class
C, then Self is of the type class of C. Thus you cannot use Self to access
fields, properties, and normal (object) methods, but you can use it to call
constructors and other class methods.
A class method can be called through a class reference or an object
reference. When it is called through an object reference, the class of the
object becomes the value of Self.
</quote from Delphi7 help>
 

Re:Global methods to Class methods

Bob Dawson writes:
| Right--that's a difference, for example, between Delphi class methods and
| C# class methods, and a reason why virtual class methods are natural in
| Delphi but missing from C#.
I use virtual constructors and class methods in C# because I wrote a class
to emulate the class class, that has a virtual Create function to emulate
the constructor and a mechanism for calling the appropriate class method.
Joanna
 

Re:Global methods to Class methods

"Joanna Carter" wrote
Quote

I use virtual constructors and class methods in C# because I wrote
a class to emulate the class class,
Didn't say it couldn't be done :-)
bobD
 

Re:Global methods to Class methods

"Sebastian Moleski" <XXXX@XXXXX.COM>writes
Quote
[...]
If you want to avoid name clashes, you can always fully qualify the
functions by its unit name.
The problem with using unit name is that it is optional, hence the problems.
But honestly, name clashes are not that much of a concern, it just feels
better to have methods in a class than orphaned :)
Danny
 

Re:Global methods to Class methods

"Daniel Mauric" <danny at neobee dot net>writes
Quote
A step further the same path.
Warning: some might consider this as pure madness.

In my previous post I made global methods class methods, now I would like to
loose global variables too. There are no class variables, so we need to
create instances. Since we need instances to hold the variables we might as
well make the class methods normal methods.
If you really want to get rid of global variables, why don't you write a class
function that returns a value as a replacement of the global variable?
sm
 

Re:Global methods to Class methods

"Joanna Carter" <XXXX@XXXXX.COM>writes
Quote
I use virtual constructors and class methods in C# because I wrote a class
to emulate the class class, that has a virtual Create function to emulate
the constructor and a mechanism for calling the appropriate class method.

Wow.
 

Re:Global methods to Class methods

Daniel Mauric writes:
| Problems arise if you want to make it thread safe, otherwise it is ok.
Use a Critical section in the accessor methods or a threadvar.
Joanna
 

Re:Global methods to Class methods

"Dennis Landi" <none[at]none.com>writes
Quote

"Joanna Carter" <XXXX@XXXXX.COM>writes
>I use virtual constructors and class methods in C# because I wrote a
class
>to emulate the class class, that has a virtual Create function to
emulate
>the constructor and a mechanism for calling the appropriate class
method.
>

Wow.
It's true - I have seen it done.
The Class class (class Class in C#) used as the class type of another class
caused much amused confusion.
Of course you would need an instance of the Class class (a Class object?) to
achieve what you would with a TClass, but with GC it was no big deal.
Achieving virtual construction without subclassing class Class (C#) was
quite impressive.
Always happy to clarify :)
Joe
 

Re:Global methods to Class methods

"Joe Otten" <XXXX@XXXXX.COM>writes news:3f296818
Quote
Always happy to clarify :)

:-) classy
 

Re:Global methods to Class methods

Peter Thönell writes:
| Just wondering... I understand how class records work and the like, so I
| can understand the unwillingness by Borland to make class variables
| available.
Why is that?
| But, would there much difficulty in making class properties available?
| They would require methods rather than fields (just like interfaces do).
| That way one could make getter and setter methods to unit variables.
If you have tried the D4DN preview, you would see that class fields and
properties are both implemented in the new compiler.
Joanna