Hi
Im having this issue with my code. Cannot implicitly convert type 'string' to 'Event' (Class) - Thats the error message. Im trying to save an object of my class to an array but its coming up with this message and I dont see why it cant do it because it allows me to add my class Event to an array? Heres a part of my code:

class Event
    {
     private string myListofAthletes = "N/A";

public string ListofAthletes
        {
            get
            {
                return myListofAthletes;
            }
            set
            {
                myListofAthletes = value;
            }
        }

}

Event[] ListofAthletesArray = new Event[50];

Console.WriteLine("Enter List of Athletes participating in Event 1:");
            event1.ListofAthletes = Console.ReadLine();
           string test;
            test = Convert.ToString(event1.ListofAthletes);
            eventsarray[0] = event1;
            ListofAthletesArray[0] = test ;

What would I use to convert the string of event1.ListofAthletes to so that it can go inside the array?
Any suggestions would be appreciated :) THanks in advance

Recommended Answers

All 2 Replies

Well, you've created an array of Event so you can only store Event in the array. Event has a property, ListOfAthletes, which is a string. So you create an Event object, assign the string to ListOfAthletes, and then store the Event object:

Event newEvent = new Event();
newEvent.ListofAthletes = Console.ReadLine();
ListofAthletesArray[0] = newEvent;

You should rethink your class/variable names as they are confusing and don't represent what they are.

Simply do:

class Event
{
     private string myListofAthletes = "N/A";
     public string ListofAthletes
     {
          get { return myListofAthletes; }
          set { myListofAthletes = value; }
     }
}

class Program
{
     private static void Main()
     {
          Event[] ListofAthletesArray = new Event[50];
          Console.WriteLine("Enter List of Athletes participating in Event 1:");
          for(int i = 0; i < ListofAthletesArray.Length; i++)
          {
              Console.Write("{Insert {0}/{1} athlet: ", (i + 1).ToString() , ListofAthletesArray.Length);
              ListofAthletesArray[i].ListofAthletes = Console.ReadLine();               
          }
          Console.WriteLine("All done! Here is the list of all inserted:");
          foreach(Event athlet in ListofAthletesArray)
              Console.WriteLine(athlet);
          Console.ReadLine();
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.