早上好。
问题:
我有一个名为 Reports 的类。两个构造函数。一个不允许参数,另一个允许字符串数组。字符串数组应该是他们想要显示的报告。我想做的是:
string strSQL = this.Queries[strReportName];
我觉得这是可能的,因为在我使用的 dataGridView 中,我通过以下方式获取列索引:
int nColumnIndex = dgvParts.Columns[strColumnName].Index;
这两个示例都使用字符串来确定它们检索的数组中的值,但我不确定如何执行此操作。谁能给我一些帮助?感谢您提供任何帮助。
致编辑和 mods:是的,报告部分与我的另一篇关于动态加载 DLL 的文章有松散的联系,但我想让另一篇文章保持打开状态。我的老板和我决定在短期内,我们将只有一个 DLL,所有内容都是硬编码的,但从长远来看,我们希望动态地放入 DLL 作为报告,所以请不要将其标记为重复。我计划这个周末尝试实现在另一个线程中给我的方法。谢谢。
编辑 - 问题的第 2 部分:好的,这是我现在的类(class):
public class Queries
{
#region Report Queries
#region Missing Code
string strMissingCode = "SELECT * FROM PARTS WHERE CODE IS NULL OR CODE = ''";
#endregion
#endregion
}
我想把它改成这样:
public class Queries : Dictionary<string, string>
{
}
但我不希望他们必须使用构造函数来实例化它。我想要某种静态的,这样我就可以运行这样的代码:
class Reports
{
private List<ReportRecord> _lstRecrods = new List<ReportRecord>();
public List<ReportRecord> Records { get { return _lstRecords; } }
public Reports(string[] strDisplayedReports)
{
foreach (string strReportTitle in strDisplayedReports)
{
this.BuildReportList(strReportTitle);
}
}
private void BuildReportList(string strReportTitle)
{
using (DataSet ds = Database.GetDataSet(Queries[strReportTitle]))
{
...
}
}
}
如何使它成为静态的,而不必实例化查询?谢谢伙计们。
最佳答案
我认为您正在寻找类似以下的内容:
public class Reports
{
System.Collections.Generic.IDictionary<string,string> queries;
public string this[string key]
{
get
{
return this.queries[key];
}
}
}
参见 http://msdn.microsoft.com/en-us/library/aa288464(VS.71).aspx了解更多信息。
如果您不需要向 Reports 类添加任何更多功能,则只需使用其他人发布的 Dictionary。
关于c# - 如何使用字符串作为索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3684685/