Error 3 Cannot Modify The Result Of An Unboxing Conversion
SQL Server 2014 Express resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. Ask a question Quick access Forums home Browse forums users FAQ Search related threads Remove From My Forums Answered by: Cannot modify the result of an unboxing conversion Archived Forums V > Visual C# Language Question 0 Sign in to vote Guys, I have created two hashtables with my own object types. These are public struct sCandle { public double Close; } public struct VCV { public double STDev; public double Mean; public int Obs; public int df; } My hashtables are created thus: for (int i = 0; i < instruments.Count; i++) { double[] dArr = new double[periods]; htCandles.Add(i, dArr); } //AND for (int i = 0; i < instruments.Count; i++) { VCV v = new VCV(); htVCV.Add(i,v); } What I want to do is pull out the double from the htCandles table and and do some calculations and parse it back to the htVCV table. I have tried the following code: for (int i = 0; i < instruments.Count; i++) { ((VCV)htVCV)[i].Mean = Mathematik.Statistics.Mean(((double[])htCandles.Values)); } But I have the following error Cannot modify the result of an unboxing conversion. if I remove the boxing the error is cannot convert type System.Collections.Hashtable to VCVDemo.VCV Could someone please tell me where I am going wrong in the casting of this subroutine Thanks, Harry Thursday, October 01, 2009 1:02 PM Answers 1 Sign in to vote There's a good reason it doesn't let you do that: You would be modifying a COPY of the original item, not the original item itself. When you access an element of a collection class that stores objects, if the element type is a struct then the element is "boxed" which involves making a copy of the element and putting it into a reference type. That's where the copy comes in. You could try using a Dictionary
help? Post your question and get tips & solutions from a community of 418,509 IT Pros & Developers. It's quick & easy. Cannot modify the result of an unboxing conversion? P: n/a Håkan Johansson error CS0445: Cannot modify the result of an unboxing conversion I'm quite new to C# and can't really see how to get rid of the above error. I have an ArrayList instance to which I add two types of elements. The first type of element is a class, the other type of element is a struct. Sometimes I need to change the field values of the elements held by https://social.msdn.microsoft.com/Forums/en-US/30481c07-c326-41fd-a550-8578dea0e3cf/cannot-modify-the-result-of-an-unboxing-conversion?forum=csharplanguage the ArrayList. In the case of the class type element, this is no problem. However, in the case of the struct type element, C# won't allow it. First of all, I don't really understand why. Secondly, do I really have to create a new class with similar content of the struct to get rid of this error? Regards Carl Johansson Jun 20 '07 #1 Post Reply Share this Question 6 Replies P: n/a https://bytes.com/topic/c-sharp/answers/665395-cannot-modify-result-unboxing-conversion marss Håkan Johansson wrote: error CS0445: Cannot modify the result of an unboxing conversion I'm quite new to C# and can't really see how to get rid of the above error. I have an ArrayList instance to which I add two types of elements. The first type of element is a class, the other type of element is a struct. Sometimes I need to change the field values of the elements held by the ArrayList. In the case of the class type element, this is no problem. However, in the case of the struct type element, C# won't allow it. First of all, I don't really understand why. Secondly, do I really have to create a new class with similar content of the struct to get rid of this error? Regards Carl Johansson Hi, I guess you wrote something like this: ((SomeStruct)ar[0]).Field1 = ...; Change it to: SomeStruct s1 = (SomeStruct)ar[0]; s1.Field1 = ...; Regards, Mykola http://marss.co.ua Jun 20 '07 #2 P: n/a Jon Skeet [C# MVP] On Jun 20, 10:06 am, "Håkan Johansson" programming questions. Go to Page... Thread Tools Display Modes 01-17-2010, 12:32 PM #1 malmaa Member Join Date: Dec 2009 Posts: 67 Thanks: 0 Thanked 0 times in 0 posts Modifying http://ninjatrader.com/support/forum/showthread.php?t=24801 elements of an ArrayList- Any help please? This might be a general C# http://findbugs.sourceforge.net/bugDescriptions.html question. But any help or guidance is highly appreciated. I am using a ArrayList of struc “Osto”. I Unbox the values as I read from the ArrayList “List”. Struct “Osto” contains IOrders like Oe1, Oe2, Os1, Os2 and Ol1 along with other data types. Now I like to change one element error 3 (Oe1) from a particular element (for instance say 2nd element) of the Arraylist “List”. I get an error message “Cannot modify the result of an unboxing conversation” near If-statement. Do I have to remove the i th element, reconstruct it and then add it back to the “List”? Any simpler way of modifying just the particular inner element(Oe1)? Many thanks in advance. My Code: // error 3 cannot struc containing info of stops, limits, IOrders of Positions that opened at an instance privatestruct Osto {publicdouble op;publicdouble st;public DateTime tm;public IOrder oe1;public IOrder oe2;public IOrder os1;public IOrder os2;public IOrder ot1;} // Arraylist to store the data of Resistance, Support and Orders private ArrayList Shortord = new ArrayList(); privateint Gono(IOrder Or, ArrayList List){ bool B1; for (int i=0;i and Publications Links Downloads FindBugs Swag Development Open bugs Reporting bugs Contributing Dev team API [no frames] Change log SF project page Browse source Latest code changes FindBugs Bug Descriptions This document lists the standard bug patterns reported by FindBugs version 3.0.1. Summary DescriptionCategory BC: Equals method should not assume anything about the type of its argumentBad practice BIT: Check for sign of bitwise operationBad practice CN: Class implements Cloneable but does not define or use clone methodBad practice CN: clone method does not call super.clone()Bad practice CN: Class defines clone() but doesn't implement CloneableBad practice CNT: Rough value of known constant foundBad practice Co: Abstract class defines covariant compareTo() methodBad practice Co: compareTo()/compare() incorrectly handles float or double valueBad practice Co: compareTo()/compare() returns Integer.MIN_VALUEBad practice Co: Covariant compareTo() method definedBad practice DE: Method might drop exceptionBad practice DE: Method might ignore exceptionBad practice DMI: Adding elements of an entry set may fail due to reuse of Entry objectsBad practice DMI: Random object created and used only onceBad practice DMI: Don't use removeAll to clear a collectionBad practice Dm: Method invokes System.exit(...)Bad practice Dm: Method invokes dangerous method runFinalizersOnExitBad practice ES: Comparison of String parameter using == or !=Bad practice ES: Comparison of String objects using == or !=Bad practice Eq: Abstract class defines covariant equals() methodBad practice Eq: Equals checks for incompatible operandBad practice Eq: Class defines compareTo(...) and uses Object.equals()Bad practice Eq: equals method fails for subtypesBad practice Eq: Covariant equals() method definedBad practice FI: Empty finalizer should be deletedBad practice FI: Explicit invocation of finalizerBad practice FI: Finalizer nulls fieldsBad practice FI: Finalizer only nulls fieldsBad practice FI: Finalizer does not call superclass finalizerBad practice FI: Finalizer nullifies