0

I understand things in here are value types and not referenced so the field _num won't be modified when I just update the list. But my question is how to update the field _num when I modify the list that contains it gets modified?

class Foo
{
    public List<object> mylist;

    private int _num;

    public int num
    {
        get
        {
            return _num;
        }
        set
        {
            this._num = value;
            mylist[0] = value;
        }

    }

    public Foo()
    {
        mylist = new List<object>();
        mylist.Add(_num);
    }
}


class Program
{
    static void Main(string[] args)
    {
        Foo my = new Foo();
        my.num = 12;
        my.mylist[0] = 5;
        Console.WriteLine("" + my.mylist[0] + " " + my.num);    ==> output is "5 12"
        Console.ReadLine();
    }
}

What changes could be done so the list and the field is synced? Like my output should be "5 5" Thanks for the help!

Vikyboss
  • 940
  • 2
  • 11
  • 23
  • 1
    Since we just helped answer a question containing this particular code, this is starting to smell suspiciously like homework. Is it? =) – J. Steen Jul 21 '11 at 14:32
  • 2
    Why do you need to store the same value in two places? – FishBasketGordo Jul 21 '11 at 14:32
  • No. It is part of my work. I am implementing something that requires this functionality. – Vikyboss Jul 21 '11 at 14:34
  • So I can access it with index. – Vikyboss Jul 21 '11 at 14:34
  • 2
    Maybe if you explain the intent of your design, we can help you in a better way. – J. Steen Jul 21 '11 at 14:35
  • I had a feeling you were going to want to go reverse as well :-) – James Michael Hare Jul 21 '11 at 14:37
  • It would help us to know why you want to present the fields as a list... If you could explain that might give us a way to suggest a better design that isn't subject to these limitations. – James Michael Hare Jul 21 '11 at 14:38
  • I asked my intentions over here: http://stackoverflow.com/questions/6768062/pass-a-data-structure-to-a-function-and-access-the-fields-of-the-data-structure-l/6768154#6768154 and http://stackoverflow.com/questions/6767555/how-to-create-an-array-of-custom-type-in-c/6767579#6767579. I got an anser to use dictionary which is preety good. But trying to do the same with a list this time. – Vikyboss Jul 21 '11 at 14:38
  • Because accessing dictionary elements like this Object.property will not return the value. Here property is a Dictionary. – Vikyboss Jul 21 '11 at 14:42
  • Well, if you want to be able to get/modify a property by either the property or an index, you could do something like an indexer in my solution below. It's not elegant, but it would do what you want... You could also implement IEnumerable if you wanted ability to iterate in a foreach instead of a for loop... – James Michael Hare Jul 21 '11 at 14:48

1 Answers1

3

This may or may not be what you want... and I'm still not sure I see the need for modifying the fields by index, but if you really want to do that have you considered an indexer for your type? That is, the indexer would replace your list like so:

class Foo
{
    public int num;
    public string name;
    public bool isIt;

    public object this[int index]
    {
        get
        {
            switch(index)
            {
                case 0:
                    return num;
                case 1:
                    return name;
                case 2:
                    return isIt;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
        set
        {
            switch(index)
            {
                case 0:
                    num = (int) value;
                    break;
                case 1:
                    name = (string) value;
                    break;
                case 2:
                    isIt = (bool) value;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
}

Then you can either say:

var foo = new Foo();
foo.num = 13;  // either works
foo[0] = 13;  // either works
James Michael Hare
  • 37,767
  • 9
  • 73
  • 83
  • 2
    Yeah, it's not the most elegant, but if the user really wants a way to reference a property directly or by index, it does the job. – James Michael Hare Jul 21 '11 at 14:47
  • 1
    Quite so. Eliminates the need of managing two sets of values, too. It's elegant for the constraints given. – J. Steen Jul 21 '11 at 14:48
  • it is just the implementation of this what you did longer, but accessing it many ways is a breeze. Finally a very simple, nice and clean solution. Thank you. Much appreciated! :))) – Vikyboss Jul 21 '11 at 14:49
  • 4
    It works, but it's altogether too clever by half. If I saw this in production code I'd have a conniption. – Justin Morgan - On strike Jul 21 '11 at 14:50
  • @Justin: I don't disagree with the fact it's kinda of a "trick" and would be difficult to maintain as new fields are added/removed/etc. However, if the user has a need for this kind of dual access/update ability, it does the trick. – James Michael Hare Jul 21 '11 at 19:00