1

I need something like

public class  object_t 
{

    public object_t ( string config, object_t default_obj )
    {
         if( /*failed to initialize from config*/ )
         { 
            this = default_obj; // need to copy default object into self
         }
    }
 }

I know this is not correct. How to implement this constructor ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Newbee
  • 1,032
  • 1
  • 11
  • 27
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 28 '12 at 01:07

2 Answers2

3

The most common is probably to use a static factory method:

public class object_t 
{    
    public static object_t CreateObjectT(string config, object_t default_obj)
    {
         object_t theconfiguredobject = new object_t();

         //try to configure it

         if( /*failed to initialize from config*/ )
         { 
             return default_obj.Clone();
         }
         else
         {
             return theconfiguredobject;
         }
    }
}

A better way to do the above would be to create a copy constructor:

public object_t (object_t obj)
    : this()
{
    this.prop1 = obj.prop1;
    this.prop2 = obj.prop2;
    //...
}

and a method that tries to create your object from the config string:

private static bool TryCreateObjectT(string config, out object_t o)
{
    //try to configure the object o
    //if it succeeds, return true; else return false
}

then have your factory method call the TryCreateObjectT first, and if it fails, the copy constructor:

public static object_t CreateObjectT(string config, object_t default_obj)
{
     object_t o;
     return TryCreateObjectT(config, out o) ? o : new object_t(default_obj);
}
lc.
  • 113,939
  • 20
  • 158
  • 187
  • I really need to clone default object because new object may overwrite some fields in the future. Your CreateObjectT just return reference to default object. So any field changes will affect default object. – Newbee Dec 28 '12 at 01:14
  • @Newbee Ok, I added the call to `Clone()` - you can use a copy constructor here instead too. (See also the second/extended suggestion) – lc. Dec 28 '12 at 01:18
2

You should copy each field from the default object to the new one within the constructor:

public class  object_t 
{ 
    int A, B;

    public object_t ( string config, object_t default_obj )
    {
         if( /*failed to initialize from config*/ )
         { 
            this.A = default_obj.A; 
            this.B = default_obj.B; 
            //...
         }
    }
}

However you should remember, if this class' fields are reference types, you'll have to Clone them as well, not just assign a refernce to.

This approach does create a copy of the default object, while if you only need to return the default object itself, you should use the factory approach, mentioned by lc.

horgh
  • 17,918
  • 22
  • 68
  • 123
  • 1
    @Newbee however you'll need to copy the values from the default object to the new one somehow. Of course you may use `Reflection` to do this with one `for`-loop, but think for a _dozen_ of times before that. – horgh Dec 28 '12 at 01:19
  • See [C# Using Reflection to copy base class properties](http://stackoverflow.com/questions/1198886/c-sharp-using-reflection-to-copy-base-class-properties) post on how to do this with Reflection – horgh Dec 28 '12 at 01:21