Forum Cross Platform with Xamarin

Problem with using Add when trying to add a class to a list

Hi,

I've had a problem with a simple bit of code. When a button is clicked an activity should be added to a list of activities. The code is below, I don't understand why it does not add it when it is so simple, i'm probably missing something very obvious?? Thanks in advance

`namespace serialiser
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}

    public void Serialiser(object sender, EventArgs args)
    {
        SelfCare selfCare = new SelfCare();

        // add activities
        var myActivity1 = new Activity()
        {
            Category = "Fitness",
            ActivityDescription = "Boxing class",
        };

        selfCare.Activities.Add(myActivity1);
    }

    public class SelfCare
    {
        public List<Activity> Activities { get; set; }

        public SelfCare()
        {
            Activities = new List<Activity>();
        }
    }

    public class Activity
    {
        public String ActivityDescription { get; set; }
        public String Category { get; set; }
    }
}

}
`

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    What makes you think its not adding?
    If you're waiting for the UI to update: That's not going to happen with that code.
    But that doens't mean the List isn't getting the additional value.

  • padget11padget11 Member ✭✭

    Hi @ClintStLaurent thanks for your reply. When debugging and looking at selfCare.Activities it says null but myActivity1 exists

  • padget11padget11 Member ✭✭

    here is a picture of my variables on the last line

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    You're at the end of the Serializer method.
    Garbage collection & disposers and & destructors are probably already starting to do their jobs.
    selfCare and activity1 are scoped to that method.
    They are probably already in the midst of being destroyed.

  • padget11padget11 Member ✭✭

    I have added some extra lines to the end by it still says null, surely they aren't already being destroyed there?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    They are done being used, referenced, access etc. As far as the system is concerned they are free for disposal.

    Besides - what is the point of these as far as your app is concerned? They die after the method is done. They don't have a purpose in life. You realize that, right?

    Maybe fix up the code so they have a purpose and reference elsewhere - use them like you would in a real program. Then they won't be going out of scope.

  • padget11padget11 Member ✭✭

    I am trying to serialise to xml using but when i get to the xmlserializer I get an error saying derived classes must provide an implementation.

                string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                string fileFullPath = Path.Combine(appDataPath, "selfCareOptions.xml");
                var doc = new XDocument();
                using (var writer = doc.CreateWriter())
                {
                    var serializer = new XmlSerializer(typeof(SelfCare));
    
                    serializer.Serialize(writer, selfCare);
                }
    
                doc.Save(fileFullPath);
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Amongst the many problems in your code...
    Your classes aren't marked as [Serializable]

    There are literally hundreds of tutorials on the 'net for how to serialize in C#. I'd urge you to work some from start to end and learn the material before you try to do it in your own code.

Sign In or Register to comment.