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 .

6
  • try X x1=new X(){y = new List<Y>()}; Commented Aug 14, 2013 at 6:49
  • 1
    Please define "Its Failing". Are you getting any object null reference error? If so, in class X constructor, create a new list like y = new List<Y>(). Commented Aug 14, 2013 at 6:50
  • When you do X x1=new X(); then your x.Y is actually null, so do x1.y = new List<Y>(); Commented Aug 14, 2013 at 6:51
  • Its failing because class X list of Y's is not initialized. Commented Aug 14, 2013 at 6:51
  • You defined the List<Y> as field, not as property. stackoverflow.com/questions/295104/… Commented Aug 14, 2013 at 6:57

3 Answers 3

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>();
}
Sign up to request clarification or add additional context in comments.

Comments

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; }
}

5 Comments

x1.y = new List<Y>(); will initialize
@ 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.
have you added x1.y = new List<Y>() line before that?
@Damith yes i have added. For your info Y[0] and y[1] belongs to array of object of type Y only.
I would not create a read/write property of type List< >, i would use a IEnumerable< > instead for read/write properties.
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);

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.