Home > Software design >  add instance of class to a list
add instance of class to a list

Time:12-17

I have a class called Email that's stored in a class library which will contain values from textboxes in my UI (Sorry large wall of code):

namespace MessageLibrary
{
    public class Email
    {
        private int _emailID;
        private string _emailSubject;
        private string _emailSender;
        private string _emailContent;

        public int EmailID {get , set}

        public string EmailSubject {get , set}

        public string EmailSender {get , set}

        public string EmailContent {get , set}

I have a method below that adds an instance of that class:

        public void AddEmail()
        {


            Email aEmail = new Email();
            int emailID = Convert.ToInt32(txtID.Text);
            string emailSender = Convert.ToString(txtSender.Text);
            string emailSubject = Convert.ToString(txtSubject.Text);
            string emailContent = Convert.ToString(txtContentClean.Text);
            if (txtContentClean.Text.Length <= 1028 && txtSubject.Text.Length <= 20 && txtID.Text.Length == 9)
            {
                try
                {
                    aEmail.EmailID = emailID;
                    aEmail.EmailSender = emailSender;
                    aEmail.EmailSubject = emailSubject;
                    aEmail.EmailContent = emailContent;
                }
                catch (Exception exp)
                {
                    throw new ArgumentException(exp.Message);
                }
            }
            emails.Add(aEmail);
        }

And I have a class that would contain a list of each instance of Email which is in my MainProgram.Xaml.cs:

        public class EmailList
        {
            private List<Email> _list = new List<Email>();
            public List<Email> Emails => _list;

            public void add(Email newEmail)
            {
                _list.Add(newEmail);
            }
        }

And finally I have a button that would serialize this to JSON in a .txt file:

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            var emailList = new EmailList();
            var obj = JsonConvert.SerializeObject(emailList.Emails, Formatting.Indented);
            File.WriteAllText(@"C:\Users\patri\OneDrive\Desktop\test.txt", obj);
        }

I'm currently getting the error message "the name emails does not exist in the current context" on the last line of my AddEmail() method.

I'm really new with OO and would anyone be able to kindly assist and show me how to get this to correctly add each instance of the class and serialize the values.

CodePudding user response:

It seems you are missing the definition for this variable "emails". It looks like you already define in class EmailList.

You can reference back to "Emails" (typo from "emails") or define var emails = new List().

CodePudding user response:

Define "emails" as a List<Email> field directly in your MainProgram.xaml.cs class and get rid of the superfluous EmailList class:

private readonly List<Email> emails = new List<Email>();

public void AddEmail()
{
    Email aEmail = new Email();
    //...  same code as before
    emails.Add(aEmail);
}

private void btnExport_Click(object sender, RoutedEventArgs e)
{
    var obj = JsonConvert.SerializeObject(emails, Formatting.Indented);
    File.WriteAllText(@"C:\Users\patri\OneDrive\Desktop\test.txt", obj);
}
  • Related