开发者

How to "edit" a external object in a function

I have this code :

CreaPager(m_oPacchettiEnum, 1, 10);

private void CreaPager(IEnumerable enPass, int limiteUp, int limiteDown)
{
    enPass= enPass.Skip(limiteDown).Take(limiteUp);
}

and I'd like, when I edit enPass, reflect the edit to m_oPacchettiEnum, without have a return IEnumerable<T> function.

So, edit directly m_oPacchettiEnum trought the function. I use to do it on C, don't know if is possible on C# (but I think so).

I read somethings about using out, but seems that it doesnt work :

CreaPager(out m_oPacchettiEnum, 1, 10);

private void CreaPager(out IE开发者_JAVA百科numerable enPass, int limiteUp, int limiteDown)
{
    enPass= enPass.Skip(limiteDown).Take(limiteUp);
}


out doesn't work here because you can't use an out parameter in the method until it's been assigned. Since your assignment uses enum (which you should rename) as part of the right-hand-side, it's not allowed. Switching out to ref will make things work.

However. As I mentioned in my answer to your previous (now deleted) question, please don't do this. It is much more idiomatic when working with sequences to return a new IEnumerable<T>:

var newQuery = CreaPager(m_oPacchettiEnum, 1, 10);

private void CreaPager<T>(IEnumerable<T> enumeration, int limiteUp, int limiteDown)
{
    return enumeration.Skip(limiteDown).Take(limiteUp);
}

As well, you can make the method an extension method by preceding the first parameter with this:

private void CreaPager<T>(this IEnumerable<T> enumeration, int limiteUp, int limiteDown) { .. }

Doing so would allow you to integrate your method into a LINQ-to-Objects call chain:

var query = myEnumerable.Where(x => x.OtherProp == 1).CreaPager(1, 10).Select(x => x.SomeProp);

EDIT: Since you need to also return a string, make that the out parameter:

private void CreaPager<T>(this IEnumerable<T> enumeration, int limiteUp, int limiteDown, out string myString)
{
    myString = "foo";
    return enumeration.Skip(limiteDown).Take(limiteUp);
}

You'd call the method like this:

string theString;
var newQuery = CreaPager(m_oPacchettiEnum, 1, 10, out theString);

out is fine here, since I'm assuming you don't need to both use and assign the string. And if you do, then I would suggest you split it into two parameters anyway.


Use ref instead of out:

CreaPager(ref m_oPacchettiEnum, 1, 10);

private void CreaPager(ref IEnumerable enum, int limiteUp, int limiteDown)
{
    enum = enum.Skip(limiteDown).Take(limiteUp);
}


@Serge answer would work but wouldn't it be better to do this

IEnumerable pager = CreaPager(m_oPacchetti, 1, 10);

private IEnumerable CreaPager(IEnumerable pachetti, int limiteUp, in limiteDown)
{
    return pachetti.Skip(limiteDown).Take(limiteUp - limiteDown);
}

That way you can reuse m_oPachetti and you avoid the nasty reference passing and the code is easier to read.

EDIT:

How about, since "Pager" is an italian noun??? Pager is an IEnumerable, you could also extend the functionality rather than passing the string in.

string theString = "TheStringYouNeedToReturn";
Pager<?> pager = new Pager<?>(m_oPacchetti, 1, 10);

private class Pager<T> : List<T>
{
    private Pager(){}

    public Pager(IEnumerable<T> pachetti, 
                   int limiteUp, 
                   int limiteDown, 
                   string aString)
    {
        this.AString = astring;
        this.AddRange(pachetti.Skip(limiteUp).Take(limiteUp - limiteDown);
    }

    public string AString {get; private set;}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜