views:

73

answers:

2

This is the class I'm trying to instantiate:

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

namespace Test
{
    public class Posicion
    {
        public int X { get; set; }
        public int Y { get; set; }
    }
}

And here I'm trying to create it:

button1.Tag = new Posicion() { 1, 1 };

I remember I used to be able to do something like this before, how can I instantiate an object by giving it values up front in the single line? Thanks!

+7  A: 

Use the object initializer syntax:

button1.Tag = new Posicion() { X = 1, Y = 1 };

This relies on X and Y having public setters.

Richard Cook
Thanks, I knew I wasn't far off. :D
Serg
+1  A: 
button1.Tag = new Posicion() { X = 1, Y = 1 };
Jackson Pope