Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When to initialize fields? 1

Status
Not open for further replies.

dragonwell

Programmer
Oct 21, 2002
863
US
Thanks to all for the input on my previous question(s).

I have a more specific question now.

Let's say I have a "Car" class that essentially mirrors a row from a database, with properties such as "Year", "Make", "Model", "Mileage", etc. and an "ID" which is an integer matching the record's primary key in the database. Class Car has a constructor that takes an ID integer and sets this.ID to that number which can, at some point, be used to query the data needed for the other properties.

So, when it comes time to instantiate a Car object, I construct it using an ID integer. Should I not go ahead and run my database query in the constructor and set the rest of the properties then? I have heard that's not a good idea, and it makes sense to me for performace reasons.

My next thought is that perhaps the Car class should have a Fill() method, which runs the query and populates the rest of the fields. I could then instantiate the object, call it's Fill() method, and then go about accessing the properties. Is that a pattern ( accepted way of doing things)?

Is this the same as lazy initialization?

Hopefully someone can understand my question and help me out.

Thanks,
David
 
>> Is this the same as lazy initialization?

No, initialization refers to the difference between initialized and uninitialized. Some high level languages manage that for you and so you don’t actually have to contend with it. In C/C++ for example if you declare an integer variable

Code:
int num;

The value of num will be whatever was in memory at the location where num is assigned. Therefore it can be anything! Clearly not what you want so even if you don’t know the value for num because it is going to come from a database query you still want to initialize it to some value that makes sense in the context of your application.

Code:
int num = -1;

The term lazy evaluation refers to what you mentioned in your post about “waiting” until a point at runtime when some event triggers the need for a resource (the use of your database data) before you obtain the resource. If the event will always be triggered due to the nature of your application then lazy evaluation buys you nothing. If however the event may not be triggered the lazy evaluation will optimize your application.

-pete

 
Thanks. Interesting point about the memory and unilitialized variables.
 
I just started using C# and I'm trying to get out my old procedural, function-based style and program in a more object-oriented way. I'd like to know if the following makes good design sense:

In writing on the "Car" class example above, I provide at least two constructors -

1. An empty constructor that simply initializes fields to defaults:
Code:
public Car(){
    this.id = 0;
    ///etc...
}


2. One that takes an ID integer and a boolean for working with an existing record:
Code:
public Car(int ID, bool Fill){
    
    //set the id 
    this.id = ID;
    
    if(Fill){

        //run a query to get the 
        //values for the other properties
        //and set them here...

    }else{

        //set the remaining properties to 
        //their defaults...

    }
}
If all I want to do is update the database I can create my object

code]
new Car(myID, false)
[/code]

then set the properties with my new values (all of them coming from a form) and call the object's Save() method.

If I actually need to read any of the existing record's properties, I create the object like
Code:
new Car(myID, true)
which would run the query and set the fields to their values from the database.

How does that sound????

Thanks for your help,
[pipe]David

 
In general, I want to pass the "identity" values or references to it in the constructor.

I can imagine that the car has the primary key value as identity in the database, but also a frame serial number as an identity for the user. After construction, I want my objects to have at least their identity. These identity values are usually not mutable, so there are no setters for them.
So, in this case, yes, I would query the database upon construction.

Something else: I usually do not put the database handling in the objects themselves, but in the collection class they come from. See thread678-487289 .

Best regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top