cmd.prompt
cmd.prompt

Reputation: 964

Is there a wa to call a method immedietely after object instantiation?

public class Foo
{
    public Foo(){ }

    //One of many properties 
    //set up in the same way
    private String _name;
    public String Name 
    { 
        get { return _name; }
        set {
            _name = value;
            //code that is important to run
            //only after the objects initial creation   
        }
    }

    private int _id;
    public int ID 
    { 
        get { return _id; }
        set {
            _id = value;
            //code that is important to run
            //only after the objects initial creation   
        }
    }

    public void Win()
    {
        //clean up method that wouldn't be needed
        //if I used optional parameters because
        //i would be able to set _name (and all the
        //other private properties directly without
        //using the public Set
    }
}

How do I call a method automatically after this kind of object creation in c#

Foo ko = new Foo() {
    ID = 4,
    Name = "Chair"
};
ko.Win(); // <-- Want this to be called automatically inside the class

Upvotes: 2

Views: 3555

Answers (4)

Alexei Levenkov
Alexei Levenkov

Reputation: 100545

There is no method that automatically called after some random set of properties is set (Which is what initialization is translated to...)

var foo = new Foo { Name = "bar" };

Is actually shortcut to:

var foo = new Foo();
foo.Name = "bar";

When written in second form one would not expect any magical method to be called after foo.Name assignment.

You options:

  • if you have some information that need to be set on property change - just make it a property and write code in set part of it.
  • if you must have particular set of properties configured before object is considered "created" constructor arguments is one reasonable way to enforce it.
  • you can also implement builder pattern that allow you to delay final construction (or use some other factory method that forces setting parameters before final object creation.

Sample of code with builder pattern:

 var foo = new FooBuilder { Name = "bar" }
    .Build();

Upvotes: 2

Andrei
Andrei

Reputation: 5005

Not the most scalable solution but why not try this:

public class Foo
{
    public int ID { get; set; }
    public String Name { get; set; }

public Foo()
{
}

public Foo( int id )
{
// Win()
ID = id;
// Win()
}

Public Foo( string name )
{
// Win()
Name = name;
// Win()
}

public Foo( int id, string name )
{
// Win()
ID = id;
Name = name;
// Win()
}

    public void Win()
    {
        //Do Stuff that would go into the constructor
        //if I wanted to use optional parameters
        //but I don't
    }
}

You can call Win before or after setting the properties.

Upvotes: 0

Xelom
Xelom

Reputation: 1625

Well if you are always setting ID and the Name how about this?

private string _Name;

    public string Name
    {
        get { return _Name; }
        set {
            _Name = value;
            this.Win();
        }
    }

Win function will always called after you set a value on the name or you can do this for ID that's your choice!

Upvotes: 1

iefpw
iefpw

Reputation: 7052

add the Win() to the constructor. Call/put inside the constructor.

public Foo(string value, string value2) {
Value = value;
Value2 = valu2e;

Win();
}

This is the constructor. Set it manually.

Upvotes: 0

Related Questions