Question

I have a WizardInfo class which as several TLists as properties, this then populates as the user goes through the wizard on the last screen I query the Tlists and make them into Lists and private fields I then create Lists of DefaultItems from these lists. This is my own class and as name and Id as its property. He is some code

public class DefaultItem
{
   public int ID {get;set;}
   public string Name {get;set;}
} 

private List<DefaultItem> _defaultList = null;
_defaultList = new List<DefaultItem>();
defaultValue = PopulateDefaultList(_asmgps, defaultList);
private int PopulateDefaultList(
        List<ASGMP> asmgps,
        ref List<DefaultItem> defaultList)
{
   int isdefault = -1;
   foreach (ASGMP asgmp in asgmps)
   {
      if (asgmp.IsChecked)
      {
         if (asgmp.IsDefault)
         {
            isdefault = asgmp.ID;
         }

         DefaultItem defaultItem = new DefaultItem();
         defaultItem.ID = asgmp.ID;
         defaultItem.Name = GetMPTName(asgmp.ID);  
         defaultList.Add(defaultItem);
      }
   }

   return isdefault;
}

private string GetMPTName(int ID)
{
    try
    {
        SGMP sgmp = DataRepository.SGMPProvider.GetByASGMPID(ID)
        if (serviceGroupMailPresentation != null)
        {
            MPT mpt DataRepository.MPTProvider.GetByMPTID(SGMP.MPTID);
            if (mailPresentationType != null)
            {
                return mpt.Name;
            }
        }

        return string.Empty;
    }
    catch (Exception ex)
    {
        WindowsEventLog.Write(ex);
        throw;
    }
}

The problem i am having is when i remove a item from the defaultList it affects asgmp. I have found the answer. When I get the mpt name I get asgmp from the database this is where Codesmith does a strange thing and connects the usage of the List and the DefaultList. By querying the original List instead of going to the database it now works fine.

Was it helpful?

Solution 3

I found out that this is because of ntiers instead of using the database the to get the ID I should of used the in List of T in

OTHER TIPS

It is being removed because List<T> is derived from object, and is a Reference type. Reference types are passed by reference, i.e. when you pass your list, you are passing a pointer to its location in memory. So any changed you make on the copied reference, will also be reflected on the original object.

In order to make a copy you can change this like: defaultValue = PopulateDefaultList(_asmgps, defaultList);

to this:

defaultValue = PopulateDefaultList(_asmgps.ToList(), defaultList);

This will enumerate the collection as IEnumerable<T> and return is as a list. This will effectivlly create a copy.

erm, instead of PopulateDefaultList why not just do,

var defaultList = asgmps
        .Where(asgmp => asgmp.IsChecked)
        .Select(asgmp => new
            {
                IsDefault = asgmp.IsDefault,
                Item =  new DefaultItem
                    {
                        ID = asgmp.ID,
                        Name = GetMPTName(asgmp.ID)
                    }
            }).ToList();

of course, naming a collection defaultList that contains non-defaults seems counter intuitive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top