How to pass a Dictionary variable to another procedure

烂漫一生 提交于 2019-12-13 04:36:42

问题


I am developing a C# VS2008/SQL Server website application. I've never used the Dictionary class before, but I am trying to replace my Hashtable with a Dictionary variable.

Here is a portion of my aspx.cs code:

...
Dictionary<string, string> openWith = new Dictionary<string, string>(); 

for (int col = 0; col < headers.Length; col++)
{
   @temp = (col + 1);
   @tempS = @temp.ToString();
   @tempT = "@col" + @temp.ToString();
   ...
   openWith.Add(@tempT, headers[col]);
}
...
for (int r = 0; r < myInputFile.Rows.Count; r++)
 { resultLabel.Text = ADONET_methods.AppendDataCT(myInputFile, openWith); }

But this is giving me a compiler error on this last line: Argument '2': cannot convert from 'System.Collections.Generic.Dictionary' to 'string'

How do I pass the entire openWith variable to AppendDataCT? AppendDataCT is the method that calls my SQL stored proc. I want to pass in the whole row where each row has a unique set of values that I want to add to my database table. For example, if each row requires values for cells A, B, and C, then I want to pass these 3 values to AppendDataCT, where all of these values are strings. How do I do this with Dictionary?


回答1:


the problem is not your dictionary and its not how you are passing it. the problem is your method AppendDataCT, it is expecting a string parameter, not a dictionary<string,string>

assuming this, you probably want something kinda like this....

foreach( KeyValuePair<string,string> item in openWith )
{
    resultLabel.Text = ADONET_methods.AppendDataCT(myInputFile, item.Value);
}

this will iterate over your dictionary, and for every pair of [key,value] call your AppendDataCT method, using the value from the pair.



来源:https://stackoverflow.com/questions/2987142/how-to-pass-a-dictionary-variable-to-another-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!