Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It means that you're casting (which can be costly) the <code>customField</code> variable multiple times, and that you'd be better of by casting only once.</p> <p>You can use the <a href="http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.71%29.aspx" rel="nofollow"><code>as</code></a> operator to achieve that, since the <code>as</code> operator performs the cast and returns an instance of the desired type, or NULL if the object could not be casted to the desired type.</p> <p>Like this:</p> <pre><code>DateCustomFieldRef customField = customFields[f] as DateCustomFieldRef; // the as operator returns null if the casting did not succeed (that is, customFields[f] is not a DatecustomFieldRef instance if (customField != null) { DateCustomFieldRef dateField = customField; if (dateField.internalId != null &amp;&amp; dateField.internalId == "created_date") { createdDate = dateField.value.ToString(); } } else { var stringField = customFields[f] as StringCustomFieldRef; if (stringField != null ) { StringCustomFieldRef tradingNameField = stringField; if (businessNameField.internalId != null &amp;&amp; businessNameField.internalId == "business_name") { businessName = businessNameField.value; } } } </code></pre> <p>But, I believe that there probably exists an even better solution (although I do not know your project, nor your code), but wouldn't it be possible to abstract some things away ?</p> <p>Perhaps you have a <code>CustomField</code> baseclass, and <code>DateCustomFieldRef</code> and <code>StringCustomFieldRef</code> inherit from that <code>Customfield</code> base-class. If that's the case, you could create a virtual (or maybe even abstract) method in the <code>CustomField</code> base-class, which is overriden in each child-class, which actually returns the value of that field.</p> <p>Like this:</p> <pre><code>public class CustomField&lt;T&gt; { public string Internalid { get; set; } public T Value { get; set; } public virtual string GetStringRepresentation() { return Value.ToString(); } } public class DateCustomField : CustomField&lt;DateTime&gt; { public override string GetStringRepresentation() { return Value.ToShortDateString(); } } </code></pre> <p>Your code can then look much more simple:</p> <pre><code>foreach( CustomField f in customFields ) { if( f.InternalId == "created_date" ) { createdDate = f.GetStringRepresentation(); } if( f.InternalId == "business_name" ) { businessName = f.GetStringRepresentation(); } } </code></pre> <p>(The code above could be made more simple and clean, but you'll get the drift.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload