We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,645 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

3
Contributors
2
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
3
Views
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,726 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 12

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: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0612 seconds using 2.7MB