Adding a Property to a Properties getter and setter

J

Jimbo

I am sort of new to C#. Currently have a private property called
"_name" in a class. I have written a public getter and setter routine
for it called "Name".

Currently, the getter for the property does some data manipulation
before it returns the value. I wanted to add another getter to this
property that would returnt the "Raw" value (what is stored in _name).


Example:
myObject.Name.Raw --returns the raw data

Any ideas would be very helpful. Thanks in advance.
 
S

Sean Hederman

Either:
a) Wrap the returned item into another class which has a Raw property and a
Value property.
or
b) Create a separate getter: myObject.NameRaw
 
B

Bob Grommes

First off, you don't have a property called "_name". You have a class
member or field called "_name" which you are exposing with a property called
"Name".

No property can have more than one getter and/or setter. If you want to
expose the raw value separately then you have to create another property
with a different name, such as RawName. There are no limitations about
where a property getter derives the values it returns, so multiple
properties can access the same class member if needed.

The syntax you describe, myObject.Name.Raw, would require that Name expose a
class or structure with a property of its own called Raw. And then your
doctored property would have to be exposed in the same way, e.g.,
myObject.Name.Doctored, since Name would only return a reference to the
class instance that implements Name.

--Bob
 
R

RCS

Or.. make it an overloaded method (assuming Processed() is a function that
processes the raw value into a finished value):

public string Name(bool ReturnRaw)
{
if ( ReturnRaw )
return _Name;
else
return Processed(_Name);
}
public string Name()
{
return Name(false);
}
 

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

Top