Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating object in for loop
    text
    copied!<p>Is there a difference in creating the object inside and outside for loop..I am not sure but i am experiencing an error with the oCategoryDefinition being declared outside of forloop :</p> <blockquote> <p>Invalid cast from 'System.Int32' to 'xxx.CategoryDefinition' at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) at System.Int32.System.IConvertible.ToType(Type type, IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at Service.AddFeatureValues(VINDescription vinDescription, VehicleDescription vehicleDescription)</p> </blockquote> <p>></p> <pre><code> for (int i = 0; i &lt; listLength; i++) { CategoryDefinition oCategoryDefinition = (CategoryDefinition)Convert.ChangeType (vehicleDescription.genericEquipment[i].Item, typeof(CategoryDefinition)); vinDescription.StandardFeature.Add(oCategoryDefinition.category.Value); } </code></pre> <p>AND</p> <pre><code>CategoryDefinition oCategoryDefinition = new CategoryDefinition(); for (int i = 0; i &lt; listLength; i++) { oCategoryDefinition = (CategoryDefinition)Convert.ChangeType (vehicleDescription.genericEquipment[i].Item, typeof(CategoryDefinition)); vinDescription.StandardFeature.Add(oCategoryDefinition.category.Value); } </code></pre> <p><strong>EDIT</strong></p> <p>To add one angle to the issue : Below is the method which is firing the <strong>above error</strong>: </p> <pre><code> private void AddFeatureValues(VINDescription vinDescription, VehicleDescription vehicleDescription) { GenericEquipment[] genericEquipList = null; //To add standard features to the vehicle details list. CategoryDefinition oCategoryDefinition = new CategoryDefinition(); try { genericEquipList = vehicleDescription.genericEquipment; if (genericEquipList != null) { //Stores the number of items in generic list. int listLength = genericEquipList.Length; for (int i = 0; i &lt; listLength; i++) { //Casting the object type to CategoryDefinition type. oCategoryDefinition = (CategoryDefinition)Convert.ChangeType(vehicleDescription.genericEquipment[i].Item, typeof(CategoryDefinition)); vinDescription.StandardFeature.Add(oCategoryDefinition.category.Value ?? "N/A"); } } } catch (Exception ex) { } } </code></pre>
 

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