C# 如何连接两个相同但在不同类中的变量?
好的,我正在制作一个数据库,其中我有4个类program.cs、Athletor.cs、event.cs和Vince.cs。他们希望我包括的方法之一是搜索运动员,以及显示赛事名称和场馆名称。这必须在所有变量都是私有的情况下完成 因此,我想知道是否有必要将运动员类中的event name变量连接到event类中的event name 运动员班C# 如何连接两个相同但在不同类中的变量?,c#,class,variables,C#,Class,Variables,好的,我正在制作一个数据库,其中我有4个类program.cs、Athletor.cs、event.cs和Vince.cs。他们希望我包括的方法之一是搜索运动员,以及显示赛事名称和场馆名称。这必须在所有变量都是私有的情况下完成 因此,我想知道是否有必要将运动员类中的event name变量连接到event类中的event name 运动员班 using System; using System.Collections.Generic; using System.Linq; using Syste
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
职业运动员
{
私有字符串firstname、lastname、地址、电话号码;
私有列表eventlist=新列表();
私人活动;竞技活动;
公共字符串athleteFirstname
{
收到
{
返回名字;
}
设置
{
firstname=值;
}
}
公共字符串athleteLastname
{
收到
{
返回姓氏;
}
设置
{
lastname=值;
}
}
公共字符串地址
{
收到
{
回信地址;
}
设置
{
地址=值;
}
}
公共字符串athletePhonenumber
{
收到
{
返回电话号码;
}
设置
{
phonenumber=值;
}
}
公共事件
{
收到
{
返回项目;
}
设置
{
athleteEvent=eventlist[0]。事件名称;
}
}
}
事件类
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
班级活动
{
私有字符串名称、日期、时间、venueEvent;
私人双重收费;
私人名单运动员=新名单();
公众运动员(运动员a)
{
运动员。添加(a);//向该类添加运动员
}
public void displayters()//用于在请求时显示运动员的方法
{
foreach(运动员中的运动员a)//构造函数
{
Console.WriteLine(a.athleteFirstname);
Console.WriteLine(a.athleteLastname);
}
}
公共字符串事件名
{
收到
{
返回名称;
}
设置
{
名称=值;
}
}
公共字符串eventDate
{
收到
{
返回日期;
}
设置
{
日期=价值;
}
}
公共字符串事件时间
{
收到
{
返回时间;
}
设置
{
时间=价值;
}
}
公众双倍活动费
{
收到
{
退货费;
}
设置
{
费用=价值;
}
}
公众字符串活动地点
{
收到
{
返回事件;
}
设置
{
venueEvent=值;
}
}
}
这是我的搜索方法
#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
#区域搜索
静态无效搜索()
{
Console.WriteLine(“请输入您要查找的运动员”);
字符串searchChoice=Convert.ToString(Console.ReadLine());
for(int i=0;i
对于每个运动员,您都有一个由代码表示的事件列表。当他们添加一名新运动员时,他们应该能够在控制台上添加一个事件列表,如{event1、event2…等}。在搜索过程中,您只需调用运动员对象atternate.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
}
这是家庭作业吗?如果是这样的话,它应该被标记为这样。你说的连接是什么意思?我不知道有家庭作业标记。嗯,运动员一定在某个项目中。我通过控制台输入给运动员他们参加的活动。但问题是,当我搜索运动员时,我还必须显示运动员参加的项目名称和举行该项目的地点。运动员目前只能被分配到一个项目。是的,当你添加一名运动员时,你可以为项目输入1或2。请参见编辑。你需要保存赛事,否则你将如何为运动员访问赛事?是的,我刚才看到了。你们都准备好了。不幸的是我的搜索结果让我失望