Firstly, I would advise against relying on the ToString()
method on objects unless it is specifically documented. In this case it works with the abstract class ItemField
, but this may not always be the case.
The TOM.Net API only defines Definition and Name properties for ItemField
, so you need to cast your ItemField object to something more specific.
the TextField
abstract class, which SingleLineTextField
inherits from, defines a ToString()
method, but also Value
and Values
properties, which are much better suited to what you're trying to do. Looking at the documentation, we can see that Values
will give us an IList<String>
of the values, even if your field is not multi-valued. Perfect!
So, to answer your question, "I was looking for some way to find out if a SingleLineTextField has more values in it", you need to cast your ItemField as a TextField and check the number of Values it provides, thus:
TextField textField = (TextField)itemField;
// If you need to deal with multi-valued fields separately
if (textField.Values.Count > 1)
{
//Logic to deal with multiple values goes here
}
else
{
//Logic to deal with single valued goes here
}
// Much better... If you can deal with any number of values in a generic fashion
foreach (string value in textField.Values)
{
// Generic code goes here
}