How do you use a string as an index?

Morning.

Problem:
I have a class called Reports. Two constructors. One gives no parameters, the other a string array. The string array should be the reports that they would like to display. I would like to do the following:

string strSQL = this.Queries[strReportName]; 

I have a feeling that this is possible, because in the dataGridView that I use, I get the column index:

 int nColumnIndex = dgvParts.Columns[strColumnName].Index; 

Both of these examples use a string to determine what value in the array they are retrieving, but I'm not sure how to do this. Can anyone help me? Any help is appreciated.

To editions and mods: Yes, part of the reports is closely related to another message that I have about dynamic loading of DLLs, but I would like to leave another open. My boss and I decided for a short time, we will have only one DLL, and everything will be hardcoded, but in the end we want to dynamically drop the DLL as reports, so please do not put this as a duplicate. This weekend I plan to try and implement the methods provided to me in another thread. Thanks.

Edit - part 2 of the question : Ok, here is my class, as of now:

 public class Queries { #region Report Queries #region Missing Code string strMissingCode = "SELECT * FROM PARTS WHERE CODE IS NULL OR CODE = ''"; #endregion #endregion } 

I would like to change it to something like this:

 public class Queries : Dictionary<string, string> { } 

But I do not want them to use the constructor to create it. I want static sorts so that I can run the code like this:

 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])) { ... } } } 

How to make it static sorts where I don't need to instantiate queries? Thank you guys and girls.

+7
string c # indexing
source share
6 answers

A Dictionary can use strings as an β€œindex” (in fact).

+9
source share

I think you are looking for something like the following:

 public class Reports { System.Collections.Generic.IDictionary<string,string> queries; public string this[string key] { get { return this.queries[key]; } } } 

See http://msdn.microsoft.com/en-us/library/aa288464(VS.71).aspx for more details.

If you do not need to add any additional functions to the Reports class, just use the dictionary others have published.

+13
source share

Look at the dictionary and iDictionary

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

+2
source share

will this.Queries.SingleOrDefault (x => x.Name == strReportName) not work? If not, you can create an extension method that hides this code in the indexer []

0
source share

You can use Array.IndexOf (this.Queries, strReportName);

0
source share

base arrays in C # are index based and cannot use string accessors.

It works:

 string[] someArray = {"query 1", "query 2"}; string myQuery = someArray[1]; 

It does not mean:

 myQuery = someArray["some value"]; 

Use the Dictionary class, which allows you to use keys of any type, including complex types.

-2
source share

All Articles