473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Returning "this" or classtype from static method

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
5 1803
'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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Aryeh M. Friedman | last post by:
Why is the value of "Instance" whacked in the following code. Note this only happens in Graph::*Wrapper. Also please note that if I hardcode WalkFunc and PrintFunct to be BredthFirst and...
7
by: Daniel Ervi | last post by:
Hi All, I have a question for the group as I can't seem to come up with any suitable solutions. I'm not that new to programming or C#, but neither am I very fluent yet, so I'd appreciate any...
0
by: KathyB | last post by:
Hi, Using the following in an asp.net procedure. I get the error "The expression passed to this method should result in a NodeSet". Dim xDoc As New Document() The line causing the error is:...
6
by: Marty | last post by:
Hi, I have a class that I modified to be static. It is now a public sealed class and all function are static, no more constructor but a init() function to do the constructor job. This class...
5
by: ChrisB | last post by:
Hello: An object that is a field in another object has a constructor that requires a reference to the containing object: // object fields ChildObject childObject = new ChildObject(this); ...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
7
by: dhnriverside | last post by:
Hi peeps I'm just following this HOW-TO from MSDN.. http://support.microsoft.com/default.aspx?scid=kb;en-us;306355 But I've got a problem. I've adding the #using System.Diagnostics; line to...
2
by: danny.dion | last post by:
Hi ! I have a question about JScript : I have an object class wich dynamically creates a control in the page. Then it binds an event to that control, pointing on one of its methods (the...
7
by: psp | last post by:
I'm working on some code and I see in many methods that call other methods of the same class prefix a this-to the method being called, i.e. class X::methodA() { this->methodB(); } And they...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.