7

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.

XstreamINsanity
  • 4,176
  • 10
  • 46
  • 59
  • 1
    Static indexers are (for no particularly good reason) not allowed. http://stackoverflow.com/questions/154489/are-static-indexers-not-supported-in-c – spender Sep 10 '10 at 13:28
  • lol. I was just reading a thread involving him on another site regarding this. It looked like he solved it, but if he says no here, then it's no. Dern it. :) Thanks. – XstreamINsanity Sep 10 '10 at 13:30

6 Answers6

14

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.

Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
  • 1
    +1 for mentioning defining one's own operator []. More info here: http://msdn.microsoft.com/en-us/library/6x16t2tx%28v=VS.80%29.aspx – spender Sep 10 '10 at 12:51
  • 2
    +1 that's the answer that says how to implement a string as index operator. The others only show other classes that have implemented them. – Oliver Sep 10 '10 at 12:56
10

A Dictionary can use strings as the "index" (key, actually).

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Thanks, this is what I'll be going with. I didn't even think to look at Dictionaries. Preciate it. – XstreamINsanity Sep 10 '10 at 12:48
  • @Ryan - I was going to, but I have another part to the question I had to add. If I don't get anything I need from the second part, I'll mark this one the answer. Thanks. – XstreamINsanity Sep 10 '10 at 13:12
  • Accepting this as answer as the answer to my second part is no and has been answered in another thread on this site previously. Thanks. – XstreamINsanity Sep 10 '10 at 13:31
2

Look at Dictionary and iDictionary

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

Alex
  • 14,338
  • 5
  • 41
  • 59
0

this.Queries.SingleOrDefault(x=>x.Name == strReportName) would not work? If not you can create a extension method that would hide this code in an indexer []

epitka
  • 17,275
  • 20
  • 88
  • 141
0

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

Chinjoo
  • 2,697
  • 6
  • 28
  • 45
-2

basic arrays in c# are index based and can't use string accessors.

This works:

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

This doesn't:

myQuery = someArray["some value"];

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

KP.
  • 13,218
  • 3
  • 40
  • 60