I'm interested in making an .ini file parser and I want it to have an internal list that can hold it's individual entries. The catch is I want to make one list to hold all the values. That means strings, boolean values and int types.

I have a class to hold key-value pairs that is a template.

public class KVPair<T>
    private string key;
    private T      value;

I'm interested to know if there's any way to make a list something like

List<KVPair> something;

without having to specify a certain type for the inner object, or a work-around.

Recommended Answers

All 3 Replies

Generally you could just do:

List<object> anything = new List<object>();

The issue is you'd need to know what the values are, so that you could cast them as specific types when you need to:

string name = (string)anything[0];

Hope that helps.

I don't want to sound pompous, but I was hoping for something more elegant, since casting to a key-value pair outside the file handler class defeats the whole purpose of encapsulating the data into a class.
For some reason this

List<KVPair<object>> something;

doesn't work. In the build errors it sais:
"Cannot convert type 'KVPair<object>' to 'KVPair<string>'"
when I try to add a KVPair<string>.

The user of your class will need to know what type is being returned, or else how are they going to use it? They will have to know to expect a string, boolean, int, etc. Sure, they could assign it to var, but then what good is it? Should you add something to it, or parse it as a string or test the true/false value?

You are going to have to cast (easy way) or use a struct to hold the values and create a 'union' type (like the one in C). Here is an article about creating a union type for C#.

As for your error, a KVPair<object> is a different type than a KVPair<string>, with no method defined to cast it, so you get an error. Don't be confused by the <object> part, remember it's a KVPair<T>.

You also need to consider that ini files have sections, and it's perfectly fine to have the same key in multiple sections. For example



So a simple dictionary of key/values won't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.