Quick Indexers Q and a bit more...

P

Picho

Hi everyone.

Is it possible to decalre a static indexer to a class?
obviosly not, because the 'this' keyword is not valid when declaring it
static...
so how can I get a similar result of an indexer WITHOUT using accessor
methods like
static object GetObject(int index){...}
and
static void SetObject(int index, object value){...}
?

another question:
lets say i have a class:
public class ClassA
{
private object _value;

public ClassA()
{
}
public object Value
{
get{...}
set{...}
}
}

Can I cause one of the class properties to be a ... lets say default
property?
so I can write something like:

ClassA c = new ClassA();
c = null;
//instead of:
c.Value = null;

I know this looks twisted and silly, but i would realy like to try this -
skipping the property access stage (the property type is an object and thats
a given).
I tried creating an implicit conversion to and from 'object' which looked
like the right direction to me but then I got a compiler error that says I
cant cast from a base class (since ClassA derives from object naturally..)

smart compiler. silly me.

anyways,
can I do this in some manner or is it hopeless?

thanx alot,

Picho
 
J

Jon Skeet [C# MVP]

Picho said:
Is it possible to decalre a static indexer to a class?

Unfortunately not.
obviosly not, because the 'this' keyword is not valid when declaring it
static...
so how can I get a similar result of an indexer WITHOUT using accessor
methods like
static object GetObject(int index){...}
and
static void SetObject(int index, object value){...}
?

You can't, I'm afraid.
another question:
lets say i have a class:
public class ClassA
{
private object _value;

public ClassA()
{
}
public object Value
{
get{...}
set{...}
}
}

Can I cause one of the class properties to be a ... lets say default
property?
so I can write something like:

ClassA c = new ClassA();
c = null;
//instead of:
c.Value = null;

No - thank goodness. That would be *very* confusing, causing really
unreadable code. How would you change or access the actual value of c
rather than that of c.Value?
I know this looks twisted and silly, but i would realy like to try this -
skipping the property access stage (the property type is an object and thats
a given).

Could I ask why? What's the benefit here?
I tried creating an implicit conversion to and from 'object' which looked
like the right direction to me but then I got a compiler error that says I
cant cast from a base class (since ClassA derives from object naturally..)

smart compiler. silly me.

anyways,
can I do this in some manner or is it hopeless?

I don't believe you can do it.
 
P

Picho

Hi Jon, thanx for the reply.

it is unfortunate that we cannot set a static indexer....

the benefit of the other issue is... well simply much more clear code even
though i stated it seems twisted and silly for the same reasons you said and
therefor this question was doomed to start with so we might as well just
forget it :)

since you asked:

I am trying to write a class that will encapsulate application-wide
variables (very much like the session object in asp). for that I could use a
simple sorted list or other ADT. I wanted to add support for change events
for these variables. this means that the objects in the list are not the
actual variable values but other objects that i created. my goal was to be
able to write this:

//(Session is static)
Session["var1"] = true;
Session["var2"] = new string[] {"bla", "bla"};
Session["var2"].Change+=new EventHandler(var2_Change);

this will explain:
1. static indexer
2. twisted silly assignment

any suggestions?

Picho
 
J

Jon Skeet [C# MVP]

Picho said:
it is unfortunate that we cannot set a static indexer....
Agreed.

the benefit of the other issue is... well simply much more clear code even
though i stated it seems twisted and silly for the same reasons you said and
therefor this question was doomed to start with so we might as well just
forget it :)

since you asked:

I am trying to write a class that will encapsulate application-wide
variables (very much like the session object in asp). for that I could use a
simple sorted list or other ADT. I wanted to add support for change events
for these variables. this means that the objects in the list are not the
actual variable values but other objects that i created. my goal was to be
able to write this:

//(Session is static)
Session["var1"] = true;
Session["var2"] = new string[] {"bla", "bla"};
Session["var2"].Change+=new EventHandler(var2_Change);

this will explain:
1. static indexer
2. twisted silly assignment

any suggestions?

Well, you could make Session a singleton, and use

Session.Instance["var1"] = true;

or use

Session session = Session.Instance;
x["var1"]=true;
x["var2"]=true;

etc

I personally don't like the last bit of syntax, preferring:

Session.Change["var2"]+=new EventHandler(...);

However, getting that to work would be somewhat tricky too. I suggest
you just have:

Session.AddChangeHandler ("var2", new EventHandler(...));

Not quite as nice, but gets the job done.
 
P

Picho

well,
nice is what its all about :)

thanks.


Jon Skeet said:
Picho said:
it is unfortunate that we cannot set a static indexer....
Agreed.

the benefit of the other issue is... well simply much more clear code even
though i stated it seems twisted and silly for the same reasons you said and
therefor this question was doomed to start with so we might as well just
forget it :)

since you asked:

I am trying to write a class that will encapsulate application-wide
variables (very much like the session object in asp). for that I could use a
simple sorted list or other ADT. I wanted to add support for change events
for these variables. this means that the objects in the list are not the
actual variable values but other objects that i created. my goal was to be
able to write this:

//(Session is static)
Session["var1"] = true;
Session["var2"] = new string[] {"bla", "bla"};
Session["var2"].Change+=new EventHandler(var2_Change);

this will explain:
1. static indexer
2. twisted silly assignment

any suggestions?

Well, you could make Session a singleton, and use

Session.Instance["var1"] = true;

or use

Session session = Session.Instance;
x["var1"]=true;
x["var2"]=true;

etc

I personally don't like the last bit of syntax, preferring:

Session.Change["var2"]+=new EventHandler(...);

However, getting that to work would be somewhat tricky too. I suggest
you just have:

Session.AddChangeHandler ("var2", new EventHandler(...));

Not quite as nice, but gets the job done.
 
P

Picho

b.t.w:

could you explain 'singleton'?

thanks


Jon Skeet said:
Picho said:
it is unfortunate that we cannot set a static indexer....
Agreed.

the benefit of the other issue is... well simply much more clear code even
though i stated it seems twisted and silly for the same reasons you said and
therefor this question was doomed to start with so we might as well just
forget it :)

since you asked:

I am trying to write a class that will encapsulate application-wide
variables (very much like the session object in asp). for that I could use a
simple sorted list or other ADT. I wanted to add support for change events
for these variables. this means that the objects in the list are not the
actual variable values but other objects that i created. my goal was to be
able to write this:

//(Session is static)
Session["var1"] = true;
Session["var2"] = new string[] {"bla", "bla"};
Session["var2"].Change+=new EventHandler(var2_Change);

this will explain:
1. static indexer
2. twisted silly assignment

any suggestions?

Well, you could make Session a singleton, and use

Session.Instance["var1"] = true;

or use

Session session = Session.Instance;
x["var1"]=true;
x["var2"]=true;

etc

I personally don't like the last bit of syntax, preferring:

Session.Change["var2"]+=new EventHandler(...);

However, getting that to work would be somewhat tricky too. I suggest
you just have:

Session.AddChangeHandler ("var2", new EventHandler(...));

Not quite as nice, but gets the job done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top