Author Topic: Oh the joys of a "high level" language!  (Read 8167 times)

0 Members and 1 Guest are viewing this topic.

Chuck Gabriel

  • Guest
Oh the joys of a "high level" language!
« on: May 15, 2008, 08:22:46 PM »
Can anybody tell me how to cram an integer (from user input) into an instance of Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags?

I have gotten a couple of different cast permutations to compile, but they throw an invalid cast exception at runtime.

Here is the whole shooting match for context:

Code: [Select]
using System;
using System.IO;
using System.Text;

using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;

using ofdFlags = Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags;

namespace GetFileDialog
{
    public class Main
    {
        [LispFunction("GetFileDialog")]
        public static ResultBuffer GetFileDialog(ResultBuffer buff)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            if (buff == null)
            {
                ed.WriteMessage("\nToo few arguments.");
                return null;
            }

            ResultBufferEnumerator iter = buff.GetEnumerator();

            iter.MoveNext();
            if (iter.Current.TypeCode != (short)LispDataType.Text)
            {
                ed.WriteMessage("\nGetFileDialog expects arguments of string string string int.");
                return null;
            }
            string title = (string)iter.Current.Value;

            if (iter.MoveNext() == false)
            {
                ed.WriteMessage("\nToo few arguments.");
                return null;
            }
            if (iter.Current.TypeCode != (short)LispDataType.Text)
            {
                ed.WriteMessage("\nGetFileDialog expects arguments of string string string int.");
                return null;
            }
            string defaultFileName = (string)iter.Current.Value;

            if (iter.MoveNext() == false)
            {
                ed.WriteMessage("\nToo few arguments.");
                return null;
            }
            if (iter.Current.TypeCode != (short)LispDataType.Text)
            {
                ed.WriteMessage("\nGetFileDialog expects arguments of string string string int.");
                return null;
            }
            string defaultExtension = (string)iter.Current.Value;

            if (iter.MoveNext() == false)
            {
                ed.WriteMessage("\nToo few arguments.");
                return null;
            }
            if (iter.Current.TypeCode != (short)LispDataType.Int16)
            {
                ed.WriteMessage(LispDataType.Int16.ToString());
                ed.WriteMessage("\nGetFileDialog expects arguments of string string string int.");
                return null;
            }
            int flags = (int)iter.Current.Value;

            if (((flags & (int)ofdFlags.DefaultIsFolder) != 0) && Path.HasExtension(defaultFileName))
            {
                defaultFileName = Path.GetDirectoryName(defaultFileName);
            }

            OpenFileDialog ofd = new OpenFileDialog(title, defaultFileName, defaultExtension, title, (ofdFlags)flags);
            if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return null;
            }

            ResultBuffer result = new ResultBuffer();
            foreach (string file in ofd.GetFilenames())
            {
                result.Add(new TypedValue((int)LispDataType.Text, file));
            }
            return result;
        }
    }
}

Chuck Gabriel

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #1 on: May 15, 2008, 08:29:04 PM »
Here are the casts I've already tried:

(ofdFlags)flags

(ofdFlags)Enum.ToObject(typeof(ofdFlags), flags)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Oh the joys of a "high level" language!
« Reply #2 on: May 15, 2008, 08:36:01 PM »
Chuck, I don't have docs handy at the moment .. but are the fields int16 or int32 ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

tjr

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #3 on: May 15, 2008, 08:50:43 PM »
What int values are you trying to pass to the flags?

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8709
  • AKA Daniel
Re: Oh the joys of a "high level" language!
« Reply #4 on: May 15, 2008, 08:53:04 PM »
Code: [Select]
//(GetFileDialog "Title" "MyFile" "dwg" 4)

    [LispFunction("GetFileDialog")]
    public static ResultBuffer GetFileDialog(ResultBuffer buff)
    {
      Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
      ResultBuffer result = new ResultBuffer();

      try
      {
        if (buff == null)
        {
          ed.WriteMessage("\nToo few arguments.");
          return null;
        }

        ResultBufferEnumerator iter = buff.GetEnumerator();

        iter.MoveNext();
        if (iter.Current.TypeCode != (short)LispDataType.Text)
        {
          ed.WriteMessage("\nGetFileDialog expects arguments of string string string int.");
          return null;
        }
        string title = (string)iter.Current.Value;

        if (iter.MoveNext() == false)
        {
          ed.WriteMessage("\nToo few arguments.");
          return null;
        }
        if (iter.Current.TypeCode != (short)LispDataType.Text)
        {
          ed.WriteMessage("\nGetFileDialog expects arguments of string string string int.");
          return null;
        }
        string defaultFileName = (string)iter.Current.Value;

        if (iter.MoveNext() == false)
        {
          ed.WriteMessage("\nToo few arguments.");
          return null;
        }
        if (iter.Current.TypeCode != (short)LispDataType.Text)
        {
          ed.WriteMessage("\nGetFileDialog expects arguments of string string string int.");
          return null;
        }
        string defaultExtension = (string)iter.Current.Value;

        if (iter.MoveNext() == false)
        {
          ed.WriteMessage("\nToo few arguments.");
          return null;
        }
        if (iter.Current.TypeCode != (short)LispDataType.Int16)
        {
          ed.WriteMessage(LispDataType.Int16.ToString());
          ed.WriteMessage("\nGetFileDialog expects arguments of string string string int.");
          return null;
        }
        ofdFlags flags = (ofdFlags)(short)iter.Current.Value;

        if (((flags & ofdFlags.DefaultIsFolder) != 0) && Path.HasExtension(defaultFileName))
        {
          defaultFileName = Path.GetDirectoryName(defaultFileName);
        }

        OpenFileDialog ofd = new OpenFileDialog(title, defaultFileName, defaultExtension, title, flags);
        if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
        {
          return null;
        }

        foreach (string file in ofd.GetFilenames())
        {
          result.Add(new TypedValue((int)LispDataType.Text, file));
        }
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage(ex.Message);
        ed.WriteMessage(ex.StackTrace);
      }
      return result;
    }

Edit: removed the extra casts
« Last Edit: May 15, 2008, 09:08:06 PM by Daniel »

Chuck Gabriel

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #5 on: May 15, 2008, 08:53:38 PM »
Chuck, I don't have docs handy at the moment .. but are the fields int16 or int32 ??

Int16

What int values are you trying to pass to the flags?

In this particular instance, 16.

Thanks for your help you guys.

Chuck Gabriel

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #6 on: May 15, 2008, 08:57:34 PM »
Daniel,

Is that your way of telling me that maybe I should give people a hint about how I intend to call this code?  :-)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Oh the joys of a "high level" language!
« Reply #7 on: May 15, 2008, 08:59:18 PM »
Chuck, I don't have docs handy at the moment .. but are the fields int16 or int32 ??

Int16



As Daniels code indicates ..

int32 is int

int16 is short
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8709
  • AKA Daniel
Re: Oh the joys of a "high level" language!
« Reply #8 on: May 15, 2008, 09:01:01 PM »
Daniel,

Is that your way of telling me that maybe I should give people a hint about how I intend to call this code?  :-)

Hehe :) all you needed to do was cast the value as a short , Kerry spotted it too( but with no debugger)  :-o

Chuck Gabriel

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #9 on: May 15, 2008, 09:22:16 PM »
WooHoo!!!  That's got it.  Thanks guys.  Y'all are the greatest!

It's always something in the minutia that bites me.  It seems to me that some of the "low level" languages are actually much more forgiving than .NET.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Oh the joys of a "high level" language!
« Reply #10 on: May 15, 2008, 10:03:11 PM »
Code: [Select]
It seems to me that some of the "low level" languages are actually much more forgiving than .NET. for me too. 

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8709
  • AKA Daniel
Re: Oh the joys of a "high level" language!
« Reply #11 on: May 15, 2008, 10:10:04 PM »
C# (.NET) is strict by design, it’s to prevent security flaws, buffer overruns and such.

Glenn R

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #12 on: May 16, 2008, 04:33:05 AM »
Chuck,

Similar short of thing :) happened in your 'C# HotKeys' thread if memory serves and the solution then was a cast to short as well.

Glad it worked for you and good to see you having a dabble again.

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Oh the joys of a "high level" language!
« Reply #13 on: May 16, 2008, 04:49:41 AM »
Chuck,

Similar short of thing :) happened in < snip>

very punny !

 :-)

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Oh the joys of a "high level" language!
« Reply #14 on: May 16, 2008, 04:59:29 AM »
 :lmao: