0

I have a problem here with the Var "item", i call Method MyItems from a xml loop, and choose the class from Dictionary by key, for some reason the amount is correct but they are all from the same type/values/icons.....

How could i reset , clear, null or whatever "item" after it was added to _list.Add(item) .... New to c#......

*i hope my post code/format is now correct :p

public class ISwitch : Form1
{
    private static Dictionary<int, Base> _objectsToList;
    private static bool _dicSet = false;

    // Build Dictionary 
    static ISwitch()
    {
        Debug.Write("-----------------------------------------------------\n");
        Debug.Write("dic created\n");

        _objectsToList = new Dictionary<int, SenseItemBase>
                             {
                                 {1, new Control()},
                                 {2, new Control2()},
                                 {3, new MoreItems()},
                                 {4, new MoreItems2()}
                             };
        _dicSet = true;
    }

    public static void MyItems(int id, int funcid, string _index, string _text, string icongfx, string property, string type, string _function, string _itemtype)
    {
        if (!_dicSet) return;
        Debug.Write("-----------------------------------------------------\n");
        Debug.Write("ID : " + id + "\n");
        Debug.Write("funcid : " + funcid + "\n");
        Debug.Write("Case : " + _index + "\n");
        Debug.Write("Property : " + property + "\n");
        Debug.Write("Type : " + type + "\n");
        Debug.Write("Function : " + _function + "\n");
        Debug.Write("Icon : " + icongfx + "\n");
        Debug.Write("Text : " + _text + "\n");
        Debug.Write("Switch : " + _itemtype + "\n");
        XFItemStyle Style;

        switch (id)
        {
            case 1:

                if (_objectsToList.ContainsKey(funcid))
                {
                    var item = _objectsToList[funcid];

                    Debug.Write("-----------------------------------------------------\n");
                    Debug.Write("- Item-\n");
                    Debug.Write("_objectsToList : " + item + "\n");
                    Debug.Write("ID : " + id + "\n");
                    Debug.Write("funcid : " + funcid + "\n");
                    item.Style = IStyle.MyStyle(_index, "HelveticaNeue", "HelveticaNeue", out Style);
                    item.MainText = _text;
                    item.myIcon = Path + "\\" + icongfx;

                    _list.Add(item);

                }

                return;

            case 2:
                if (_objectsToList.ContainsKey(funcid))
                {

                    var item = _objectsToList[funcid];

                    Debug.Write("-----------------------------------------------------\n");
                    Debug.Write("- Item action-\n");
                    Debug.Write("_objectsToList : " + item + "\n");
                    Debug.Write("ID : " + id + "\n");
                    item.Style = IStyle.MyStyle(_index, "HelveticaNeue", "HelveticaNeue", out Style);
                    item.MainText = _text;
                    item.SecondaryText = _text;
                    item.myIcon = Path + "\\" + icongfx;

                    _list.Add(item);
                    item = null;
                }

                return;
        }
    }
}

thank you

manga
  • 105
  • 1
  • 8
  • 2
    It's very difficult to see what's going on here. It would really help if you could write a short but *complete* console app (desktop - the behaviour here doesn't change for CF) demonstrating the problem. – Jon Skeet Mar 22 '12 at 11:12
  • fixed it, i had to set _objectsToList = new Dictionary _objectsToList.add.... into the MyItems Method runs now :) – manga Mar 22 '12 at 12:45
  • @manga please add your own solution as an answer and accept it. It feels a little silly to do, but this way someone with a similar problem can find your question and the answer and solve their issue. – Bazzz Mar 22 '12 at 14:49

1 Answers1

0

How could i reset , clear, null or whatever "item" after it was added to _list.Add(item) .... New to c#......

If I understand your question correctly, you want to remove item from _objectsToList after adding it to _list. This can be done with:

_objectToList.Remove(funcid);  

For more information about working with this method, or the Dictionary collection in general, reference: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

LiquidAsh
  • 271
  • 3
  • 4