简体   繁体   English

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

[英]How to Connect two variables that are the same but in different classes?

Okay I'm making some sort of database where I have 4 classes program.cs, athlete.cs, event.cs and venue.cs. 好的,我正在建立某种数据库,其中有4个类program.cs,运动员,事件,事件和会场。 One of the methods they want me to include is to search for the athlete, as well as displaying the event name and venue name. 他们希望我包括的方法之一是搜索运动员,以及显示事件名称和场地名称。 This has to be done with all the variables being private. 这必须在所有变量均为私有的情况下完成。

So I was wondering if there is anyway to have the event name variable in the athlete class connect to the event name in the event class. 所以我想知道运动员类中是否有将事件名称变量连接到事件类中的事件名称。

Athlete Class 运动员班

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;
        }

    }
}

Event Class 活动类

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;
        }
    }
}

This is my search method 这是我的搜索方法

#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

For every Athlete you have a list of Events as denoted by your code. 对于每个运动员,您都有一个事件列表,如代码所示。 When they add a new Athlete they should be able to add a list of Events like {event1, event2...etc} at the console. 当他们添加新运动员时,他们应该能够在控制台上添加事件列表,例如{event1,event2 ... etc}。 In your search you just have to call the Athlete object, Athlete.GetEventsList() and print out each event associated with this Athlete. 在搜索中,您只需要调用Athlete对象Athlete.GetEventsList()并打印出与此运动员关联的每个事件。 I'll do pseudocode so you learn on your own. 我将做伪代码,以便您自己学习。

Edit First of all you need to save Events for each Athlete. 编辑首先,您需要为每个运动员保存赛事。 So you need a Set/Get method to save incoming Events from the console for the particular Athlete. 因此,您需要一种Set / Get方法来保存来自控制台的特定运动员的传入事件。 Once you have a list of Events for the Athlete then you can do this. 有了运动员的活动列表之后,就可以执行此操作。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java模板,如何使用两个具有相同名称和不同类型的类 - Java Templates, how to use two classes with the same name and different types 如何处理具有相同名称的两个类的MVC DisplayTemplates(不同的命名空间) - How to handle MVC DisplayTemplates for two classes with the same name (different namespaces) 如何将两个类连接在一起 - How to connect two classes together 创建两个具有相同变量但具有不同访问方法的类的最佳方法 - Best method of creating two classes that have the same variables, but with different access methods 记录同一接口的两个不同类 - Logging for two different classes of the same interface 在不同环境中将两个类映射到同一张表 - Mapping two classes to the same table in different environments 需要在两个不同的类中添加相同的属性 - Need to add same properties in two different classes 如何跨不同的类返回多个变量 - How to return multiple variables across different classes 是否可以关联两个包含相同方法但参数不同的不同类? - is it posible to relate two different classes that contains same method but with different parameters? 编写将两个不同类的变量放在一起的函数时遇到问题 - Having trouble writing a function that puts together variables of two different classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM