Click here to Skip to main content
15,909,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.I'm having this problem between two classes. In the 1st class, there is a value as a list.I would like to print this list in the 2nd class ta with console.writeline.In this case, how can I call the list value in another class and print list.

Definition.cs
C#
public List<string> ListValue=new List<string> ();

Usebility.cs (call class)

What I have tried:

Console.WriteLine( ListValue);
Posted
Updated 25-Feb-19 21:08pm
v2

Assuming that ListValue is accesible...
You need to loop through the collection of items in a List<string>, for example:
C#
foreach(string s in ListValue)
    Console.WriteLine(s);
 
Share this answer
 
v2
Here is one way:
ing System;
using System.Collections.Generic;

namespace classSample
{
    class Program
    {
        static void Main(string[] args)
        {
            ClassTwo cTwo = new ClassTwo();
            List<string> theList = cTwo.GotThem;
            foreach (string item in theList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }
    }
    public class ClassOne
    {
        public List<string> GetMe = new List<string>();

        public ClassOne()
        {
            GetMe.Add("one");
            GetMe.Add("two");
            GetMe.Add("three");
        }

    }
    public class ClassTwo
    {
        public List<string> GotThem { get; set; }

        public ClassTwo()
        {
            ClassOne cOne = new ClassOne();
            GotThem = cOne.GetMe;
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900