0

Let me explain I have a class X

public class X
{
  private List<Y> y;
}

I have another class Y

public class Y
{
  int a ;
  Private List<Z> z;
}

Now i want to mannually assign some values to class Y object whitch is an array of obejcts of size 2. Now i have added the values. How can i add this to the object of class X, in which a propery of type List of Class Y exists. Hope am able to explain it correctly.

i have

Y [] y1= new Y[2];
y1[0] = new Y();
y1[0].a=1;
y1[1]=new Y();
y1[0].a=2;

How can i assign y1[0] and y1[1] to object of X.

X x1=new X();
x1.y.add(y1[0]);
x1.y.add(y1[1]);

Its failing...

Please help me .

Sachin
  • 40,216
  • 7
  • 90
  • 102
Anjana
  • 1,447
  • 5
  • 23
  • 33

3 Answers3

2

you can use AddRange

x1.y.AddRange(y1);
x1.y.AddRange(y1.GetRange(0,2)); // Adds first two elements

Although if its only a couple elements, its probably just as efficient to add them on their own

Since your list is private you may wish to add a method to do this

public void AddRangeToList(List<Y> items)
{
     y.AddRange(items);
}
x.AddRangeToList(y1);
x.AddRangeToList(y1.GetRange(0,2));

It appears you never initialise your arrays I would make constructors to do this

public X()
{
    y = new List<Y>();
}

public Y()
{
     z = new List<Z>();
}
Sayse
  • 42,633
  • 14
  • 77
  • 146
1

if you want to access properties of the class you need to make them public

X x1=new X();
x1.y = new List<Y>(); // need create instant of y before add items 
x1.y.Add(y1[0]);
x1.y.Add(y1[1]);

I would change the classes as below

public class X
{
    public List<Y> y { get; set; }
}


public class Y
{
    public int a;
    public List<Z> z { get; set; }
}
Damith
  • 62,401
  • 13
  • 102
  • 153
  • `x1.y = new List();` will initialize – Damith Aug 14 '13 at 07:08
  • @ Damith Eventhough i have updated as you suggested. Its throwing the error "Object reference not set to an instance of an object." This is thrown when we try to add ie, x1.y.add(y[0]); where y forms an array of Objects of Type y. – Anjana Aug 14 '13 at 07:21
  • have you added `x1.y = new List()` line before that? – Damith Aug 14 '13 at 07:26
  • @Damith yes i have added. For your info Y[0] and y[1] belongs to array of object of type Y only. – Anjana Aug 14 '13 at 07:47
  • I would not create a read/write property of type List< >, i would use a IEnumerable< > instead for read/write properties. – Jeroen van Langen Aug 14 '13 at 07:56
0

You should make your list property public instead of private.

Accessibility Levels (C# Reference) http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

-

I would create a constructor that creates an instance of the List< Y>.

classes:

    public class X
    {
        public X()
        {
            Y = new List<Y>();
        }

        public List<Y> Y { get; private set; }
    }

    public class Y
    {
        public Y()
        {
            Z = new List<Z>();
        }

        public int A { get; set; }
        public List<Z> Z { get; private set; }
    }

    public class Z
    {
        public int V { get; set; }
    }

Usage:

        Y[] y1 = new Y[2];
        y1[0] = new Y();
        y1[0].A = 1;
        y1[1] = new Y();
        y1[1].A = 2;

        X x1 = new X();
        x1.Y.Add(y1[0]);
        x1.Y.Add(y1[1]);

        // or
        X x2 = new X();
        x2.Y.AddRange(y1);

        // or
        X x3 = new X();
        foreach (Y y in y1)
            x3.Y.Add(y);
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • If you specified the error, i could tell what went wrong. I updated it, It's still the same code, tell me where it differs/went wrong? note: i would not name my properties the same as classes. – Jeroen van Langen Aug 14 '13 at 08:03