I want to know how to access ListArray 1 and print one of the parameters of the object added to that ListArray
Main Program:
namespace ConsoleApp1 {
class Program
{
static void Main(string[] args)
{
UserDetails ud = new UserDetails();
List<TaskDetails> taskDetails = new List<TaskDetails>();
Console.Write("Please enter User ID: ");
string i = Console.ReadLine();
Console.Write("Please enter username: ");
string s = Console.ReadLine();
ud.SetDetails(i, s);
int cnt = int.Parse(Console.ReadLine());
for (int x=1; x <= cnt; x++)
{
Console.Write("Please enter title for Task: " + x + " ");
string title = Console.ReadLine();
TaskDetails td = new TaskDetails();
td.SetTask(i, title);
taskDetails.Add(td);
}
PrintDetails pd = new PrintDetails();
}
}
}
Task Details
namespace ConsoleApp1
{
class TaskDetails
{
private string UserID { set; get; }
private string TaskTitle { set; get; }
private List<TaskDetails> TaskList = new List<TaskDetails>();
public TaskDetails() { }
public List<TaskDetails> GetTaskList
{
get { return TaskList; }
}
public void SetTask(string userID, string taskTitle)
{
this.UserID = userID;
this.TaskTitle = taskTitle;
}
}
}
this is class I am using to access the ArrayList in class TaskDetails, I want to be able to access the object in the array by index and to one of its parameters
namespace ConsoleApp1
{
class PrintDetails
{
public void print(int i)
{
TaskDetails qw = new TaskDetails();
List<TaskDetails> MyList1 = qw.GetTaskList;
Console.WriteLine(qw.GetTaskList);
}
}
}
So I think the problem you're facing is that when you create a new TaskDetails, you initialise a new version of TaskDetails, which starts off empty. If you feel that a separate class is necessary for the PrintDetails you could have something like the following:
Then in your main function, call it as follows: