简体   繁体   中英

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.

    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.

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? 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.

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. I also don't understand why you have 2 levels of lists which seem to be the exact same implementation.

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.

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. 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.

A few other things that seem odd: Why is your ModuleList getter creating a new Modules object and then returning a property of it? Does the constructor for Modules do something to create the data underlying that ListOfModules property or retrieve it from somewhere?

Why does your ListOfModules property check if _modules is null, then return it whether or not it is null? I'd skip that and the _modules property and do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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