Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reuse an object?

 
David Ogasawara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't I be able to reuse an object? My results are always the same.
(The QueryClass connects to a database and returns the result as a String.)

Here's an example:
QueryClass queryObj = new QueryClass();
String queryObjString_1 = queryObj.QueryClass("select id from user").toString();
String queryObjString_2 = queryObj.QueryClass("select name from user").toString();
Here are the incorrect results (they should not be the same):
queryObjString_1 = 52145
queryObjString_2 = 52145
--------------------------------------
However, it works when I do this:
QueryClass queryObj_1 = new QueryClass();
String queryObjString_1 = queryObj_1.QueryClass("select id from user").toString();
QueryClass queryObj_2 = new QueryClass();
String queryObjString_2 = queryObj_2.QueryClass("select name from user").toString();
Here are the correct results:
queryObjString_1 = 52145
queryObjString_2 = Dave
Any help would be greatly appreciated.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to be using QueryClass both as a Constructor and as a method somehow. It would be interesting to see your QueryClass definition. If you post it please enclose the code between UBB code brackets which you get by using the CODE button sited under the edit box.
 
David Ogasawara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
That is exactly what I am doing. I guess it's incorrect?
Here's my class:

 
Billybob Marshall
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of problems:
1) Your method name should not be the same as the class name. Name it something according to what it does, like 'doQuery' for example.
2) Your 'result' class member should probably be moved inside the method to be just a normal method variable (afterall, is it referenced by any other methods, or is the previous value of importance?)
That said,
My guess would be that the second time the method executed, it didn't change the 'result' reference - maybe you conditionally set it (which you're not showing in the snippet you posted)? Otherwise it should have worked as desired (albeit sloppily).
 
David Ogasawara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for pointing me in the right direction. That's something I really needed.
-Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic