473,382 Members | 1,736 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,382 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 2018
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...
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: 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: 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?
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...

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.