1

In school, for a C# course, they gave us a very mystical assignment. There is the following code in Main() and we have to write the class Group.

Group group = new Group(3);
group["wholenumber"] = 123;
group["decimalnumber"] = 456.78;
group["text"] = "Hello world!";

double sum = (double)(int)group["wholenumber"] + (double)group["decimalnumber"];
Console.WriteLine("Sum of numbers is {0}", sum);

Does that make any sense? I thought you cant use strings as index? I would understand if there was variable names as index, so they could pass for finals, but now I don't get it. I tried to use Google and found dictionaries which are like arrays and can use a string as an index, but all values are different - first is int, second double and last is string, so dictionary won't work.

At the moment we are talking about static classes, constants, destructors and indexers

Thanks in advance

8
  • 3
    Dictionary<string,object> will work Commented Apr 23, 2014 at 18:46
  • 1
    BTW, group is a keyword in C#. You should avoid naming your variables group. Use another name instead. Commented Apr 23, 2014 at 18:47
  • msdn.microsoft.com/en-us/library/6x16t2tx.aspx Commented Apr 23, 2014 at 18:47
  • Thanks. Well, it is actually not called group, I just swiftly translated it as group from Finnish. Commented Apr 23, 2014 at 18:49
  • As to the vote to close: there is nothing wrong with a homework question, especially if it is a good question. Commented Apr 23, 2014 at 18:49

3 Answers 3

3

Since you mention that you are currently talking about indexers, this is exactly what you need to implement for your Group class (assuming that the code in main is fixed and you need to make a class work for that code).

According to MSDN: Indexers (C# Programming Guide):

Indexers do not have to be indexed by an integer value; it is up to you how to define the specific look-up mechanism.

So in this case, you could have code like:

public class Group
{
    public object this[string s]
    {
        get
        {
            //return something based on "s"
        }
        set
        {
            //set something based on "s"
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

but all values are different - first is int, second double and last is string, so dictionary wont work.

Actually it will as System.object is a common base class. THis is why all retrievals from the Group have a cast. Not that this Group "class" makes any logical sense - noone would write a separate class for this, everyone I know would jst write:

var group = new Dictionary<string,object> (3);

Note that group is a terrible variable name due to being a keyword.

Comments

1

You could use Dictionary<TKey, TValue> as some guys said, but you could inherit from it, for sample:

public class Group : Dictionary<string, object>
{
}

And use like a dictionary:

Group g = new Group();
g.Add("wholenumber", 123);
g.Add("decimalnumber", 456.78);
g.Add("text", "Hello world!");

the problem to read is the value is an object, and you need to cast it to right type.

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.