0

I want to create a data store to allow me to store some data.

The first idea was to create a dictionary which could have one key to the some values, but i have no idea how to store few different vaues, for example i need two values (string and Image). Can someone suggest how to realize that?

How else could I store this information??

2
  • Create a new type, MyType, that holds a string and an image. Then make a Dictionary<TKey, MyType>. Commented Mar 27, 2012 at 5:58
  • Would wrapping a string and image object in another class1, then creating a dictionary <key, class1object> work for you? Commented Mar 27, 2012 at 6:01

5 Answers 5

2

In OOP world usually the data is represented in classes, which define it's structure, behavior and responsibilities. From your example to store string and image you could create class:

public class Article
{
    public string Name { get;set;}
    public byte[] Image { get;set;}
}

and then your dictionary would be like:

Dictionary<string,Article> data;
Sign up to request clarification or add additional context in comments.

3 Comments

can you add example : if i coll data["first element"] i get (for exaple) string "car" and picture of car fro resources?
I didn't really understand your question, but it works like this: string car = data["first"].Name; byte[] carImage = data["first"].Image
how to get using class article that i need?
1

Well there can be several approaches to achieve this.

1) create a class which has string and image as its members. Then store the instance of this class as value.

2) use Tuple<String, Image> as value

Comments

0

Since in c# all objects derive from object, you may use object as the value of dictionary.

Dictionary<string, object> dataStore;

Comments

0
var dic = new Dictionary<string, object>();

With this way you can store any type of object in your dictionary but don't forget to make the right cast.

Comments

0

Do you want to store different data according to one key? For example "key1" ->(Name, Age, Birthday) or do you want to store a different type of data under ache key? For example "key1"->Name , "key2"->Age, "key3"->Birthday? In the first case, you should create a new class for you values and store an instance of the the class under each key. For the second case you can create an dictionary and make the value of the type "object". Then you can store any object data type to the keys.

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.