Modify a structure from ArrayList

I have an ArrayList list, which I populate with some structures like

struct NiftyStructure

{

     public int number1, number2...;
}

Doing something like:

((NiftyStructure)list[3]).number1--;

doesnt work.  I get a "Error 2 Cannot modify the result of an unboxing conversion" compilation error.

Can anyone help please

 



Answer this question

Modify a structure from ArrayList

  • Jay Gattani

    1. Can't I do it without changing struct to class

    2. I am using .NET Framework 2.0.

    3.Yes, I am learning that the hard way... they become very confusing in large projects.

     


  • Vasudeva

    1. try changing it from a structure to a class.

    2. are you in 1.1 or 2.0

    3. public fields are a bad, bad, bad idea!

     



  • adkent

    1. Do some research on Reference Types vs Value Types and at the same time, how an ArrayList (which is built to work with reference types) must box/unbox to deal with Value types and you will see why what you will fail. Too much to explain here, but this is one of the ideosyncracies of .Net that need to be understood.

    If you are using 2.0 look at the List generic. use that instead of ArrayList (not sure if it works with structs)

    3 (and more) I am assuming the example you are using is extremely trivial.

    In your situation are you specifically interested to using the ++ operator on a member field

    you wont be able to use ++ on a property.

    You could create methods "IncNumber1()", "IncNumber2()"

    and call list[ i ].IncNumber1()

    If you use the generic, casting the List Item is unnecessary as the generic is typed as a list of your object. 



  • lavrinenkoe

    The problem is this.  When you retrieve a value type object from a collection you acttually get a copy of the object.  Setting properties of this copy through a temprary reference is pointless because they would be immediately lost.  What you have to do is assign the copy to a permanent reference, i.e. a variable, set its properties and then assign that object back to the element of the collection.

  • DivyaNSingh

    Hi Allen,

    I am using structures to hold some of the groups of financial data structures. I am dealing with some 2 lakhs of records which will be loaded into different structures. For storing collection of structures I am using Generic List. Everything is working fine, but, earlier the collection of structures was implemented using array of structures in VB6.0 and now when we changed this implementation to use Generic List in C# .Net 2.0, the time for loading and make use of the generic list has increased.

    I basically want to understand few things here,

    What will be memory allocation when we insert the instance of an structure into Generic List Will the instance of structure be still a value type or will this instance be boxed and allocated on to the heap If this is boxed then again when we want to access this instance, i guess this will again be unboxed, which I think is an performance hit.

    Also I want to know if i change the Generic List implementation back to array of structures, can i expect an increase in performance of the application

    Please let me know if you need anyother information.

    Thanks,

    Venkatesh



  • Modify a structure from ArrayList