Click here to Skip to main content
15,904,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, may be this question already has been asked but I am still confused in this. Say I have a class named A which contains some properties, a method to initialize its property, a method to save the object.
then what is best practise to call the save method of the object?
this
C#
A objA = new objA();
objA.Name = "abc";
objA.Address= "xyz";
objA.Save();



OR

C#
A objA = new objA();
objA.Name = "abc";
objA.InitializeProperties("abc", "xyz");
objA.Save()



and suppose another class named B which contains other class object as private property.
When I call B.Update, the following method is executed. in this scenario which practice is better?


C#
public class B
{
 public string Name{get;set;}
 public string Address{get;set;}

 private OtherClass otherClassObject {get;set;}

 private void MappObject(ref OtherClass otherClassObject)
{
otherClassObject.Name = this.Name;
otherClassObject.Address= this.Address;
}

public void Update()
{
 this.MappObject(this.otherClassObject)
otherClassObject.Update();
}
}
Posted
Updated 20-Aug-13 3:29am
v2
Comments
ZurdoDev 20-Aug-13 8:41am    
Go with the first way.

First approach should be a good approach.According to you say there is a class named A with mentioned properties and methods.Then i can initialize the object of that particular class by

C#
A objA = new A()
{
    Name="abc",
    Address="xyz"
};
objA.Save();

or else you can go for the above solution1 approach.
 
Share this answer
 
The second method has redundance and should be avoided.
Both first and second method allow a possibly 'uninitialised' object to be saved.
I would define a constructor the way OriginalGriff suggested, in order to put the object in a initialized state on its very creation.
 
Share this answer
 
Comments
fjdiewornncalwe 20-Aug-13 11:39am    
+5.
I'd go with the first way, or create a constructor:
C#
public A(string name, string address)
   {
   Name = name;
   Address = address;
   }
...
A objbA = new objA("abc", "xyz");
objA.Save();
 
Share this answer
 
Comments
CPallini 20-Aug-13 9:22am    
5.
fjdiewornncalwe 20-Aug-13 11:40am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900