Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No you cannot. The name of the variable passed to your method is not available within the method.</p> <p><strong>Edit:</strong> based on your comments elsewhere, I clearly had the wrong impression of what you were trying to do. Instead of augmenting the SortedList with a name, you could pass a dictionary. </p> <pre><code> public void CallsDoStuffWithLists() { SortedList&lt;int, string&gt; theFirstList = new SortedList&lt;int, string&gt;(); SortedList&lt;int, string&gt; aSecondList = new SortedList&lt;int, string&gt;(); SortedList&lt;int, string&gt; thirdList = new SortedList&lt;int, string&gt;(); SortedList&lt;int, string&gt; theLastList = new SortedList&lt;int, string&gt;(); PopulateTheFirstList(theFirstList); PopulateTheSecondList(aSecondList); //etc // call do stuff with lists. DoStuffWithLists(new Dictionary&lt;string, SortedList&lt;int, string&gt;&gt;{{"theFirstList", theFirstList}, {"aSecondList",aSecondList}, {"thirdList", thirdList}, {"theLastList", theLastList}}); } public void DoStuffWithLists(Dictionary&lt;string, SortedList&lt;int,string&gt;&gt; lists) { // does not loop through all, // does not throw exceptions.. // if there is a list that was misnamed, it will not be handled. SortedList&lt;int, string&gt; 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? } } </code></pre> <p><strong>On a side note:</strong> Although I've shown you how you can do what you want, that doesn't mean it is a good idea. In this case it seems you are adding complexity without any gain. You have much more complex and error prone code, and though you are separating the data retrieval from the data processing per list, you are combining the dataprocessing for all the lists. </p> <p>Have you considered having DoStuffWithLists actually do the data retreival also? far less error prone, No need to match up names. </p> <pre><code>btn_Click(object sender, EventArgs e) { DoStuffWithLists(); } public void DoStuffWithLists() { //this is far simpler and less error prone. SortedList&lt;int, string&gt; theFirstList = new SortedList&lt;int, string&gt;(); PopulateTheFirstList(theFirstList); DoStuffWithSubList(theFirstList); SortedList&lt;int, string&gt; aSecondList = new SortedList&lt;int, string&gt;(); PopulateTheSecondList(aSecondList); DoOtherStuffWithSublist(aSecondList); } </code></pre> <p>Or better still having a method for handling each List completely and call that instead...</p> <pre><code>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&lt;int, string&gt; paygrades = new SortedList&lt;int, string&gt;(); populatePaygrades(paygrades, sqlconnection); DoStuffWithPaygrades(paygrades); } </code></pre> <p><strong>Edit: Original answer below--</strong> Based on how you have stuff named (lists), it looks as though you are expecting all the lists passed into doStuffWithLists to be handled simultaneously. This is not the case, each time it is called it deals with the one list that was passed. </p> <p>An Example using integers...</p> <pre><code>//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); } </code></pre> <p>Also Looks like you are trying to get doStuffWithLists to behave differently depending on where it was called from. In this case you just want different methods. </p> <pre><code>protected void doStuffWithPaygradeLists(SortedList&lt;int,string&gt; list) { ... } protected void doStuffWithSomeOtherLists(SortedList&lt;int,string&gt; list) { ... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload