Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use a string as an Index?

Morning.

Issue:
I have a class called Reports. Two constructors. One allows no parameters, the other a string array. The string array is supposed to be the reports they'd like to display. What I'd like to do is the following:

string strSQL = this.Queries[strReportName];

I have a feeling it's possible because in the dataGridView I'm using, I get the column index by:

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

Both of those examples use a string to determine what value in the array they retrieve, but I'm not sure how to do this. Can anyone give me some help? Any and all help is appreciated.

To the editors and mods: Yes, the reports part loosely ties to the other post I have about dynamically loading DLLs but I'd like to keep the other open still. My boss and I decided for the short term, we'll just have a single DLL and everything is hard coded, but in the long run we want to dynamically drop in DLLs as reports, so please don't tag this as a duplicate. I plan this weekend to try and implement the methods given to me in the other thread. Thanks.

Edit - Part 2 of the question: Ok, here's my class as it is right now:

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

I'd like to change it to something like this:

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

But I don't want them to have to use a constructor to instantiate it. I want static of sorts so I can run 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 do I make it static of sorts to where I don't have to instantiate Queries? Thanks guys and gals.

like image 862
XstreamINsanity Avatar asked Sep 10 '10 12:09

XstreamINsanity


1 Answers

I think you're 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 info.

If you don't need to add any more functionality to your Reports class, then just use the Dictionary as others have posted.

like image 76
Patrick McDonald Avatar answered Sep 20 '22 09:09

Patrick McDonald