Kennyist
Kennyist

Reputation: 63

c# How to return a list through a class return?

I've got a list in a class that I'm trying to return to view on a form but I cant return it from the class, How would I do this?

On the class:

private List<Module> modules;

public void AddModule(Module add)
{
    modules.Add(add);
}

public List Modules
{
    get { return modules; }
}

On the form:

moduleTitleLabel.Text = cnet.Title;

stgOne.AddModule(cnet);

moduleList.DataSource = stgOne.Modules;

I get an error on the List on the return.

Upvotes: 0

Views: 3010

Answers (2)

Kenneth
Kenneth

Reputation: 28747

Your datatypes are different:

private List<Module> modules;

public void AddModule(Module add)
{
    modules.Add(add);
}

// this one should be generic too
public List<Module> Modules
{
    get { return modules; }
}

Upvotes: 2

Zbigniew
Zbigniew

Reputation: 27614

You're missing generic type argument, it should be:

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

Upvotes: 1

Related Questions