0

I need to store data about one article in multiple fields, 3 fields in this example:

article["title"]       = "Article Title";        // string
article["description"] = "Article Description";  // string
article["total_pages"] = 15;                     // int

Then I need to be able to use the stored data:

int pages = article["total_pages"]; // cast not required

MessageBox.Show(pages.ToString());    
MessageBox.Show(article["title"]);

Question:

What would be the proper way of storing data like this?

I could simply create variables like string article_title, string article_description, int article_total_pages but if the article actually has more than 10 fields, then the code gets quite messy.

Edit:

It's fine if all fields can be defined somewhere in one place. What I need is to be able to have a clean code when I use these data in the program.

E.g. Article["Title"] or Article.Title (very clean usage)

1
  • Why not create a Article class? And maybe some sub-classes? Commented Feb 1, 2016 at 12:15

4 Answers 4

2

create a class for that case

class Article
{
    public string Title {get;set;}
    public string Description { get; set; }
    public int Pages { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You could try a dictionary with string keys and dynamic values:

var article = new Dictionary<string, dynamic>();

article["title"] = "Article Title";
article["description"] = "Article Description";
article["total_pages"] = 15;

int pages = article["total_pages"];
string title = article["title"];

You can also achieve a similar effect with an ExpandoObject:

dynamic article = new ExpandoObject();
article.Title = "Article Title";
article.Description = "Article Description";
article.TotalPages = 15;

int pages = article.TotalPages;
string title = article.Title;

2 Comments

and how do you manage a list of articles? with a list of dictionaries? that question is about avoiding messy code
@fubo "I need to store data about one article"
1

You could use a dictionary with object as type. Then wrap it and to avoid casting it we use generics when fetching it:

class ObjectStore
{
    private readonly Dictionary<string, object> _objects = new Dictionary<string, object>();

    public void Add(string key, object obj)
    {
        _objects[key] = obj;
    }

    public object Get<T>(string key)
    {
        object obj;
        if(_objects.TryGetValue(key, out obj))
        {
            return (T)obj;
        }

        return default(T);
    }

    public object this[string key]
    {
        get { return _objects[key]; }
        set { _objects[key] = value; }
    }
}

Usage:

ObjectStore os = new ObjectStore();
os["int"] = 4;
os.Add["string"] = "hello";

// With generic method (no cast)
int num = os.Get<int>("int");
string str = os.Get<string>("string");

// Or...
int num = (int)os["int"];
string str = (string)os["string"];

1 Comment

how do you iterate this?
0

Ideally you store like information in a class, grouping it together logically to represent an object. For example, an Article class.

public class Article
{
    public string Title { get;set; }
    public int TotalPages { get; set; }
    public string Description { get; set; }
}

Then you can have an IList<Article> or IDictionary<Article> as needed. Either way you could use LINQ to filter or an select articles that match a desired criteria.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.