Sending parameter to properties

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

I have a property in my class like this:

public string CurrentDisplayName
{
get
{
...
return s;
}
set
{
Session[LLCConstants.SESSION_INDEX_DISPLAYNAME] = value;
}
}


Can I send parameter to get/set methods? I am trying to do something like
this:

CurrentDisplayName(HttpContext) = "something";
or
variable = CurrentDisplayName(HttpContext)

Is that possible?

Thanks,
Alan
 
Alan,

The only way you can do this is through indexers, in which case, your
object is indexed. If you want to expose this through a property, then you
have to create an object with an indexer that you want, and then expose an
instance of that object through a property.

Hope this helps.
 
Hi Alan,

A.M said:
Hi,

I have a property in my class like this:
Can I send parameter to get/set methods? I am trying to do something like
this:

CurrentDisplayName(HttpContext) = "something";
or
variable = CurrentDisplayName(HttpContext)

Is that possible?

Thanks,
Alan

The short answer is "No". The longer answer is you can make it *look*
like that's what you are doing, but it's somewhat more involved. The
Page.Session property you reference in your property is an example of how it
can be done. The session property itself does not accept parameters, but the
HttpSessionState class that the property returns defines an "indexer". So,
if CurrentDisplayName returns an object that defines an indexer, you can use
syntax like this:

CurrentDisplayName[...] = "something"; // where "..." is the value of
the parameter passed to the indexer
or
variable = CurrentDisplayName[...];

Regards,
Daniel
 

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

Back
Top