Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
namespace MeetingScheduler.Controllers
{
    public class EventsController : ApiController
    {
        EmailAddress[] emailAddress = new EmailAddress[]
        {
            new EmailAddress { Address = "hello@hello.com", Name = "helloname" }
        };

        Attendee[] attendees = new Attendee[]
        {
            new Attendee { EmailAddresses = emailAddress , Type = "user"}
        };

        Event[] events = new Event[]
        {
            new Event { Id = 1, Subject = "Hello World", Start = DateTime.Parse("2019-07-31 18:00:00"), End = DateTime.Parse("2019-07-31 20:00:00"), Location = "NCS Hub", Attendees = attendees }
        };

}
}


Can anyone tell me the correct way to store the emailAddress into Attendee?

What I have tried:

no idea. I have try to look for example online using the error message but did not find anything similar.
Posted
Updated 1-Aug-19 9:54am
Comments
Richard MacCutchan 31-Jul-19 9:28am    
The error message tells you what is wrong. Attendee cannot reference the value from emailAddress at this time, as it is not a static value. Similarly Event cannot reference attendees.

Can define the variables and initialize them in the constructor.

public class EventsController : ApiController
    {
        private EmailAddress[] emailAddress;
        private Attendee[] attendees;
        private Event[] events;

        public EventsController()
        {

            EmailAddress[] emailAddress = new EmailAddress[]
            {
                new EmailAddress { Address = "hello@hello.com", Name = "helloname" }
            };

            Attendee[] attendees = new Attendee[]
            {
                new Attendee { EmailAddresses = emailAddress , Type = "user"}
            };

            Event[] events = new Event[]
            {
                new Event { Id = 1, Subject = "Hello World", Start = DateTime.Parse("2019-07-31 18:00:00"), End = DateTime.Parse("2019-07-31 20:00:00"), Location = "NCS Hub", Attendees = attendees }
            };

        }

    }
 
Share this answer
 
v2
Based on the direction you appear to be going, you need to add an "AddAttendee" method to your EventsController class; e.g.

C#
public bool AddAttendee( string email, ...) {

   // Duplicate check
   ...
   attendees.Add( new Attendee(...));
   return true;  // Added.
}
 
Share this answer
 
Your intent is not clear here: do you want a Class instance for each Event, or, do you want a Class that will contain all the Events ?

Consider the different ways you can create default values for Class Properties. Assume you have this simple Class:
public class EmailAddress
{
    public EmailAddress(){}

    public string Address { get; set; }
    public string Name { get; set; }
}

// set default values in ctor
public EmailAddress(string address = "hello@hello.com", string name = "helloname")
{
    Address = address;
    Name = name;
}

// with c# >= 6 " : set default values in auto-property declaration
public EmailAddress(string address, string name) : this()
{
    if (address != null) Address = address;
    if (name != null) Name = name;
}

public string Address { get; set; } = "hello@hello.com";
public string Name { get; set; } = "helloname";
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900