Another way is for Celda to be an struct. Arrays of value-types like int
or your structs are preinitialized. objects like String and other
classes are reference types and must be created as Jonathan pointed out.

public struct Celda
{
    public int Valor ;
    public Celda(int V) {
        Valor = V;   
    }
    public override string ToString(){
        return "("+Valor+")";
    }   
}

Now your original class Terreno should work as it was...

HIH,

On Thu, 2005-03-03 at 09:07, Jonathan Pryor wrote:
> On Wed, 2005-03-02 at 22:30 +0000, Phillip Neumann wrote:
> > Hello...
> > 
> > I wonder how to work with arrays of objects.
> 
> <snip/>
> 
> > when executing i get:
> > System.NullReferenceException: Object reference not set to an instance 
> > of an object
> > 
> > how shall i create my matrix?
> 
> Your example is equivalent to this:
> 
>       string[] array = new string[10];
>       array[0].Length;        // throws NullReferenceException
> 
> The above will also generate a NullReferenceException.  The reason is
> because the array is separate from the objects it contains; you created
> an array, but never put anything in it.
> 
> The correct approach would be this:
> 
>       // create array
>       string[] array = new string[10];
> 
>       // populate
>       for (int i = 0; i < array.Length; ++i)
>               array[i] = "some string" + i.ToString();
> 
>       // access array
>       int total_length = 0;
>       for (int i = 0; i < array.Length; ++i)
>               total_length += array[i].Length;
> 
> Or for your matrix code in the Terreno constructor:
> 
>       // create matrix
>       _Matrix = new Celda[px,py];
> 
>       // populate matrix
>       for (int i = 0; i < px; ++i)
>               for (int j = 0; j < py; ++j)
>                       _Matrix[i, j] = new Celda ();
> 
>  - Jon
> 
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
> 
> E-mail classificado pelo Identificador de Spam Inteligente Terra.
> Para alterar a categoria classificada, visite
> http://www.terra.com.br/centralunificada/emailprotegido/imail/imail.cgi?+_u=rafael.teixeirabr&_l=1,1109851947.488687.3159.cabue.terra.com.br,3962,Des15,Des15
> 
> Esta mensagem foi verificada pelo E-mail Protegido Terra.
> Scan engine: McAfee VirusScan / Atualizado em 02/03/2005 / Versão: 4.4.00 - 
> Dat 4438
> Proteja o seu e-mail Terra: http://www.emailprotegido.terra.com.br/
-- 
Rafael "Monoman" Teixeira 
Mono Hacker since 16 Jul 2001 - http://www.go-mono.org/
Mono Brasil Founding Member - http://monobrasil.redesolbrasil.org/
English Blog: http://monoblog.blogspot.com/
Brazilian Portuguese Blog: http://monoblog.weblogger.terra.com.br/

_______________________________________________
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to