3

OK, this is the problem that I'm trying to solve - I want to assign object properities returned through linq from db, to this object prperties and get possibility to update any changes, without inserting new row in db.

partial class Employee : Person
{
    public Employee(int id = 0)
    {
        if (id > 0)
            this.get(id);
    }

    public override bool get(int id)
    {
        Employee empl = db.Employees.Single(Employee => Employee.id == id);
        if (empl != null)
        {
            // here I need to do sth like
            this = empl;
            // or
            ObjectProperties.Copy(empl, this);
            return true;
        }
        else
            return false;
    }

    public override void save()
    {
        if (this.id > 0)
        {
            db.SubmitChanges();
        }
        else
        {
            db.Employees.InsertOnSubmit(this);
            db.SubmitChanges();
        }
    }

    public override void remove()
    {
        if (this.id > 0)
        {
            db.Employees.DeleteOnSubmit(this);
            db.SubmitChanges();
        }
    }
}

And I don't want to make get method static.

kryski
  • 414
  • 2
  • 6
  • 18
  • look here http://stackoverflow.com/a/69988/1714342 – Kamil Budziewski Aug 26 '13 at 12:06
  • 3
    What's the point of this? Is your intent to use something like `ref` to have all references to this instance change to another instance altogether? Or do you just want to update all the properties of your `MyClass` (like `id`) to new values? – Chris Sinclair Aug 26 '13 at 12:15
  • I just want to update all the proprties of MyClass. – kryski Aug 26 '13 at 12:21
  • If you just want to update all the properties of MyClass, then you should a) update all the properties of MyClass one at a time or b) create and call a method in MyClass that updates all the properties of MyClass one at a time or c) create a new instance of MyClass – Brian Kintz Aug 26 '13 at 12:29
  • if you just want to copie the properties to your object then you could create a simple method with used `sourceObj.GetType().GetProperties();` – WiiMaxx Aug 26 '13 at 12:31
  • Is there a method in c#, that could copy all properties from some object to this object? e.g. objectProperties.Copy(NewClass.create(), this) – kryski Aug 26 '13 at 12:36
  • @user2211974: No built-in C# language feature, no. You can do so via reflection (this would likely be shallow copies), but it seems terribly unnecessary in this case. You may as well just write the couple lines copying each field's/property's value. – Chris Sinclair Aug 26 '13 at 12:47
  • I solved problem by creating a method to copy the object properties and adding the code: `this.db = new Database(); db.Employees.Attach(this);` I suppose it's not optimal, but I don't heve better ideas. – kryski Aug 27 '13 at 09:23

3 Answers3

5

It's a bit hard to give you a good example/suggestion of what to do as we don't have much context about what your application or end-goal usage is. However, outside of the struct scenario posted, you can't reassign the this reference. Depending on your usage/application, it might be possible or more correct to use the ref keyword, but I'm not sure on that without more information about your usage.


However based on your comment that you just want to update all the properties of your class, it might be simplest to have a kind of "CopyInfo" method which would just copy over the values from another instance:

class MyClass
{
    public MyClass()
    {
        this.id = 2;
    }

    public void get()
    {
        CopyInfo(NewClass.Create());
    }

    private void CopyInfo(MyClass otherInstance)
    {
        this.id = otherInstance.id;
    }
}

Another option might be to extract the various properties to an interface, then you can create a kind of wrapper around a reassignable instance of your class:

public interface MyInterface
{
    public int id { get; }
}

class MyClass : MyInterface
{
    public int id { get; private set; }

    public MyClass()
    {
        id = 2;
    }
}

Then the wrapping class might be:

class MyReassignableClass : MyInterface
{
    private MyClass BackingInstance;

    public int id
    {
        get
        {
            return BackingInstance.id;
        }
    }

    public void get()
    {
        BackingInstance = NewClass.create();
    }
}

Your code can treat it like a MyInterface, but accessing its members is really indirectly accessing the members of another instance which can be swapped out as needed.

EDIT: Finally, I would also suggest you take a peek at the C# Naming Guidelines to make sure your code is a bit more consistent with most C#.NET code out there.

Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
  • he could also create a generic one by using `sourceObj.GetType().GetProperties();` – WiiMaxx Aug 26 '13 at 12:33
  • @WiiMaxx: You _could_, but if a possible compile-time usage exists, then that should likely be preferred. It will be faster. It will be flexible for non-assignable properties (such as my second case with only a `getter`), it can be made virtual and let _derived_ classes properly handle their copies, it can handle _deep_ copies where applicable (whereas a typical reflection one would probably only be _shallow_ copies), and you can ignore properties/fields that you do _not_ want to copy (such as the `BackingInstance` in my last example). I don't think reflection is a good solution for this case. – Chris Sinclair Aug 26 '13 at 12:37
2

It's not possible to assign this in a class. That would basically be telling an object that it is not itself. The closest you can get to what you've written is the Factory Method Pattern:

public class CoolClass {
    public string MyProperty { get; private set; }
    public string MyOtherProperty { get; private set; }

    private CoolClass(string myProperty, string myOtherProperty) {
        MyProperty = myProperty;
        MyOtherProperty = myOtherProperty;
    }

    public static CoolClass CreateNew(string myProperty, string myOtherProperty) {
        return new CoolClass(myProperty, myOtherProperty);
    }
}

You can then use this as follows:

var coolClass = CoolClass.CreateNew("blah", "something");
Brian Kintz
  • 1,983
  • 15
  • 19
0
  public class MyClass
  {
   private static MyClass myClass;
   public int Id
   {
    get
   {
    return myClass.Id;
   }
   set
   {
    myClass.Id = value;
   }
  }

  public static MyClass Get()
  {
   if (myClass == null)
    {
     myClass = NewClass.create();
    }
    return myClass;
  }
 }

 static class NewClass
 {
   public static MyClass create()
   { 
    return new MyClass();
   }
 }

I hope this will help.

Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70