0

I have the below code:

public Dictionary<int, Ticket> GetNewTickets()
    {
        Dictionary<int, Ticket> output = new Dictionary<int, Ticket>();

        foreach (KeyValuePair<int, Ticket> item in ticketStore)
        {
            if (!ticketStoreNew.ContainsKey(item.Key))
            {
                output.Add(item.Key, item.Value);
            }
        }

        ticketStoreNew = ticketStore;

        return output;
    }`

It takes a dictionary, ticketStore, checks to see if it has any new elements not in ticketStoreNew and puts them in the output dictionary. Then, ticketStoreNew is set to ticketStore until ticketStore is updated with another method and this method is ran again.

However, when I include the line ticketStoreNew = ticketStore, the program returns an empty dictionary. It looks like the method is not executing sequentially and this is running prior to the for loop.

I really just need to return any new items added to the ticketStore dictionary.

EDIT Below is the code for getting ticketStore:

public void UpdateTickets(string inputXml)
{
    // If no new tickets exit
    if (inputXml.Trim() == "") { return; }
    //xmlString = inputXml;

    // Load XML into an enumerable 
    XElement xelement = XElement.Parse(inputXml);
    IEnumerable<XElement> xml = xelement.Elements();

    foreach (var item in xml)
    {
        if (item.Name == "incident")
        {
            int id;

            // If ID can be converted to INT
            if (Int32.TryParse(item.Element("id").Value, out id))
            {
                // If ticket is not already in store create ticket and populate data
                if (!ticketStore.ContainsKey(id))
                {
                    Ticket ticket = new Ticket();
                    ticket.id = id;
                    ticket.number = Int32.Parse(item.Element("number").Value);
                    ticket.title = item.Element("name").Value;
                    ticket.description = item.Element("description").Value;

                    ticketStore.Add(id, ticket);
                }
            }
        }
    }
}

}

The tickets are all based on getting XML from the Samanage API.

J. Galus
  • 1
  • 3
  • Please include method that updates ticketStore as this would be relevant here. – Andrey Shchekin Dec 16 '16 at 03:11
  • I've edited the original post to include the update method. The ticket store and it's ticket items are all generated from XML pulled from an API call. – J. Galus Dec 16 '16 at 03:25
  • mike z: I tried replacing `ticketStoreNew = ticketStore` with your code, but it's doing the same thing. It returns a blank dictionary, but when the line is commented out, it returns some items. – J. Galus Dec 16 '16 at 03:29

2 Answers2

0

If another method updates ticketStore then the assignment is the problem. It doesn't copy the contents of ticketStore to ticketStoreNew it sets the reference ticketStoreNew to point to the same instance as ticketStore. Thus they are the same object and always have the same contents. Try creating a new Dictionary to copy the items:

ticketStoreNew = new Dictionary<int, Ticket>(ticketStore);
Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • I tried replacing the line `ticketStoreNew = ticketStore` with your code, but it's having the same affect. It returns an empty dictionary, but when the line is commented out, I get some results. – J. Galus Dec 16 '16 at 03:33
  • @J.Galus I'm not sure how ticketStoreNew is initialized, but commenting out that line would probably just cause it to return all the items in ticketStore. What kind of application is this? Can these methods run on multiple threads? – Mike Zboray Dec 16 '16 at 03:35
  • ticketStore is a class property (I hope I'm using the correct term). The application get's tickets in the form of XML from a website, then displays a notification if there's a new ticket. – J. Galus Dec 16 '16 at 03:38
  • @J.Galus I mean is it a console or WPF or ASP.NET or some other kind of application? Second, and this is sort of a wild guess, are you running GetNewTickets in the debugger, e.g. in the VS watch window? Because that is going to cause side-effects in the program the way it's been structured. – Mike Zboray Dec 16 '16 at 03:41
  • It's WinForm. Also, it is in debugger mode, but I don't think it's in a watch window. Sorry if I'm not being clear.. I'm still trying to figure this all out. – J. Galus Dec 16 '16 at 03:46
0

Try this code:

    private Dictionary<int, Ticket> ticketStoreNew = 
        new Dictionary<int, Ticket>(); // add this line
    public Dictionary<int, Ticket> GetNewTickets()
    {
        Dictionary<int, Ticket> output = new Dictionary<int, Ticket>();

        foreach (KeyValuePair<int, Ticket> item in ticketStore)
        {
            if (!ticketStoreNew.ContainsKey(item.Key))
            {
                output.Add(item.Key, item.Value);
                ticketStoreNew.Add(item.Key, item.Value); // add this line
            }
        }

        //ticketStoreNew = ticketStore; remove this line

        return output;
    }
Shadmehr
  • 278
  • 4
  • 13