Re: enums, using methods as initializers
Mikhail Teterin wrote:
Daniel Pitts wrote:
Yes, kind of. You won't be able to pass in a method, but you can create
a delegate method easily.
:(
I have an example here actually:
<http://virtualinfinity.net/wordpress/program-design/2007/10/22/using-enums-as-a-flyweight-pattern/>
In your example, each case of the enum spells-out its methods in full.
It is workable, of course, but I wanted to have list the cases as a kind
of a /table/ -- preferably one case per line.
I'm pretty certain, I can do this with my own /data/:
FIELD1 ("string"),
FIELD2 ("double"),
...
FIELDN ("Timestamp);
private String type;
and then use the type to tell me, which of method of the foreign class to
call:
if (type == "string")
return rs.getString(......)
if (type == "double")
return rs.getDouble(......)
.... a case for each type ....
But it would all have been much easier, if I could pass the
/methods/ the same way I can pass function-pointers in C or C++.
Thanks!
-mi
You can use reflection in this case, but thats not necessarily a good
idea. Reflection can add unnecessary complexity.
I gather from your previous posts that you are used to programming in
C/C++, and finding more concise manors to express a particular effect of
code. Just remember, more lines doesn't mean more complex. In fact,
/sometimes/ it means less complex :-)
Like I said, you *can* use reflection for this, but I advise against it:
<http://virtualinfinity.net/wordpress/program-design/2007/01/11/the-dangers-of-reflection-or-put-down-that-mirror/>
What you're trying to do sounds a lot like something that I did a while
ago. It also sounds like you would be better off using Hibernate or some
other ORM solution. Trust me, the ramp-up time is well-worth the
maintenance costs down the road.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>