1

I can add rows of data to a datatable.

DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Id");
dt.Columns.Add("Title");
dt.Columns.Add("Description");

DataRow r = dt.NewRow();
r["Id"] = 1;
r["Title"] = "Article Title";
r["Description"] = "Article Description";
dt.Rows.Add(r);

Then output the necessary cell value.

MessageBox.Show(dt.Rows[0]["Title"].ToString());

So it's a kind of array ( that contains rows ) of arrays ( each one having 3 textual keys and 3 textual values ).


Question:

How can I use Collection variables to achieve this without a datatable?
Or maybe you could suggest any other type of data which is more appropriate.

1 Answer 1

3

You can create a class to represent your object like this:

public class MyData //You can name this whatever makes sense in your case
{
    public int Id {get;set;}
    public string Title {get;set;}
    public string Description {get;set;}
}

And then you can create a List<MyData> like this:

List<MyData> list = new List<MyData>();

list.Add(
    new MyData
    {
        Id = 1,
        Title = "Article Title",
        Description = "Article Description"
    });

And access it like this:

MessageBox.Show(list[0].Title);
Sign up to request clarification or add additional context in comments.

Comments

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.