简体   繁体   中英

C# Method is not executing sequentially

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.

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);

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM