Archive for category Uncategorized

Test Post For SyntaxHighlighter

Dictionary<string, string> ColumnValuesHash = new Dictionary<string,  string>();

// ... load with values...
foreach (KeyValuePair<string, string>  entry in ColumnValuesHash)
   Console.WriteLine("{0}: {1}", entry.Key,  entry.Value);



Share

C# Using the This Accessor of a Class (an Indexer)

In C# one of the most useful constructs is the ability to access data in a class via an indexer. Example is something like this:

MyClass ab() = new MyClass();

string myValue = ab[2];

This example will show you how to use and define an accessor for a class which takes a specific enum. In the example we will the access data within the class with another accessor. In the example, see the static method Usage() on the class for the how to run the example.

public class ShowConcepts
{
 public enum Operation
 {
   TargetA,
   TargetB,
 }

 private Dictionary<Operation, List<string>> _myData = new Dictionary<Operation,List<string>>();

 // Index the data here
 public List<string> this[Operation op]
 {
     get
        {
           return _myData[op];
        }

     set
        {
           _myData.Add(op, value);
        }

 }

 public static void Usage()
 {

   List<string> temp = new List<string>();
   temp.Add("Jabber");

   ShowConcepts sc = new ShowConcepts();

   sc[Operation.TargetA] = temp;

   temp = new List<string>();
   temp.Add("Wocky");

   sc[Operation.TargetB] = temp;

   // Prints Jabberwocky
   Console.WriteLine(sc[Operation.TargetA][0] + sc[Operation.TargetB][0]);

 }

}
  • Line 03: This will be the basis of each list which we will deal with in this enum. This is a small example, but one can obviously add more. This will be the key to access internal data in the hashtable/dictionary.
  • Line 09: Here is where we hold all the outside user generated string lists, each with the unique Operation enum.
  • Line 13: Process the target list either by setting it or getting it.
  • Line 28: This static method simply demonstrates how to use this class; for this article only.
  • Line 31: Create our first list.
  • Line 34: Create our list manager to hold the lists.
  • Line 36: Send the first list into the manager.
  • Line 41: Send in the second list.
  • Line 44: Access the strings via the multiple indexers.
Share

Tags:

Administration POST do not read

XXXXXX ÅÅÅÅÅÅÅ  ©©©©

Technorati Profile

Share