Link to home
Start Free TrialLog in
Avatar of Plexo
Plexo

asked on

Question about saving objects

Hi experts,

I have a object _Login and I need save it. (line 6).
Then I created a new object _WLogin and I copied the _Login value. (line 7)
The problem is that, When I changed the properties of _WLogin, changed too the _Login properties of the. (Line 8)
The object _Login that not was changed, has a new value in _Login.LastLogin. (line 10)
What´s wrong?

Thanks.


1        public Login Logar(string Nome, string Senha)
2        {
3            try
4            {
5                PlexoEntities db = new PlexoEntities();
6                Login _Login = db.Login.FirstOrDefault(p => p.Nome == Nome && p.Senha == Senha);
7                Login _WLogin = _Login;
8                _WLogin.LastLogin= DateTime.Now;
9                Alterar(_WLogin);
10               return _Login;
11           }
12           catch (Exception ex)
13           {
14               PMensagem(this.ToString() + "\r\n" + ex.Message + "\r\n" + ex.InnerException);
15               return null;
16           }
17       }
18       #endregion
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

implement IClonable interface and override the Clone method of Login object, and replace line 7 to be:
Login _WLogin = _Login.Clone();

Avatar of Plexo
Plexo

ASKER

Hi sedgwick,

How do I  implement IClonable interface?

Thanlks.
these lines:

6                Login _Login = db.Login.FirstOrDefault(p => p.Nome == Nome && p.Senha == Senha);
7                Login _WLogin = _Login;


are NOT doing what you think they are doing.

_WLogin is actually a copy of the REFERENCE to _Login, not a completely new 'copy' of the object itself.

_Login is a 'pointer' (also called a reference) to a location in memory.  _WLogin holds a copy of the same reference (a copy pointing to the same location in memory).

Where have you defined the Login class?  YOu need to look up the definition of the ICloneable interface, which describes a single method Clone.  A Clone of an object creates a completely NEW instance of the undelying class, and then assigns ALL of the properties of the original object to the corresponding properties of the new instance, then returns new Instance as the result of the Clone method.

AW
There is an example in the MSDN article about ICloneable:

ICloneable Interface
http://msdn.microsoft.com/en-us/library/system.icloneable.aspx

using System;
using System.Collections.Generic;
using System.Text;

namespace ICloneableTest
{
    class Student : ICloneable
    {
        private string firstName;
        private string lastName;
        private string regNo;

        public string RegNo
        {
            get { return regNo; }
            set { this.regNo = value; }
        }

        public string LastName
        {
            get { return lastName; }
            set { this.lastName = value; }
        }

        public string FirstName
        {
            get { return firstName; }
            set { this.firstName = value; }
        }

        public Student(string _firstName, string _lastName, string _regNo)
        {
            this.firstName = _firstName;
            this.lastName = _lastName;
            this.regNo = _regNo;
        }

        #region ICloneable Members

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        #endregion
    }
}

===============================================
using System;
using System.Collections.Generic;
using System.Text;

namespace ICloneableTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create Student.
            Student student1 = new Student("abc", "pqr", "xyz");
            //Clone Student1.
            Student student2 = (Student)student1.Clone();
            //Here you can see it gives same value.
            Console.WriteLine("Before we change student1s first name");
            Console.WriteLine(student1.FirstName + " " + student2.FirstName);
            //By changing one objects property, you can understand they are not same reference.
            student1.FirstName = "ABC";
            Console.WriteLine("After we changed student1s first name");
            Console.WriteLine(student1.FirstName + " " + student2.FirstName);
            Console.Read();
        }
    }
}

Open in new window


MemberwiseClone is a protected inherited from the System.Object class, so it is only available to inheriting classes, and can only do light-weight cloning, which doesn't include reference cloning.  For that you would need to look into deep-cloning.

Fundamentals: Deep Cloning In C#
http://www.thereforesystems.com/fundamentals-deep-cloning-in-c/
Avatar of Plexo

ASKER

Hi Arthur_Wood.

I didn´t define Login Class. It is a Entitie, defined in PlexoModel.edmx.

Thanks.
Avatar of Plexo

ASKER

Hi TheLearnedOne.

Como a minha classe Login é uma entidade eu não consegui usar o exemplo.
Parece que o exemplo seria para uma classe Login criada manualmente.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Plexo

ASKER

Hi TheLearnedOne.

Excuse me. I copied the wrong part of the translator!
Avatar of Plexo

ASKER

Hi TheLearnedOne.

How I´m using Entity Framework, my difficulty was to understand where it should be placed the public partial class Login: ICloneable.
Just to record for future references, it should be placed in the EntityModel.Designer.cs before the end of region Primitive Properties.

Thanks