如何连接两个相同但在不同类中的变量?

时间:2012-02-28 23:45:10

标签: c# class variables

好的我正在建立一些数据库,我有4个类program.cs,athlete.cs,event.cs和venue.cs。他们希望我包括的方法之一是搜索运动员,以及显示活动名称和地点名称。这必须在所有变量都是私有的情况下完成。

所以我想知道是否还有运动员类中的事件名称变量连接到事件类中的事件名称。

运动员班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Athlete
{
    private string firstname, lastname, address, phonenumber;
    private List<Event> eventlist = new List<Event>();
    private Event athleteEvent;

    public string athleteFirstname
    {
        get
        {
            return firstname;
        }
        set
        {
            firstname = value;
        }
    }

    public string athleteLastname
    {
        get
        {
            return lastname;
        }
        set
        {
            lastname = value;
        }
    }
    public string athleteAddress
    {
        get
        {
            return address;
        }
        set
        {
            address = value;
        }
    }
    public string athletePhonenumber
    {
        get
        {
            return phonenumber;
        }
        set
        {
            phonenumber = value;
        }
    }

    public Event eventA
    {
        get
        {
            return athleteEvent;
        }
        set
        {
            athleteEvent = eventlist[0].eventName;
        }

    }
}

活动类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Event
{
    private string name, date, time, venueEvent;
    private double fee;
    private List<Athlete> athletes = new List<Athlete>();


    public void addAthlete(Athlete a)
    {
        athletes.Add(a);// adds the athletes towards this class
    }

    public void displayAthletes()// method used for displaying athlete when requested
    {
        foreach (Athlete a in athletes) //Constructor
        {
            Console.WriteLine(a.athleteFirstname);
            Console.WriteLine(a.athleteLastname);
        }
    }

    public string eventName
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
    public string eventDate
    {
        get
        {
            return date;
        }
        set
        {
            date = value;
        }
    }

    public string eventTime
    {
        get
        {
            return time;
        }
        set
        {
            time = value;
        }
    }

    public double eventFee
    {
        get
        {
            return fee;
        }
        set
        {
            fee = value;
        }
    }

    public string eventVenue
    {
        get
        {
            return venueEvent;
        }
        set
        {
            venueEvent = value;
        }
    }
}

这是我的搜索方法

#region Search Athlete
    static void searchAthlete()
    {
        Console.WriteLine("Please enter which athlete you would like to find");
        string searchChoice = Convert.ToString(Console.ReadLine());

        for(int i = 0; i < athleteClass.ToArray().Length;i++) // goes through the athletes class to the end
        {
            if (searchChoice == athleteClass[i].athleteFirstname) // checks to see what has been entered matches any of the data in the athlete class
            {
                Console.WriteLine("Athlete First Name: " + athleteClass[i].athleteFirstname);
                Console.WriteLine("Athlete Last Name:  " + athleteClass[i].athleteLastname);
                Console.WriteLine("Event Name: " + athleteClass[i].eventA);
                Console.WriteLine("Venue Name: " + eventClass[i].eventVenue);
            }
            else
            {
                Console.WriteLine("No record was found");
            }
            //for (int e = 0; e < 2; e++)
            //{
            //    if (athleteClass[e].eventA == eventClass[e].eventName) // checks 
            //    {
            //        Console.WriteLine("Event name: " + eventClass[e].eventName);
            //    }
            //    for (int v = 0; v < 2; v++)
            //    {
            //        if (eventClass[v].eventVenue == venueClass[v].venueName)
            //        {
            //            Console.WriteLine("Venue Name: " + venueClass[v].venueName);
            //        }
            //    }
            //}

        } 
    }
    #endregion

1 个答案:

答案 0 :(得分:0)

对于每位运动员,您都有代码所示的事件列表。当他们添加新的运动员时,他们应该能够在控制台上添加{event1,event2 ......等}事件列表。在您的搜索中,您只需要调用Athlete对象,Athlete.GetEventsList()并打印出与此运动员相关的每个事件。我会做伪代码,所以你可以自己学习。

修改 首先,您需要为每位运动员保存事件。因此,您需要一个Set / Get方法来保存来自控制台的特定运动员的传入事件。一旦你有一份运动员赛事清单,你就可以做到这一点。

for each Athlete a in ListOfAthletes
{
  get eventList for a
  for each event in eventList
   get event Name
   //print out athlete Name and event Name
}