creating a static Dictionary

Hi all,

I have been trying without success to create a static version of the Dictionary that I list below. The following code works when instantiated but I was unable to turn it into a static class. the dictionary holds approximatly 1000 entries and to be able to use a string as a key is most important. Any help getting me started would be appreciated. I would have listed the code that I tried but since it was off base I figured I would save the reader the added grieg of going through it. The parts I seem to have trouble with is replacing the "this" and "Dictionary" entries.


public class CodeXrefDictionary : DictionaryBase {

public CodeXrefDictionary(){
string cTmp = "";
string CodeXRef = "";
string xDataPath = "";
string sqlQuery = "";
string sqlConnection = "";
DataSet ds = new DataSet();
int iTmp=0;

try{ //just reading in some config info from an ini file
CodeXRef = Xtrn.iniProductFile.GetValue("X_Refs", "CodeXref");
if (CodeXRef=="") {
Console.WriteLine("No products X-refs listed in Product.Ini ???");
return;
}
}catch{
Console.WriteLine("No products X-refs listed in Product.Ini ???");
Application.Exit();
return;
}

//fixing up the path
xDataPath = Xtrn.iniProductFile.GetValue("X_Refs", "server") + Xtrn.iniProductFile.GetValue("X_Refs", "DataPath");
if (xDataPath.IndexOf("\", xDataPath.Length - 1)<0){
xDataPath = xDataPath + "\";
}

//accessing the FoxPro data
sqlConnection = "Provider=VFPOLEDB.1;Data Source=" + xDataPath + CodeXRef;
sqlQuery = "SELECT * from " + CodeXRef.Substring(0, CodeXRef.LastIndexOf("."));

ds.DataSetName = CodeXRef.Substring(0, CodeXRef.LastIndexOf(".")).ToLower();
try{
OleDbConnection cnItem = new OleDbConnection(sqlConnection);
cnItem.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery, cnItem);
adapter.Fill(ds);
cnItem.Close();
cnItem.Dispose();
adapter.Dispose();
}
catch(Exception){
}

//iTmp = ds.Tables[0].Rows.Count;

//Putting it into the Dictionary
foreach(DataRow myRow in ds.Tables[0].Rows){
//Product + Field + Code
cTmp = myRow.ItemArray[0].ToString().Trim() + //Product
myRow.ItemArray[1].ToString().Trim() + //Field
myRow.ItemArray[2].ToString().Trim(); //Code
if (cTmp.Length > 0){
this.Add( cTmp, myRow.ItemArray[4].ToString().Trim()); //replacement code
}
}
}



public String this[ String key ] {
get {
return( (String) Dictionary[key] );
}
set {
Dictionary[key] = value;
}
}

public ICollection Keys {
get {
return( Dictionary.Keys );
}
}

public ICollection Values {
get {
return( Dictionary.Values );
}
}

public void Add( String key, String value ) {
try{
Dictionary.Add( key, value );
}catch{
//skip this insert but let them know that there is a duplicate
AppLogger.Info("ADD(): Key already in Dictionary: " + key + ", " + value);
}
}

public bool Contains( String key ) {
return( Dictionary.Contains( key ) );
}

public void Remove( String key ) {
Dictionary.Remove( key );
}

public void Dump(){
DictionaryEntry myDE;
System.Collections.IEnumerator myEnumerator = this.GetEnumerator();
while ( myEnumerator.MoveNext() )
if ( myEnumerator.Current != null ) {
myDE = (DictionaryEntry) myEnumerator.Current;
Console.WriteLine(" " + myDE.Key + " : " + myDE.Value);
}
}
}
}

Comments

  • : Hi all,
    :
    : I have been trying without success to create a static version of the
    : Dictionary that I list below. The following code works when
    : instantiated but I was unable to turn it into a static class.

    Until the next version of the .NET framework, a class can have static members, but you cannot declare an entire class as static.

    : The parts I seem to have trouble with is replacing the "this"
    : and "Dictionary" entries.

    The static keyword cannot be applied to an indexer (the "this" keyword refers to the current instance, which does not exist in static methods), so you won't be able to get the Dictionary-like functionality, such as MyDictionaryClass["MyEntry"]. You'd have to implement a static method or property to access values.

    I think you'd be better off rolling your own version of a dictionary class if you're not going to instantiate it in the calling code. The Dictionary class' implementaion uses instance members and the inner collection itself is an instance member, therefore all of the important functionality of the Dictionary class would be off-limits to you because you cannot manipulate instance members from a static method or property. Hence you wouldn't really gain any functionality by deriving from Dictionary.
  • : : Hi all,
    : :
    : : I have been trying without success to create a static version of the
    : : Dictionary that I list below. The following code works when
    : : instantiated but I was unable to turn it into a static class.
    :
    : Until the next version of the .NET framework, a class can have static members, but you cannot declare an entire class as static.
    :
    : : The parts I seem to have trouble with is replacing the "this"
    : : and "Dictionary" entries.
    :
    : The static keyword cannot be applied to an indexer (the "this" keyword refers to the current instance, which does not exist in static methods), so you won't be able to get the Dictionary-like functionality, such as MyDictionaryClass["MyEntry"]. You'd have to implement a static method or property to access values.
    :
    : I think you'd be better off rolling your own version of a dictionary class if you're not going to instantiate it in the calling code. The Dictionary class' implementaion uses instance members and the inner collection itself is an instance member, therefore all of the important functionality of the Dictionary class would be off-limits to you because you cannot manipulate instance members from a static method or property. Hence you wouldn't really gain any functionality by deriving from Dictionary.
    :
    :
    :I knew the "this" keyword was referring to the current instance and I could deal with that but I guess you cleared up what was troubling me with the Dictionary methods in general. Does this same pattern (using instance members) hold true with the other collections? The Dictionary class has always been very convienient as far a being used for a lookup table using a string as a key.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories