CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2010
    Posts
    39

    NotSupportedException from IList/Array

    I have a CLR array in my code. What method will remove an element at the specified index ? The only thing I found was RemoveAt that is one of the IList interfaces. When I use something like

    Code:
    System::Collections::IList^ myList = myArray;
    aList->RemoveAt(k);
    the exception is thrown. I read in forums and MSDN that this is expected. Does that mean the method works and does what it has to do? If I just catch the exception and ignore it, will my app work how I want it to?

    If not, what is the solution ?

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: NotSupportedException from IList/Array

    Yes, this is indeed the expected behaviour of the "regular" array (System::Array) because it has a fixed length. I didn't try it, but I'm pretty sure that catching and ignoring the exception wouldn't help, as it wouldn't enable the RemoveAt() method to change the array's length either. And even if it would work, it would not be the clean way of doing things.

    There is, however, a designated variable-length replacement for the "regular" array: System::Collections::ArrayList. This does differ from the "regular" array in both interface and behaviour, though, and thus using it requieres significant changes to existing code.

    Here is a small working example. Note that you don't even have to cast the ArrayList to use RemoveAt():

    Code:
    #include "stdafx.h"
    
    using namespace System;
    
    void PrintArr(Collections::ArrayList ^a)
    {
      Console::Write("{");
      int i = 0;
      if (a->Count>i)
        while (1) {
          Console::Write(a[i]);
          if (++i>=a->Count)
            break;
          Console::Write(", ");
        }
      Console::WriteLine("}");
    }
    
    int main(array<System::String ^> ^args)
    {
      Collections::ArrayList ^arr = gcnew Collections::ArrayList;
      for (int i=0; i<10; ++i)
        arr->Add(i);
      PrintArr(arr);
      arr->RemoveAt(5);
      PrintArr(arr);
      Console::ReadLine();
      return 0;
    }
    HTH

    EDIT: A big difference between System::Collections::ArrayList and the "regular" array is that it accepts any element that is derived from Object. Thus, a single ArrayList allows elements of differing types. In that respect, System::Collections::Generic::List<T> might better resemble the "regular" array, in that it is determined to only contain elements of a certain type. This one is quite similar to the std::vector of native C++.
    Last edited by Eri523; October 10th, 2010 at 10:02 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Aug 2010
    Posts
    39

    Re: NotSupportedException from IList/Array

    Thanks a lot. I saw that ArrayList in the drop-down meny when I was typing Collections::, but didn't know how to use it and whether I had to. I'll try your solution. If I have a problem, I'll post it here.

    Honestly, when I work with C++/CLI, I always have a feeling of missing good old <vector>. It had all types of functionality to work with arrays.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: NotSupportedException from IList/Array

    Quote Originally Posted by MajinSaha View Post
    If I have a problem, I'll post it here.
    You're welcome.

    Honestly, when I work with C++/CLI, I always have a feeling of missing good old <vector>.
    For people who do that, like both of us , MS has created a set of STL/CLR containers that mimic the good ol' STL containers in C++/CLI. But be warned: I myself encountered severe problems when using particularly the STL/CLR vector (see http://www.codeguru.com/forum/showthread.php?t=503117) and ended up using a System::Containers::Generic::List<T>, at least in that particular app.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: NotSupportedException from IList/Array

    Quote Originally Posted by MajinSaha View Post
    Thanks a lot. I saw that ArrayList in the drop-down meny when I was typing Collections::, but didn't know how to use it and whether I had to. I'll try your solution. If I have a problem, I'll post it here.

    Honestly, when I work with C++/CLI, I always have a feeling of missing good old <vector>. It had all types of functionality to work with arrays.
    No, vector was not an array. STL's vector is a container that can grow and shrink and that is not an array. By definition, an array is a fixed size sequence. Now, STL does have an array class (or will have when C++0x will be finally released).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured