Like we do Session.Add("LoginUserId", 123);
and then we can access Session["LoginUserId"]
, like an Array, how do we implement it?
4 Answers
You need an indexer:
public Thing this[string index]
{
get
{
// get the item for that index.
return YourGetItemMethod(index)
}
set
{
// set the item for this index. value will be of type Thing.
YourAddItemMethod(index, value)
}
}
This will let you use your class objects like an array:
MyClass cl = new MyClass();
cl["hello"] = anotherObject;
// etc.
There's also a tutorial available if you need more help.
Addendum:
You mention that you wanted this to be available on a static class. That get's a little more complicated, because you can't use a static indexer. If you want to use an indexer, you'd need to access it off of a static Field or some such sorcery as in this answer.

- 1
- 1

- 8,084
- 3
- 28
- 34
-
why not inherit from Dictionary
? – Mo Valipour Jul 24 '11 at 14:29 -
I gave the most basic way to do what he wanted. I don't know how he stores the collected objects, so he can use anything he likes from within an Indexer. Inheriting from Dictionary will just make what amounts to an alias for a Dictionary of predetermined types unless he overrides the right methods. – jakebasile Jul 24 '11 at 14:35
-
@Jake Basile, How do I write Add method? and unlike Indexers, I wouldn't like to create its object and using it like static class. – Riz Jul 24 '11 at 14:36
-
@eFriend, what do you need to do that you can't do with a normal Dictionary? – Can Gencer Jul 24 '11 at 14:38
-
Normal dictionary is your solution (it has Add, Remove, etc. and also indexer[string]), if you need specific actions for your class, create a class and inherit it from Dictionary
. Sometimes I see strange things in SO, I proposed this answer and I got -3 !!! – Mo Valipour Jul 24 '11 at 14:46 -
@valipour, I agree with you. Sometimes it's better not to give the original poster of the question exactly what they **think** they want! just like the customers ;) But I believe your answer was downvoted because you just pasted code with no context. – Can Gencer Jul 24 '11 at 15:09
Sounds like all you need is a generic dictionary.
var session = new Dictionary<string, object>();
//set value
session.Add("key", value);
//get value
var value = session["key"] as string;
If you want to make this static, just make it a static member in another class.
public static class SharedStorage
{
private static Dictionary<string, object> _data = new Dictionary<string,object>();
public static Dictionary<string, object> Data { get { return _data; } }
}
Then you can access it as such, without having to initialize it:
SharedStorage.Data.Add("someKey", "someValue");
string someValue = (string) SharedStorage.Data["someKey"];
If you want to be more adventurous and are using .NET 4 you can also use an Expando Object, like the ViewBag member available to controllers in ASP.NET MVC 3:
dynamic expando = new ExpandoObject();
expando.UserId = 5;
var userId = (int) expando.UserId;

- 8,822
- 5
- 33
- 52
You should use indexers See the link: http://msdn.microsoft.com/en-us/library/2549tw02.aspx

- 138
- 1
- 6
With the way you usually use the Session variable all you really need is a generic Dictionary collection like this one. You don't really need to write a class. But if you need to add extra functionality and/or semantics you could certainly wrap the collection with a class and just include and indexer.
For other collections check out the Collections.Generic namespace.

- 79,492
- 20
- 149
- 189
-
I want a simple class like Session. I should be able to a) Use without creating object, like static b) Using Add method to add elements c) accessing like array how can I achieve this? – Riz Jul 24 '11 at 14:41
-
1
-
@eFriend, take a look at my answer to see how you would do this in a static way. – Can Gencer Jul 24 '11 at 15:15