Author Topic: Problems using ArrayList.Insert  (Read 1948 times)

0 Members and 1 Guest are viewing this topic.

HD

  • Guest
Problems using ArrayList.Insert
« on: September 24, 2007, 08:11:11 PM »
Hello,

I am having problems using the Insert method of the ArrayList class. Can someone please tell me if there's something obviously wrong with the way that I am trying to insert a value into an arraylist? The following code compiles, but when I run the AutoCAD command, AutoCAD halts on the "bolded" line.

    public class Commands
    {
        [CommandMethodAttribute("MxTest")]
        public void MxUtilsTest()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            // Prompt user to select files.
            ArrayList assemblyData = mmm.Mx.Utils.SelectFiles();
            assemblyData.Insert(0, "PICS ACAD Assembly|12-3000-0450-9|A");
           
            // Display user selected files.
            foreach (Object item in assemblyData)
            {
                ed.WriteMessage("Assembly Data: {0}\n", item);
            }
         }
    }

As a frame of reference, I have included the SelectFiles method that's called by the above method.
    public static class Utils
    {
        /// <summary>
        /// Prompts the user to select one or more drawing files.
        /// </summary>
        /// <returns>
        /// An ArrayList containing one or more drawing files.
        /// </returns>
        public static ArrayList SelectFiles()
        {
            // Create an instance of the OpenFileDialog class.
            Autodesk.AutoCAD.Windows.OpenFileDialog ofd =
                new Autodesk.AutoCAD.Windows.OpenFileDialog
                    ("Select One or More Drawing Files", null, "dwg", "MultipleDWGS",
                        Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);

            // Show common dialog box and determine if DialogResult = OK.
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string[] fileList = ofd.GetFilenames();
                return ArrayList.Adapter(fileList);
            }
            else
            {
                return new ArrayList();
            }
        }

Thanks,

Nick

Glenn R

  • Guest
Re: Problems using ArrayList.Insert
« Reply #1 on: September 24, 2007, 08:21:25 PM »
Here's how I'd do it:

Code: [Select]
ArrayList al = new ArrayList(fileList.Length);
al.AddRange(fileList);
return al;

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8789
  • AKA Daniel
Re: Problems using ArrayList.Insert
« Reply #2 on: September 24, 2007, 09:01:25 PM »
try creating a new ArrayList

Code: [Select]
ArrayList assemblyData = new ArrayList(mmm.Mx.Utils.SelectFiles());
assemblyData.Insert(0, (object)"PICS ACAD Assembly|12-3000-0450-9|A");