acoder
acoder

Reputation: 697

C# collection or array with string keys and values of different types

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)

Upvotes: 0

Views: 5161

Answers (4)

Simon Karlsson
Simon Karlsson

Reputation: 4129

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"];

Upvotes: 1

TVOHM
TVOHM

Reputation: 2742

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;

Upvotes: 2

David Pine
David Pine

Reputation: 24525

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.

Upvotes: 0

fubo
fubo

Reputation: 45947

create a class for that case

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

Upvotes: 2

Related Questions