On Tue, 10 Oct 2006 12:33:01 -0700, VMI
<VM*@discussions.microsoft.comwrote:
>I have a class Person that includes several properties (i.e. Name, ID) and a
method storeData() that stores these same values to a database.
What's the correct method of instantiating this class when another method in
another class is storing data to the properties and calling storeData()? I'm
currently doing it this way:
protected void Button_Click(...)
{
Person person1 = new Person();
person person2 = new Person();
The c'tor indicates person2 should be of type Person rather than type
person ... or is it the otherway around?
person1.Name = "John Smith";
person1.ID = "123";
person2.storeData(person1);
}
Is this the correct way?
It depends if person1 and person2 are of the same type or not. If they
are of the same type then the storeData method should serialize the
object's data as in
person1.storeData();
If Person and person are different types then you're moving in the
right direction but the naming should be more distinctive/descriptive.
Even then I would go with
person1.storeData();
with the method being something like
public void storeData()
{
new PersonDB().storeData(this);
/*
PersonDB personDB = new PersonDB()
personDB.storeData(this);
*/
}
regards
A.G.