The J wiki has a nifty page about integrating J with a .Net application.
http://www.jsoftware.com/jwiki/Guides/.NET_Interop

However, I felt that the J support class was too verbose.  Here's my
current version of the code -- it seems to work just fine for me, though
I've changed the interface to the class somewhat.  (In VB, for example,
you'd use the J.Item("name") psuedo-method to access a J
variable, and J.Item("name")= to set a J variable.  In C#, of course,
you would use J["name"] or J["name"]= to get or set a J variable.)

Other than that... does anyone notice if I overlooked (or clipped out)
any useful aspect of the original class definition?

Also -- given the current J interpreter, I think this class should follow
a singleton pattern -- the dispose/finalize mechanism currently
implemented is not very useful from my point of view.  However, I'm
not certain whether the current debug behavior of this system (where
the J console stays up even after the program exits) is a useful
feature or something better discarded.  Can someone with more j.net
experience than I weigh in on that issue?

Thanks,

--
Raul

P.S. here's a snapshot of my JSoftware/Session.cs:

using System;
using System.Collections.Generic;
using System.Text;
using JEXEServerLib;
using JSoftware.Properties;
using System.ComponentModel;

namespace JSoftware {
   public class Session :IDisposable {
       private JEXEServerClass _jObject;
       private bool _debug;
       public Session(bool debug) {
           _debug= debug;
           _jObject = new JEXEServerClass();
           _jObject.Quit();
           _jObject.Log(1);
           _jObject.Show(1);
           Do("18!:4 <'z'");
           this["baseScript"]=
UnicodeEncoding.UTF8.GetString(Resources.jscripts);
           if (Debug)
               Do("baseScript=: baseScript,(10{a.),'showJ
i.0',(10{a.),'loadprofile i.0'");
           this["loadScript"]= Resources.ScriptLoader;
           Do("0!:100 loadScript");
       }
       public Session() : this(false) { }
       public bool Debug { get { return _debug; } }

       public void SetM(string name, int jType, int jRank, int[]
jShape, int[] jData) {
           _jObject.SetM(name, ref jType, ref jRank, ref jShape[0],
ref jData[0]);
       }

       public object this[string name] {
           get { object retValue;
               _jObject.GetB(name, out retValue);
               return retValue;
           }
           set { _jObject.SetB(name, ref value); }
       }

       public void Do(string command) {
           int result= _jObject.Do(command);
           if (0<result) { object errorMessage;
               _jObject.ErrorText(result, out errorMessage);
               throw new
Exception(UnicodeEncoding.UTF8.GetString((byte[])errorMessage));
           }
       }

       public void Load(string fileName) {
           this["script2load"]= fileName;
           Do("0!:0"+(Debug ?"01" :"")+"<script2load");
       }

       #region Dispose/Finalize
       private bool _disposed= false;
       ~Session() { ((IDisposable)this).Dispose(); }
       void IDisposable.Dispose() {
           if (!_disposed) {
               _jObject.Quit();
               _jObject= null;
               GC.Collect();
           }
           _disposed= true;
       }
       #endregion
   }
}
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to