1,105,380 Community Members

Cannot implicitly convert type 'string' to 'Event' (Class)

Member Avatar
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Momerath
Senior Poster
3,831 posts since Aug 2010
Reputation Points: 1,327 [?]
Q&As Helped to Solve: 664 [?]
Skill Endorsements: 19 [?]
Featured
 
0
 

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.

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

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();
     }
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: