0

パラメータの変数名によってメソッドに渡されたパラメータにアクセスする方法はありますか?

btn_Click(object sender, EventArgs e)
{
    SortedList<int, string> paygrades = new SortedList<int, string>();
    populatePaygrades(paygrades, sqlconnection);
    doStuffWithLists(paygrades);
}

protected void doStuffWithLists(params SortedList<int,string>[] lists)
{
 doStuffWithSubList(lists.paygrades) //??? how can I access it similar to this?
}

あなたが提供できるかもしれないどんな援助にも前もって感謝します。

4

2 に答える 2

2

不可能です。paygrades名前はbtn_click関数内でローカルです。にpaygradesというフィールドが含まれていない限りSortedList(含まれていないことを保証します)、何も達成できません。

サブリストに名前でアクセスする場合は、SortedListを拡張して名前フィールドを追加し、最初のフィールドを名前で検索できます。

SortedListの拡張:

class MySortedList: SortedList {
    private string name;
    public string Name{
        get { return this.name; }
        set { this.name=value; }
    }

    //you then need to wrap all the constructors you need from SortedList
}

これをコードで使用する

protected void doStuffWithLists(params MySortedList<int,string>[] lists){
    foreach(MySortedList l in lists){
        if(l.Name == "paygrades")
             doStuffWithSubList(l);
}
于 2012-09-18T14:17:05.067 に答える
2

いいえ、あなたがすることはできません。メソッドに渡された変数の名前は、メソッド内では使用できません。

編集:他の場所でのあなたのコメントに基づいて、私は明らかにあなたがやろうとしていたことについて間違った印象を持っていました。SortedListに名前を追加する代わりに、辞書を渡すことができます。

    public void CallsDoStuffWithLists()
    {
        SortedList<int, string> theFirstList = new SortedList<int, string>();
        SortedList<int, string> aSecondList = new SortedList<int, string>();
        SortedList<int, string> thirdList = new SortedList<int, string>();
        SortedList<int, string> theLastList = new SortedList<int, string>();

        PopulateTheFirstList(theFirstList);
        PopulateTheSecondList(aSecondList);
        //etc
        // call do stuff with lists.
        DoStuffWithLists(new Dictionary<string, SortedList<int, string>>{{"theFirstList", theFirstList}, {"aSecondList",aSecondList}, {"thirdList", thirdList}, {"theLastList", theLastList}});
     }

    public void DoStuffWithLists(Dictionary<string, SortedList<int,string>> lists)
    {
        // does not loop through all, 
        // does not throw exceptions..
        // if there is a list that was misnamed, it will not be handled.
        SortedList<int, string> temp;
        if(lists.TryGetValue("theFirstList", out temp)) DoStuffWithSubList(temp);
        if(lists.TryGetValue("aSecondList", out temp)) DoStuffWithSubList(temp);



        // loops through each and acts on them accordingly.
        // if DoStuffWithLists is 
        foreach (var list in lists)
        {
            //or use switch statement.
            if(list.Key == "theFirstList")
            {
                DoStuffWithSubList(list.Value);
            }
            else if(list.Key == "aSecondList")
            {
                DoOtherStuffWithSublist(list.Value);
            }
            //etc...
            else
            {
                //we got an unexpected list, what do we do with it?
            }
        }

補足:私はあなたが望むことをどのように行うことができるかをあなたに示しましたが、それはそれが良い考えであるという意味ではありません。この場合、何の利益もなく複雑さを増しているようです。はるかに複雑でエラーが発生しやすいコードがあり、リストごとのデータ処理からデータ取得を分離していますが、すべてのリストのデータ処理を組み合わせています。

DoStuffWithListsに実際にデータの取得も行わせることを検討しましたか?エラーが発生しにくく、名前を一致させる必要がありません。

btn_Click(object sender, EventArgs e)                           
{                           

    DoStuffWithLists();                           
}    
public void DoStuffWithLists()
{
    //this is far simpler and less error prone.
    SortedList<int, string> theFirstList = new SortedList<int, string>();
    PopulateTheFirstList(theFirstList);
    DoStuffWithSubList(theFirstList);

    SortedList<int, string> aSecondList = new SortedList<int, string>();
    PopulateTheSecondList(aSecondList);
    DoOtherStuffWithSublist(aSecondList);

}

または、各リストを完全に処理するためのメソッドをまだ持っていて、代わりにそれを呼び出す方が良いです...

btn_Click(object sender, EventArgs e)                           
{   
    //this is simpler yet, separates the concerns of each type of list.                        
    HandlePayGrade();                  
    HandleSecondList();
    HandleThirdList();
}   
public void HandlePayGrade()
{
    // you are still separating your data access and processing concerns here.
    SortedList<int, string> paygrades = new SortedList<int, string>();                               
    populatePaygrades(paygrades, sqlconnection);       
    DoStuffWithPaygrades(paygrades);
}

編集:以下の元の回答- 名前の付いたもの(リスト)に基づいて、doStuffWithListsに渡されたすべてのリストが同時に処理されることを期待しているように見えます。これは当てはまりません。呼び出されるたびに、渡された1つのリストを処理します。

整数を使用した例...

//Double an int is invoked 3 times, each time dealing with one integer.
public int DoubleAnInt(int x)
{
    return x+x;   
}

public void CallsDoubleAnInt()
{
    int a = 1;
    int b = DoubleAnInt(a);

    int c = DoubleAnInt(b);
    int d = DoubleAnInt(c);
}

また、doStuffWithListsがどこから呼び出されたかによって、動作が異なるようにしようとしているようです。この場合、異なるメソッドが必要です。

protected void doStuffWithPaygradeLists(SortedList<int,string> list)
{
    ...
}

protected void doStuffWithSomeOtherLists(SortedList<int,string> list)
{
   ...
}
于 2012-09-18T14:19:21.897 に答える