Cannot implicitly convert type 'string' to 'Event' (Class)
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
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
Skill Endorsements: 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.
Momerath
Senior Poster
3,734 posts since Aug 2010
Reputation Points: 1,336
Solved Threads: 624
Skill Endorsements: 13
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();
}
}
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 15