Ask a questionAsk a question
 

Proposed AnswerGeneric class

  • Saturday, November 07, 2009 11:32 AMjonanini Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Guys,
    I'd like to know how to put my objects "Person" in an array considering that Person is an instance of a generic class. when i try i get this error " Object reference not set to an instance of an object".
    This my class:


    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Linq;

    using

     

    System.Text;

    namespace

     

    genClassMatrix

    {

     

    class Program

    {

     

    public class Matrix<T>

    {

    T[,] Arr;

     

    int Size;

     

     

    public Matrix(int rows, int cols)

    {

    Arr =

    new T[rows, cols];

    Size = Arr.Length;

    }

     

    public void Clear()

    {

     

    for (int i = 0; i <3; i++)

    {

     

    for (int j = 0; j <3; j++)

    {

    Arr[i, j] =

    default(T);

    }

    }

    }

     

    public void SetItem(int row, int col, T Item)

    {

     

    for (int i = 0; i < Size; i++)

    {

     

    for (int j = 0; j < Size; j++)

    {

     

    if ((i == row) && (j == col))

    {

     

    if (Arr[row, col].Equals(default(T)))

    {

     

    Arr[row, col] = Item;

    }

    }

    }

    }

    }

     

    public T GetItem(int row, int col)

    {

     

    for (int i = 0; i < Size; i++)

    {

     

    for (int j = 0; j < Size; j++)

    {

     

    if ((i==row)&&(j==col))

    {

     

    if (Arr[i, j].Equals(Arr[row, col]))

    {

     

    Console.WriteLine(Arr[i, j].ToString());

    }

    }

    }

    }

     

    return default(T);

    }

     

    public bool Exist(T Item)

    {

     

     

    for (int i = 0; i < 2; i++)

    {

     

    for (int j =2; j >=0; j--)

    {

     

    if (Equals(Arr[i, j],(Item)))

     

    return true;

     

    }

    }

     

    return false;

    }

    }

     

    class Person

    {

     

    public int id;

     

    public string Name;

    }

     

    static void Main(string[] args)

    {

     

    Matrix<int> MatInt = new Matrix<int>(3,3);

     

    Matrix<bool> MatBool = new Matrix<bool>(3,3);

     

    Matrix<Person> MatPersons = new Matrix<Person>(3,3);

     

    Person p1 = new Person();

     

    Person p2 = new Person();

     

    Person p3 = new Person();

     

    //**************

    p1.id = 345;

    p1.Name =

    "Patrick";

    p2.id = 677;

    p2.Name =

    "jonathan";

    p3.id = 5555;

    p3.Name =

    "allan";

     

    //**************

    MatPersons.SetItem(1, 1, p1);

    MatPersons.SetItem(2, 2, p2);

    MatPersons.SetItem(0, 0, p3);

     

    //**************

     

    //Console.WriteLine(MatPersons.GetItem(1,2));

     

    //**************

     

    /*MatInt.SetItem(0, 0, 25);

    MatInt.SetItem(1, 1, 15);

    MatInt.SetItem(2, 2, 35);

    MatInt.GetItem(0, 0);

    MatInt.GetItem(2, 2);

    MatInt.GetItem(1, 1);

    Console.WriteLine();

    if (MatInt.Exist(13))

    {

    Console.WriteLine("True.");

    }

    else

    {

    Console.WriteLine("False.");

    }

    Console.WriteLine();

    MatInt.Clear();

    MatInt.GetItem(0, 0);

    MatInt.GetItem(1, 1);

    MatInt.GetItem(2, 2);

    Console.WriteLine();

    //**************

    MatBool.SetItem(0, 1, true);

    MatBool.SetItem(1, 1, false);

    MatBool.SetItem(2, 2, true);

    MatBool.SetItem(0, 2, true);

    MatBool.GetItem(0, 0);

    MatBool.GetItem(1, 1);

    MatBool.GetItem(2, 1);

    MatBool.GetItem(0, 2);

    Console.WriteLine();

    if (MatBool.Exist(true))

    {

    Console.WriteLine("True.");

    }

    else

    {

    Console.WriteLine("False.");

    }

    Console.WriteLine();

    MatBool.Clear();

    if (MatBool.Exist(true))

    {

    Console.WriteLine("True.");

    }

    else

    {

    Console.WriteLine("False.");

    }

    Console.WriteLine();

    MatBool.GetItem(0, 0);

    MatBool.GetItem(1, 1);

    MatBool.GetItem(2, 1);

    MatBool.GetItem(0, 2);

    Console.Read();*/

     

    }

     

    }

    }

    Thanks.

All Replies

  • Saturday, November 07, 2009 11:45 AMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi,

    defult(T) does not create instance. And Arr[i,j] is null.

    At your SetItem method change the if statement like below.

     if (Arr[row, col] ==null || Arr[row, col].Equals(default(T)))
                                {
                                    Arr[row, col] = Item;
    
                                }
    
  • Saturday, November 07, 2009 12:08 PMTotalllama Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace genClassMatrix
    {
        class Person
        {
            public int id;
            public string Name;
        }
    
        class Program
        {
            private static List<Person> personList = new List<Person>();
            private static Person[] personArray;
    
            static void Main(string[] args)
            {
                //Create objects
                Person p1 = new Person();
                Person p2 = new Person();
                Person p3 = new Person();
                //Init properties
                p1.id = 345;
                p1.Name = "Patrick";
                p2.id = 677;
                p2.Name = "jonathan";
                p3.id = 5555;
                p3.Name = "allan";
                //Add the objects to the list
                personList.Add(p1);
                personList.Add(p2);
                personList.Add(p3);
                personArray = personList.ToArray();
                //Extract info from Array.
                Console.WriteLine("Writing out objects as an Array\n======");
                foreach (Person person in personArray)
                {
                    Console.WriteLine("Person ID: {0} Name: {1}", person.id, person.Name);
                }
                Console.WriteLine("\nWriting out objects as a List\n======");
                foreach (Person person in personList)
                {
                    Console.WriteLine("Person ID: {0} Name: {1}", person.id, person.Name);
                }
                Console.ReadKey();
            }
        }
    }
    
    
    Hi there

    From what I understand.  You want an array containing 'Person' objects?

    If so here is some code which may do away with the need to iterate through many for loops.