Dans le code ci-dessous, je voudrais utiliser le code dans la méthode de chargement statique pour actualiser l'objet ... mais comment réinitialiser l'objet actuel avec un nouvel objet? Copier le champ par champ est le seul moyen?Réinitialiser avec un nouvel objet
class WIP
{
// <Snipped> Various other properties...
public Boolean Refresh()
{
// Need to change the current object with the updated object
this = WIP.Load(this.ObjectID); // Says this is readonly...
return true;
}
public static WIP Load(long ObjectID)
{
// This static method fetches the data from DB and returns the object.
}
}
Edit: Je viens d'avoir cette idée après avoir affiché la question ... Y a-t-il des pièges dans ce domaine?
class WIP
{
// <Snipped> Various other properties...
public Boolean Refresh()
{
// This method fetches the data from DB and updates the object.
}
public static WIP Load(long ObjectID)
{
WIP newObject = new WIP();
newObject.ObjectID = ObjectID;
newObject.Refresh();
return newObject;
}
}
Non, vous ne pouvez pas faire cela. – leppie
Il serait déroutant car "Load" de l'objet déléguerait la charge à la méthode d'actualisation de l'objet. –