tags:

views:

135

answers:

3

I've got a Customer class that has a List<string> Roles property. Much of the time I want to access that property as a list of strings, but on occasion I want to see it as a comma-delimited list.

I could certainly do that in a new method, and if I anticipated wanting to get the value of the variable in different formats (comma-delimited, tab-delimited, &ct) I would certainly do so. However, I'm toying with the idea of using two different properties to access the variable value, something along the lines of

public List<string> Roles 
{
    get { return this._Roles; }
    set { this._Roles = value; } 
}

and

public string RolesToString
{
    get { do some work here to comma-delimit the list; }
}

Essentially I want to override the ToString() method of this particular list. Are there compelling reasons for doing 1 over the other? Is using two different properties to return the same variable value sufficiently non-standard to cause red flags?

+9  A: 

I would make your second "property" a method. It's doing additional processing on your list, and returning something that isn't a direct "property" of the object, but more a processed version of the object's property. This seems like a reasonable method candidate.

My preference would be:

public List<string> Roles 
{
    get { return this._Roles; }
    set { this._Roles = value; } 
}

public string GetRolesAsString()
{
    // Do processing on Roles
}
Reed Copsey
That logic makes perfect sense, thanks!
cori
+2  A: 

As Reed says it should probably be a Method, but thats kindof subjective.

Note that you don't need much code to do it - just a call to Join()

public string RolesAsString()
{
    return String.Join(", ", this._Roles);
}

And given that string joining is so easy in .NET, do you really need a method/property for it?

codeulike
that's exactly the code I was thinking of. I think I will go with the method though - seems more standard, and keeping in mind YAGNI, it's not inconceivable that I might need them in a different format later, and very little complexity to add it now in a method.I'm in .Net 2, so it's a tiny bit harder than that - near as I can tell I have to ToArray() the list first. This is essentially a library class, so I'm interested in making the client code calls as simple as possible, thus the method/property.
cori
A: 

I have no problem with what you propose. Except I would name it RolesString.

But... Why only a getter? If I can set the Roles property, why could I not set the RolesString property?

Additional processing does not necessarily mean a method should be used.

Cheeso
why no setter? yagni
Frank Schwieterman
I guess my thinking was that reading the value from different properties would be harmless but that it's possible writing it from two different properties could be potentially harmful. Also, the underlying object that I'm writing to (an SAP BAPI) needs the full list back when I update, and 90% of the management will be to add a single role, so using Roles.Add(string) is the best interface.
cori
those are fair points!
Cheeso