Returning "this" or classtype from static method

D

DamienS

Hi,

I have a static method in a class and I need to be able to return a
reference to "this". Googling around, I found a heap of discussions of
the pros/cons of "abstract static" etc. It was quite a heated debate
about purity of OO design that just did my head in a bit.

In a nutshell. Can a static method 'know' what class it's defined in
and return that type information?

Thanks in advance,


Damien
 
A

Arne Vajhøj

DamienS said:
I have a static method in a class and I need to be able to return a
reference to "this". Googling around, I found a heap of discussions of
the pros/cons of "abstract static" etc. It was quite a heated debate
about purity of OO design that just did my head in a bit.

In a nutshell. Can a static method 'know' what class it's defined in
and return that type information?

A static method can not return this, because it does not
have a this.

A static can return the type of the class. I don't know if there
is much point though.

Arne
 
D

David Anton

'this' has no meaning within a static method. A static method is not called
via an instance, so what would you expect for 'this'? There could be many
instances of the class that are 'active', but none of them could possibly be
interpreted as 'this' inside of a static method.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
A

Alex Meleta

Hi DamienS,

It already knows a class but doesn't know the instance. It can get you an
instance only if you provide it for method or provide enough information
to obtain the certain one from many ones. But, actualy, the base purpose
of static method is to be abstracted from certain instance, which 'this'
refers to, to have implemantation common for class-level (non-instance) area.
E.g. Think about singleton, and how it works.

Regards, Alex Meleta
mailto:[email protected]; blog:devkids.blogspot.com
 
D

DamienS

Thanks all.

I suppose that that makes sense.

What I wanted to do is have a method to determine (as a string) the
class name that a static method was in from within that method. I
suppose that you could argue that, seeing as I was writing that code
in the class, then I knew what the class name was. Anyway - that's a
trivial point.

Still - I think that there's a limitation in the langage around static
members. It's really cool to be able to define a static method on a
class - however it's a shame that it can't participate in inheritence
or even interfaces. Yes, I know, 'there's no object instance' and so
nothing to override from etc... however it would be cool for you to be
able to call a static member on class and, that in turn call the base.
method of the same name and have it invoke the similarly static method
on base class. Sure, there's no object instance - however the compiler
_does_ know what the base class is and I'm sure could figure out the
desired method to call.

Also - it'd be great if you could use an Interface to enforce that all
implementing classes provide a static method of given signature.
Anyway - I'm waffling. I guess that Anders and the other MS guys know
more about this than I do and had good reasons for not allowing static
methods in interfaces... however whatever they were, the fact remains
that the code I wrote today would have been alot more elegant, robust
and maintainable with them.

That's enough ranting from me today....

.... I'm going skiing now :)
 
S

salman

But there is an alternative way to return 'this' reference from static
function of class.
you have to declare a static object (any named like '_this') of class
and assign 'this' reference to the static object ('_this') in
constructor of class.

Ali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top