XstreamINsanity
XstreamINsanity

Reputation: 4296

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.

Upvotes: 7

Views: 19425

Answers (6)

Chinjoo
Chinjoo

Reputation: 2792

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

Upvotes: 0

Patrick McDonald
Patrick McDonald

Reputation: 65421

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.

Upvotes: 14

KP.
KP.

Reputation: 13730

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.

Upvotes: -2

epitka
epitka

Reputation: 17637

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 []

Upvotes: 0

Alex
Alex

Reputation: 14618

Look at Dictionary and iDictionary

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

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

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

Upvotes: 10

Related Questions