4

I want to create a c# data structure that will have a variable number of columns, and a variable number of rows. I will insert values into the 'cells'. I want to be able to index both the columns and the rows using strings, not integers, so that I can address these cells as follows: infotable("pete","monday")=3, or infotable("mike","friday")++. When I encounter either a column name or row name that does not yet exist then I will add it to the structure.

I have pondered using nested collections, but then I will not have a guarantee that each nested collection will contain the same keys as all the other nested collections (unless I manage things manually). I have pondered using DataTables, but then I can't index rows using strings.

What is the neatest way to address my requirement?

Thanks.

0

3 Answers 3

4

Two solutions come to mind:

  1. Use a database table, with a two-part string key and a value field. This is the most scalable solution, but also the 'heaviest'.

  2. Create a simple StringPair structure that takes the two strings (giving the class and the members meaningful names for your application), implement equality and hash code generation (can probably just XOR the hash codes from the two strings), then use a simple Dictionary keyed on that StringPair.

Sign up to request clarification or add additional context in comments.

3 Comments

For the second option Tuple<string, string> works well as the dictionary key.
@Rick, Tuple can work, but I would recommend creating a custom type, as that allows you to give meaningful names to the keys.
If a wrapper type is used that implements a friendly two-parameter indexer, as implied by the OP, then only about three lines of implementation code will refer to the Tuple.
1

What if you extend DataTable and add your own functionality to "index" the rows by string? You could use the method(s) as a wrapper for something like DataTable.Select

Comments

1

the following Sample Code will do what you want :

public class InfoTable
    {
        Dictionary<string, int> _collection;
        public InfoTable()
        {
            _collection = new Dictionary<string,int>();
        }

        public int this[string col,string row] {

            get
            {
                string colrow = col+"_"+row;
                if (_collection.ContainsKey(colrow))
                    return _collection[colrow];
                _collection.Add(colrow, 0);
                return 0;
            }
            set
            {
                string colrow = col + "_" + row;
                if (_collection.ContainsKey(colrow))
                    _collection[colrow] = value;
                else
                    _collection.Add(colrow, value);
            }
        }
    }

and here is how you use it:

InfoTable it = new InfoTable();
it["mike", "friday"] = 1;
it["mike", "friday"]++;
int val = it["mike", "friday"];

1 Comment

This only gives an unambiguous key if you don't allow "_" in the input string. Otherwise "some_bad","data" is the same as "some","bad_data".

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.