By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

Returning "this" or classtype from static method

P: n/a
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
Jul 3 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
'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
"DamienS" wrote:
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
Jul 3 '08 #2

P: n/a
DamienS wrote:
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
Jul 3 '08 #3

P: n/a
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:am*****@gmail.com; blog:devkids.blogspot.com
Jul 3 '08 #4

P: n/a
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 :-)
Jul 3 '08 #5

P: n/a
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
On Jul 3, 11:44*am, DamienS <damiensaw...@yahoo.com.auwrote:
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 :-)
Jul 3 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.