C# Array Property

Hi,
I have a created class  and this class i created property  
see my code
public class Test
{

public Test2[] runCode
{

get;
set;
}

}

public class Test2
{
public string name
{
get;
set;
}


}

Open in new window


I need to know how to call this class and assigned to another class
LVL 2
lankapalaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Flabio GatesCommented:
Try something like:
    class Employee
    {
        public string Name
        {
            get;
            set;
        }

        public int Id
        {
            get;
            set;
        }
    }

    class Department
    {
        public Employee[] Employees
        {
            get;
            set;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var d = new Department();
            d.Employees = new Employee[3];
            d.Employees[0] = new Employee() { Name = "John", Id = 1001 };
            d.Employees[1] = new Employee() { Name = "Bob", Id = 1002 };
            d.Employees[2] = new Employee() { Name = "Mary", Id = 1003 };
        }
    }

Open in new window

lankapalaAuthor Commented:
@Flabio Gates

I already check that example
Flabio GatesCommented:
I already check that example
What does this mean? Did it not work? What exactly did you do? Show your code!
Flabio GatesCommented:
What you need to do first before assigning values to your array, is initialize your array.
I've shown you how to do that in line 30 of my example above.
Have you initialized your array?
Chris StanyonWebDevCommented:
You should really be using a Collection and not an Array for public properties, and that collection should be Immutable (you can Add/Remove from the List, but you can't Set the list to something else!)

using System.Collections.ObjectModel;

public class Test
{
	public Test()
	{
		RunCode = new Collection<Test2>();
	}
	
	public Collection<Test2> RunCode
	{
		get;
	}

}

public class Test2
{
	public string Name
	{
		get;
		set;
	}
}

Open in new window

Now you can instantiate a new Test() and simply add new Test2() items to the RunCode collection:

var myTest = new Test();

myTest.RunCode.Add( new Test2 { Name = "Chris" } );
myTest.RunCode.Add( new Test2 { Name = "lankapala" } );

var another = new Test2();
another.Name = "Some Name";
myTest.RunCode.Add( another );

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.