locked
I need to do a foreach item on a class RRS feed

  • Question

  • Hi I need to iterate and find an item that I have in a class. I read that I need to use the Ienumerator or I enumerable interface but I don't really understand how to implement it in my existing clas.

    si basiccaly the class  is a list of country, city, state. and other properties. I would like to do a foreach loop to iterate inside.

    here is the class, I wont include all properties inside because too long.

    public class MainSettings : RouteSettings 
        {
            Map _mapView = new Map();
            private string _Country;
            private string _City;
            private string _State;
          
            public MainSettings()
            {
    
            }
    
            public string Country
            {
                get { return _Country; }
                set { _Country = value; }
            }
    
            public string City
            {
                get { return _City; }
                set { _City = value; }
            }
    
            public string State
            {
                get { return _State; }
                set { _State = value; }
            }
    
               }

    Here is how I populate the class, I would like to do a foreach item on xx  just after the following code. I need to search if an item is equal to null.

    MainSettings xx = await _Settings.ReadTheSettingsFileXML<MainSettings>("mySettings.xml", ApplicationData.Current.LocalFolder);
                if (xx == null)
                {
                    await CreateTheFile();
                    xx = await _Settings.ReadTheSettingsFileXML<MainSettings>("mySettings.xml", ApplicationData.Current.LocalFolder);
                }
    Thank you

    Wednesday, October 2, 2013 10:12 PM

Answers

  • Your class appears to be a single object and not a collection. You can only do foreach on a collection.

    You can create a List<MainSettings> to contain a list of all of the settings and then do foreach through that. Depending on how these will be used other collection classes may be better.

    --Rob

    Thursday, October 3, 2013 1:45 AM
    Moderator

All replies

  • Your class appears to be a single object and not a collection. You can only do foreach on a collection.

    You can create a List<MainSettings> to contain a list of all of the settings and then do foreach through that. Depending on how these will be used other collection classes may be better.

    --Rob

    Thursday, October 3, 2013 1:45 AM
    Moderator
  • This is what I did, but it looks messy, is there a way to make the class a list class directly from the class ?

    List<MainSettings> lMainsettings = new List<MainSettings>(); lMainsettings.Add(xx); foreach (var item in lMainsettings) { if (item.Country==null) { xx.Country = "Canada"; }else Country.PlaceholderText = item.Country; await _Settings.CreateTheSettingsFile("mySettings.xml", xx); Country.PlaceholderText = item.Country; }

    Saturday, October 5, 2013 9:58 AM