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

static class ...err.. pointer

P: n/a
i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()

....is this possible, or i'm i short on reading?

Jan 17 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a

evaristobo wrote:
i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()
Since static methods apply to the class as a whole rather than specific
instances of it, then you could achieve the above more simply with:

classStatic.staticMethod();

There's no need to have an instance of it.

Jan 17 '07 #2

P: n/a
On 17 Jan 2007 06:18:47 -0800, "evaristobo" <ne***********@gmail.com>
wrote:
>i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()

...is this possible, or i'm i short on reading?
So you want to index different types in order to access one
identically named static method on one of them?

It's not as easy as storing multiple objects, but it's certainly
possible using a mechanism known as "reflection".

Variant 1. Store the class name (e.g. "classStatic") in your
hashtable, and then use reflection to get the TypeInfo object for the
class, then the MethodInfo object for your method, then invoke the
method. That works but it's cumbersome and slow to execute.

Variant 2. Same as 1, but when adding a new element to your hashtable
you immediately determine the TypeInfo and store that instead of the
class name. Even better, if you always call just one method you
determine its MethodInfo object and store that.

Variant 3. Same as 2, but you declare a delegate that matches your
staticMethod, and when adding a new hashtable element you store a
delegate object for the new type's method, not a MethodInfo object.

Variant 4. Same as 3, but you avoid reflection altogether by having
the static classes add themselves to the hashtable. They can
immediately create a delegate for one of their own methods, without
having to use reflection and run-time name lookups. Big disadvantage
here: each of the static classes must have an "adder" method, or else
some central "adder" method has to have all static classes hardcoded.
--
http://www.kynosarges.de
Jan 17 '07 #3

P: n/a
....thank you very much Chris and Bobbo, but i was trying to escape
Reflection here, you see, i'm trying to design a registry object, which
is nothing more than an hashtable for the client code to query it for
objects who will follow a strict naming convention, this convention is
drawn from what i want the application framework to be, or better, to
supply. As an example, all application will have to supply a 'logger'
object, a 'dbConnection' object, a 'threadManager' object, and so on.
And then i want to get the 'extension.functionalityX' plugin which is
loaded from the plugins folder and implements MyFramework.IPlugin
interface...what do you think about this, how do you design your app
frame?

On Jan 17, 3:03 pm, Chris Nahr <dioge...@kynosarges.dewrote:
On 17 Jan 2007 06:18:47 -0800, "evaristobo" <newsletter...@gmail.com>
wrote:
i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()
...is this possible, or i'm i short on reading?So you want to index different types in order to access one
identically named static method on one of them?

It's not as easy as storing multiple objects, but it's certainly
possible using a mechanism known as "reflection".

Variant 1. Store the class name (e.g. "classStatic") in your
hashtable, and then use reflection to get the TypeInfo object for the
class, then the MethodInfo object for your method, then invoke the
method. That works but it's cumbersome and slow to execute.

Variant 2. Same as 1, but when adding a new element to your hashtable
you immediately determine the TypeInfo and store that instead of the
class name. Even better, if you always call just one method you
determine its MethodInfo object and store that.

Variant 3. Same as 2, but you declare a delegate that matches your
staticMethod, and when adding a new hashtable element you store a
delegate object for the new type's method, not a MethodInfo object.

Variant 4. Same as 3, but you avoid reflection altogether by having
the static classes add themselves to the hashtable. They can
immediately create a delegate for one of their own methods, without
having to use reflection and run-time name lookups. Big disadvantage
here: each of the static classes must have an "adder" method, or else
some central "adder" method has to have all static classes hardcoded.
--http://www.kynosarges.de
Jan 17 '07 #4

P: n/a

evaristobo wrote:
i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()

...is this possible, or i'm i short on reading?
Whenever this happens to me, I resort to singletons. Or, possibly,
sort-of-singletons that can participate in inheritance.

The other situation that causes me to think "singleton" is wanting a
static class that I can pass to a method as an argument, again because
I want polymorphism.

If you make your static class a singleton that implements an interface,
say IMyInterface, then you can create a hash table of IMyInterface
objects. The fact that the class is a singleton means that there can
only ever be one of them, and it's accessible from the static space,
viz:

ClassSingleton.Instance.InstanceMethod();

Jan 17 '07 #5

P: n/a
On Jan 17, 7:52 pm, "Bruce Wood" <brucew...@canada.comwrote:
....
If you make your static class a singleton that implements an interface,
say IMyInterface, then you can create a hash table of IMyInterface
objects. The fact that the class is a singleton means that there can
only ever be one of them, and it's accessible from the static space,
viz:

ClassSingleton.Instance.InstanceMethod();
.....that's it! that's my solution for this problem, i have an
application registry as a Registry class, itself a singleton too,
implementing an interface IRegistry, for example. Then i have a
Singleton singleton class accessible at the startup procedure when the
core framework is putting its act together, then i register my
singleton like
Registry.register("mysingleton",Singleton.getInsta nce());
then all i have to do is supply a reference to Registry for whichever
object needing to use it by the IRegistry interface, which must be
located in a public assembly, well, but that's another story...

I can thank you enough
best regards
joao viegas

Feb 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.