简体   繁体   English

C#将两个属性链接在一起?

[英]C# link two properties together?

I am trying to retrieve a List of Modules for a student and add them to a property in another class in order to display them. 我正在尝试检索学生的模块列表,并将其添加到另一个类的属性中以显示它们。 The aim I have is, when I create an instance of a person and their Programme has a certain name, it will retrieve the modules for that programme. 我的目标是,当我创建一个人的实例并且他们的程序具有特定名称时,它将检索该程序的模块。

This is my method in my Programme class where I try to get the modules from the ListOfModules Method in the Modules class. 这是我的Program类中的方法,在这里我尝试从Modules类中的ListOfModules方法获取模块。

    public List<Modules> ModuleList
    {
        get
        {
            Modules m = new Modules();

            if (m.ListOfModules == null)
            {
                return null;
            }
            else
            {
                return m.ListOfModules;
            }
        }
    }

This is the ListOfModules method in the Modules Class. 这是Modules类中的ListOfModules方法。

internal List<Modules> ListOfModules
    {
        get 
        {
            if (_modules == null)
            {
                return _modules;
            }
            else
            {
                return _modules;
            }
        }
    }

and here is an example of how I add modules to a list of modules. 这是我如何将模块添加到模块列表的示例。

class CGP : Programmes
{
    List<Modules> modules;
    public CGP() :base("CGP") //CGP is the name of the programme
    {
        modules = new List<Modules>();
        modules.Add(new Modules("example 1"));
        modules.Add(new Modules("example 2"));
    }
}

So how can I go about getting the ModuleList method in the Programme class to get this list of modules from the ListOfModules method in the Modules class? 那么,如何才能在Program类中获取ModuleList方法,以从Modules类中的ListOfModules方法获取此模块列表? I know that these modules are being added to the ListOfModules method but they I cant seem to work out how to link it with the ModuleList so I can print out that list. 我知道这些模块已添加到ListOfModules方法中,但是我似乎无法弄清楚如何将其与ModuleList链接,以便我可以打印出该列表。

I will appreciate any help or support as I have spent countless amount of hours trying to figure this out. 我将花费大量的时间来解决这个问题,感谢您的帮助或支持。 Sorry for sounding like an amateur. 对不起,听起来像个业余爱好者。

The root of your problem seems to be your ModuleList in your base class. 问题的根源似乎是基类中的ModuleList。 I also don't understand why you have 2 levels of lists which seem to be the exact same implementation. 我也不明白为什么您有2个级别的列表,这些列表似乎是完全相同的实现。

class Programme
{
    private List<Module> _modules = new List<Module>();

    public List<Module> Modules { get { return _moduleList; } }
}

class CGP : Programme
{
    public CGP() :base("CGP")
    {
        Modules.Add(new Module("example 1"));
        Modules.Add(new Module("example 2"));
    }
}

This especially seems silly: 这尤其显得愚蠢:

internal List<Modules> ListOfModules
{
    get 
    {
        if (_modules == null)
        {
            return _modules;
        }
        else
        {
            return _modules;
        }
    }
}

You're returning the same thing either way so the check is unnecessary. 您将以任何一种方式返回同一件事,因此不需要检查。 It's essentially the same as just this: 基本上与此相同:

internal List<Modules> ListOfModules { get { return _modules; } }

There is no direct way of linking these things in the manner you describe. 没有直接的方式可以按照您描述的方式链接这些内容。

I would recommend using an interim service that Person can call into to retrieve modules by name. 我建议使用Person可以调用的临时服务来按名称检索模块。

Looks like your problem is that the List modules is declared in the CGP subclass, so the ModuleList property in the Programmes superclass cannot access it. 看起来您的问题是List模块在CGP子类中声明,因此Programs超类中的ModuleList属性无法访问它。 If you want a property in the Programmes class to be able to return the modules that you are adding in the CGP constructor, then they need to be added to a property of the Programmes class, perhaps declared as protected, so that only subclasses can access it. 如果希望Programs类中的属性能够返回要在CGP构造函数中添加的模块,则需要将它们添加到Programs类的属性中(可能已声明为protected),以便仅子类可以访问它。

A few other things that seem odd: Why is your ModuleList getter creating a new Modules object and then returning a property of it? 还有其他一些看起来很奇怪的事情:为什么您的ModuleList getter创建一个新的Modules对象,然后返回它的属性? Does the constructor for Modules do something to create the data underlying that ListOfModules property or retrieve it from somewhere? Modules的构造函数会做一些事情来创建ListOfModules属性基础的数据还是从某个地方检索数据?

Why does your ListOfModules property check if _modules is null, then return it whether or not it is null? 为什么您的ListOfModules属性检查_modules是否为空,然后返回它是否为空? I'd skip that and the _modules property and do: 我将跳过该内容和_modules属性,然后执行以下操作:

internal List<Modules> ListOfModules { get; private set; }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM