views:

189

answers:

5

Ok, I know that property overloading is not supported in C# - most of the references explain it by citing the single-method-different-returntype problem. However, what about setters? I'd like to directly assign a value as either a string or object, but only return as a string.

Like this:

    public string FieldIdList
    {
        get { return fieldIdList.ToString(); }
        set { fieldIdList = new FieldIdList(value); }
    }

    public FieldIdList FieldIdList 
    {
        set { fieldIdList = value; }
    }
    private FieldIdList fieldIdList;

Why wouldn't this be allowed? I've also seen that "properties" simply create getter/setter functions on compile. Would it be possible to create my own? Something like:

    public void set_FieldIdList(FieldIdList value)
    {
        fieldIdList = value;
    }

That would do the same thing. Thoughts?

+3  A: 

Properties are in fact a pair of get/set methods (one of which may be left out), but the property itself has additional metadata which makes it a property in the first place instead of just two methods.

Because the property signature remains invalid as per only-different-by-return-type, even if you only have a setter. If you need this, don't use a property; set-only properties aren't a good thing anyways.

Lucero
So, the compiler creates a signature for a pair of methods, and that's what prevents additional overloads? Does this just make Properties a convenience or is there more to it? What additional data makes it a property?
end-user
Not exactly. Just as you can't have two variables of a different type with the same name, you cannot have two properties with a different type and the same name. The problems are the same, the compiler would not know which variable or property to use in which situation. Therefore, even if you chose your accessors so that the get/set methods don't generate a conflict, the compiler cannot allow you to use the same property name multiple times if the signature is the same (note that you may have several indexer properties, because in this case the signature does differ, even with the same name).
Lucero
A: 

Its not allowed because of what you already stated in your question, for C# is not designed for that scenario. No overloaded reuse of names are possible because C# is a strongly typed language and does not allow implicit conversions.

OmegaMan
A: 

One approach (you may argue amongst yourselves as to whether this is a good design choice or not) is to add a second property, which accesses the data in a different form.

However, as it is going to be parsing the string (doing significant work), it would be better not to use a property for this, but to add methods that allow you to get/set the data in a string form.

I'd go for fieldIdList providing ToString() and TryParse() interfaces - then if you need it as a string, you'd call myObject.FieldIdList.ToString() etc. This encapsulates everything tidily, allows you to convert to/from string formats anywhere in your code rather than only when accessing FieldIdLists as a member of some other class, and makes the client code really clear and easy to understand.

Jason Williams
A: 

You can achieve your goal by writing something along these lines.

class Bar
{
    private string _foo = null;

    /// <summary>
    /// Gets or sets Foo. Set requires string or Foo object. Get returns string.
    /// </summary>
    public object Foo
    {
        get { return _foo; }
        set
        {
            if (value is string)
                _foo = (string)value;
            else if (value is Foo)
                _foo = value.ToString();
            else
                throw new ArgumentException("Must use string or Foo object");
        }
    }
}

class Foo
{
}
Anthony Pegram
A: 

If you want to be able to set a property as either a string or an object then you would just use object as the type of the property, since it's the base class (you can pass a string to a property that accepts an object). This doesn't seem particularly good design decision, but it can be done. Perhaps you need to explain further what you are trying to achieve?

Dan Diplo