user1679941
user1679941

Reputation:

How can I define my return type

I have the following code:

   public IList<Content.Grid> GetContentGrid(string pk)
    {
        // How can I define result to hold the return 
        // data? I tried the following but it does not
        // work:
        var result = new IList<Content.Grid>();

        var data = _contentRepository.GetPk(pk)
             .Select((t, index) => new Content.Grid()
             {
                 PartitionKey = t.PartitionKey
                 ....
             });

        switch (pk.Substring(2, 2))
        {
            case "00":
                return data
                    .OrderBy(item => item.Order)
                    .ToList();
                break;
            default:
                return data
                    .OrderBy(item => item.Order)
                    .ToList();
                break;

        }
    }

The VS2012 is telling me that the break is not needed so what I would like to do is to remove the returns from inside the switch, store the results in a variable and then after the switch is completed have:

return result;

Can someone tell me how I can declare the variable called result. I tried the following but this gives a syntax error:

var result = new IList<Content.Grid>();

Upvotes: 0

Views: 68

Answers (5)

Rawling
Rawling

Reputation: 50194

Don't use var, and you'll be fine; you don't need a default value as it'll just get overwritten.

IList<Content.Grid> result;

Upvotes: -1

looper
looper

Reputation: 1989

You already return the result in your switch:

return data
    .OrderBy(item => item.Order)
    .ToList();

There's no need to declare a variable before/return it after the switch, because you jump out of the switch with the return-statement. (That's why you don't need the break)

However, you could use the following:

IList<Content.Grid> result;

...

case "00":
            result = data
                .OrderBy(item => item.Order)
                .ToList();
            break;
        default:
            result = data
                .OrderBy(item => item.Order)
                .ToList();
            break;

...

return result;

Upvotes: 2

LukeHennerley
LukeHennerley

Reputation: 6444

You can't instansiate an interface, only implement them.

public class SomeList<T> : IList<T>
{

}

You need a type parameter to go to the IList, so have that in your normal class

IList<T> result = New SomeList<T>();

That should do it.

Upvotes: 0

Maciek
Maciek

Reputation: 1716

IList is an interface and it cannot be instantiated. You need to pick specific implementation of IList and create object of non-abstract class, like, for example, List:

var result = new List<Content.Grid>();

Upvotes: 1

TarkaDaal
TarkaDaal

Reputation: 19595

IList is an interface. You can't construct instances of interfaces. You must create an instance of a concrete type that implements that interface, such as:

var result = new List<Content.Grid>();

Upvotes: 1

Related Questions