I want to be able to:
- Make sure the object has an index operator.
- If it is defined, I want to use it.
I want to implement this in the following code. The code contains an object (MyObject) that offers a way to go through a multidimensional array or an associated set of hash tables. It should also prevent errors from being provided if the node in the requested path does not exist. The part I cannot understand is the commented part of the code:
public class MyObject
{
private object myObject = null;
public MyObject()
{
}
public MyObject(object value)
{
myObject = value;
}
public void setValue(object value)
{
myObject = value;
}
public object getValue()
{
return myObject;
}
public object this[string key]
{
get
{
if (myObject == null)
{
return new MyObject(null);
}
else
{
}
}
set
{
if (myObject == null)
{
}
else{
}
}
}
}
This is what I want to accomplish:
string loremIpsumString = "lorem ipsum dolor sit amet";
int[] digits = new int[10];
for (int i = 0; i <= 3; i++) digits[i] = i;
Hashtable outerHashtable = new Hashtable();
Hashtable innerHashtable = new Hashtable();
innerHashtable.Add("contents", "this is inside");
outerHashtable.Add("outside", "this is outside");
outerHashtable.Add("inside", innerHashtable);
Response.Write( loremIpsumString );
Response.Write( digits[0] );
Response.Write( digits[1] );
Response.Write( digits[2] );
Response.Write( outerHashtable["outside"] );
Response.Write( ((Hashtable)outerHashtable["inside"])["contents"] );
MyObject myObject;
myObject = new MyObject(loremIpsumString);
Response.Write( myObject.getValue() );
Response.Write( myObject["unexistant"].getValue() );
myObject = new MyObject(digits);
Response.Write( myObject[0].getValue() );
Response.Write( myObject[1].getValue() );
Response.Write( myObject[2].getValue() );
myObject = new MyObject(outerHashtable);
Response.Write( myObject["outside"].getValue() );
Response.Write( myObject["inside"]["contents"].getValue() );
Response.Write( myObject["unexistant"].getValue() );
Response.Write( myObject["unexistant"]["unexistant"]["unexistant"].getValue() );
source
share