tags:

views:

111

answers:

6

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.

+6  A: 

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

Ignacio Vazquez-Abrams
Thanks, this is what I'll be going with. I didn't even think to look at Dictionaries. Preciate it.
XstreamINsanity
Make sure to mark as the answer.
Ryan Bennett
@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
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
+2  A: 

Look at Dictionary and iDictionary

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

Alexander
A: 

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
A: 

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
+5  A: 

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
+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
+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
A: 

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

Chinjoo