tags:

views:

53

answers:

1

The function works well.

If add dynamical value located to other class, and i still want to declare the list0 at Main(). How to ?

Thank you.

using System;
using System.Collections.Generic;
using System.Text;


class Program
{

    public static void Main()
    {
        List<string> list0 = new List<string>();

        list0.Add("A");  // Move to other class.
        list0.Add("B");  // Move to other class.

        string line0 = string.Join("/", list0.ToArray());
        Console.WriteLine(line0);
        Console.ReadLine();
    }
}

======================================

UPDATED.

With your help. My problem soluted. a further more question below.

using System;
using System.Collections.Generic;
using System.Text;


public class Program
{

    public static void Main()
    {

        List<string> list0 = new List<string>();

        list0.Add("A");
        list0.Add("B");

        otherClass oAdd = new otherClass();
        oAdd.AddItem(list0);

        string line0 = string.Join("/", list0.ToArray());
        Console.WriteLine(line0);
        Console.ReadLine();
    }
}

public class otherClass
{
    //error CS0117: 'otherClass' does not contain a definition for 'AddItem'
    // If uncommented and switch to this line show the error above. I don't know why?
    //public static void AddItems(IList<string> list0) 
    public void AddItem(IList<string> list0)
    {
        list0.Add("C");
        list0.Add("D");
    } 

}

I found another problem. thanks.

+2  A: 
public class OtherClass
{
    public OtherClass(IList<string> list0)
    {
        list0.Add("A");
        list0.Add("B");
    }
}

or

public class OtherClass
{
    public static void AddItems(IList<string> list0)
    {
        list0.Add("A");
        list0.Add("B");
    }
}

This works because a List is a reference type, so you are passing the reference and then adding items to the List class that lives on the heap at that reference.

Yuriy Faktorovich
Hi Yuriy, I followed your guide. I found only one method works. could you please inspect my code above. thanks.
Nano HE
@Nano HE check out my comment to your question.
Yuriy Faktorovich
Thank you, I got it.
Nano HE