473,396 Members | 1,997 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,396 software developers and data experts.

reusing an instance

Hi there.
It will sound like a stupid question, but i cant figure out how to resolve
this...

Doing a little profiler job on the app im writing, i found a bottleneck in
some parts that involve DB access to retrieve User information.
Basically, i have an "User" class. The ctor of this class uses a method in
the data access layer. As you may figure it out, the method execs a SP and
pass the info back to the class, who load the data into the fields.

All going fine, works perfectly and, best of all, its in line with MS best
practices :)

So, to avoid constant access to the DB, and taking in consideration that the
maximun amount of users wont exced 80 i told to myself... WTF, i will take
all the records from the DB and store they in a static dataset!

With this in mind, i went into the adventure of writing an static method
that takes a UserID as the parameter and return a User instance!

This method will first hit the DB and retrieve all records (just do it
once), then create an instance of the User class for each record retrieved,
and store thats instances in an arraylist. Meanwhile, i write a new internal
ctor for the User class, which takes a datarow as the only parameter (this
ctor is only called by the static method) so now the User class dosent
access the DB anymore.

Finally, i rewrite the public ctor of User class (the old ctor hit the DB)
to retrieve the "instance" previously generated by the static method.
Something like:

static User Retrieve(int UserID)
{
search the static arraylist by index looking for UserID
return (User)(arraylist(index))
}

public class User
{
public User(int UserID)
{
this = retrieve(UserID) // ERROR
}
}

but i get a "Cannot assign to '<this>' because it is read-only"
.... do i miss a chapter... ??? Cant i reuse an instance???

The goal is to write something like:
User usr = retrieve(an userID)
Thanks in advance!
Nov 16 '05 #1
2 2021
Mariano Drago <md****@softhome.net> wrote:

<snip>
but i get a "Cannot assign to '<this>' because it is read-only"
... do i miss a chapter... ??? Cant i reuse an instance???


You can't change what "this" is a reference to, no. Instead, you should
make your static method public. This is called the factory pattern, by
the way.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
So... looks like i miss a chapter :)
Thanks for your answer.

"Jon Skeet [C# MVP]" <sk***@pobox.com> escribió en el mensaje
news:MP************************@msnews.microsoft.c om...
Mariano Drago <md****@softhome.net> wrote:

<snip>
but i get a "Cannot assign to '<this>' because it is read-only"
... do i miss a chapter... ??? Cant i reuse an instance???


You can't change what "this" is a reference to, no. Instead, you should
make your static method public. This is called the factory pattern, by
the way.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ivan Voras | last post by:
Can this be done: (this example doesn't work) ---- class A: def a_lengthy_method(self, params): # do some work depending only on data in self and params class B:
2
by: Mariano Drago | last post by:
Hi there. It will sound like a stupid question, but i cant figure out how to resolve this... Doing a little profiler job on the app im writing, i found a bottleneck in some parts that involve...
2
by: Mariano Drago | last post by:
Hi there. It will sound like a stupid question, but i cant figure out how to resolve this... Doing a little profiler job on the app im writing, i found a bottleneck in some parts that involve...
4
by: Tedb | last post by:
Is there any reason why you can't reuse a delegate object instance versus creating a new one each time? For example in the following scenario I have a DataPoints object with an array of DataPoint...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.