Reputation: 3229
Let me start by showing the code:
public class Collection
{
private object[] values;
public Collection(int size) => values = new object[size];
public object this[string index]
{
get => values[index];
set => values[index] = (object)value;
}
}
public class Program
{
private static void Main()
{
Collection collection = new Collection(3);
collection["integer"] = 123;
collection["decimal"] = 456.78;
collection["text"] = "Hello World!";
double total = (double)(int)collection["integer"] + (double)collection["decimal"];
Console.WriteLine("Total is {0}", total);
Console.WriteLine();
Collection collection2 = new Collection(2);
collection2["integer"] = 123;
collection2["decimal"] = 456.78;
collection2["text"] = "Hello World!"; // This wont be added because of limit
}
}
I'm doing this tutorial, and class Program
was already given to me, and I can't modify it. What I need to do is to create Collection
class, so Collection
class is made by me. But there is this problem with Indexer
, because it is string
it doesn't seem to work the same way integer Indexers worked in previous tutorials. Is there a way to use string as Indexer, or should I consider different approach? Also adding namespaces are not allowed. I have been stuck in this tutorial for a week now.
Upvotes: 0
Views: 3340
Reputation: 11
You can use the following approach, just a little different from the one you mentioned but working perfectly.
using System;
namespace ass5
{
class Session
{
string[] values;
string[] key;
int i=0;
public Session(int size)
{
values = new string[size];
key=new string[size];
}
public string this[string index]
{
get
{
// i++;
int intIndex = Array.IndexOf(key, index);
if(intIndex==-1)
{
return " error";
}
return values[intIndex];
}
set
{
key[i]=index; // for index string storage
i++;
int intIndex= Array.IndexOf(key, index);
values[intIndex] = value; // for value corresponding to that index
}
}
}
class Program
{
static void Main(string[] args)
{
Session session=new Session(5);
session["username"]="user";
session["password"]="password";
Console.WriteLine(session["username"]);
}
}
}
Upvotes: 1
Reputation: 1833
Without using Dictionary...
This stores an object array and string array. The string array is used as your indexer, and the object array as your values. The keys and values are stored at the same location in their respective arrays.
There is plenty of optimization and improvement to be had here... but it should be a starting point.
class Collection
{
int size;
int items;
string[] keys;
object[] values;
public Collection(int size)
{
keys = new string[size];
values = new object[size];
this.size = size;
items = 0;
}
public object this[string index]
{
get
{
int position = -1;
for (int i = 0; i < size ; i++)
if (keys[i] == index)
position = i;
if (position == -1)
throw new ArgumentException("Index Not Found");
return values[position];
}
set
{
int position = -1;
for (int i = 0; i < size; i++)
if (keys[i] != null && keys[i] == index)
position = i;
if (position != -1)
{
values[position] = value;
}
else
{
if (items == size)
throw new Exception("Collection full");
keys[items] = index;
values[items] = value;
items++;
}
}
}
}
Upvotes: 1
Reputation: 127563
As a hint to point you in the right direction, (you are learning so I don't want to give it away> have Collection store two arrays, a object[]
and string[]
. See if that gets you going on the right direction.
If you need a further hint follow this link.
You will need to store the string name in the
string[]
in the same index you store the object in theobject[]
. Then if you callvar intIndex = Array.IndexOf(stringCollection, index)
you can use the result of that asreturn values[intIndex]
;
See if you can figure it out without following the link, or looking at the spoiler text above first!
Upvotes: 3
Reputation: 15112
You could use your class as below. check for ContainsKey is a good practice
class Collection
{
Dictionary<string, object> values = new Dictionary<string, object>();
public object this[string index]
{
get
{
if(values.ContainsKey(index)
{
return values[index];
}
return null;
}
set
{
if(!values.ContainsKey(index)
{
values.Add(index, value);
}
}
}
}
Upvotes: 0
Reputation: 5227
You can use Dictionary collection:
Dictionary<string, object> dict = new Dictionary<string, object>();
dict["hello"] = "world";
Upvotes: 3