3
I have in memory (not yet stored in database) an object of type List<NotaFiscal>
. I need to pass an item from this list as a parameter to a screen to make changes to that object. However it may be necessary for the user to cancel the changes, and object to return to its original state.
Then, when opening the screen you will receive this object from NotaFiscal
, I clone this object using MemberwiseClone()
.
Class Notafiscal
public class NotaFiscal
{
public string Numero { get; set; }
public string Serie { get; set; }
.........
public ICollection<NotaFiscalProduto> Produtos { get; set; }
public NotaFiscal Clone()
{
return (NotaFiscal) MemberwiseClone();
}
}
Class Telanotafiscal
public partial class TelaNotaFiscal: Window
{
private readonly NotaFiscal _notaBackup;
private readonly NotaFiscal _nota;
private bool _cancelarAlteracao;
public NotaFiscal Nota => !_cancelarAlteracao ? _nota : _notaBackup;
public TelaNotaFiscal(NotaFiscal nota)
{
InitializeComponent();
_notaBackup = nota.Clone();
_nota = nota;
}
}
Class Telegraphy
private void btnNf_Click(object sender, RoutedEventArgs e)
{
var botao = (Button)sender;
var nota = (NotaFiscal)botao.DataContext;
var telaNota = new TelaNotaFiscal(nota);
telaNota.Closed += (sen, es) =>
{
nota = telaNota.Nota;
};
telaNota .ShowDialog();
}
If the user cancels the change, the properties Numero
and Serie
return to the previous value. However, if _nota.Produtos
, this is also reflected in _notaBackup.Produtos
Immediate Window
Analyzing the Immediacy I can conclude that the objects _nota
and _notaBackup
are different, but the zero positions of Produtos
are the same objects.
_note. Products[0]. Gethashcode()
45644990
_notaBackup.Products[0]. Gethashcode()
45644990
_note.Gethashcode()
59505294
_notaBackup.Gethashcode()
23797978
What could you do to fix this problem? Or what other alternative to edit this object in memory and reverse the changes if necessary.
I think the
GetHashCode()
does not do what you imagine. Different objects with equal values will have the same hashcode.– Maniero
I didn’t know that. "I found" this sort of unique object identifier. Thank you
– user26552
The unique identifier of the object is the object itself and it is only valid under certain circumstances.
– Maniero
http://answall.com/questions/184019/o-que%C3%A9-hashcode-e-qual-sua-finalidade/184034#184034
– user26552