473,378 Members | 1,106 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,378 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 1810
'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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.