Author Topic: Passing a Variable between Classes?  (Read 1644 times)

0 Members and 1 Guest are viewing this topic.

BlackBox

  • King Gator
  • Posts: 3770
Passing a Variable between Classes?
« on: November 28, 2011, 02:22:32 PM »
I am trying to write a LispFunction Method that will Return the KeyCode.ToString value of a Windows Form KeyDown Event.

Why use the Windows Form?
Because I am new to .NET, and this is the only way I knew how to successfully access the KeyDown Event. If there's a better way to accomplish this - PLEASE let me know.

This code works well in a Windows Form solution (VB.NET, Form1 Class), but fails when attempting to pass the value to the myKey variable in the FooClass Class:

Code: [Select]
Public Class Form1
    ' I changed this from Sub to Function in an attempt to Return
    ' the KeyCode.ToString value to the LispFunction Method
    Private Function Form1_KeyDown(ByVal sender As System.Object, _
                                   ByVal e As System.Windows.Forms.KeyEventArgs) _
                               Handles MyBase.KeyDown
        Dim bHandled As Boolean = False
        'myKey = e.KeyCode.ToString ' <-- This is where I am confused
        Debug.Print(e.KeyCode.ToString) ' <-- For Debug only
        e.Handled = True
        Me.Close()
    End Function
End Class

... I am unable to figure out how to pass this variable back to the Root namespace (and Class) for my Add-In, so that it can be Returned from the original Method.

Code: [Select]
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports System

Namespace FOO
    Public Class FooClass

        Private myKey As String = ""  ' <-- Public, Private, nor Shared seems to work

        <LispFunction("FOO")> _
        Public Function FOO(ByVal args As ResultBuffer)
            ' Call Windows Form to initialize the KeyDown Event
            ' KeyDown Event received, and Windows Form Closed
            ' Pass KeyCode.ToString back to this function
            ' Return KeyCode.ToString As String
        End Function

    End Class
End Namespace

Any help would be greatly appreciated.
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: Passing a Variable between Classes?
« Reply #1 on: November 28, 2011, 03:40:11 PM »
Re-posting code in C# for the majority (used code converter):

Code: [Select]
public class Form1
{
// I changed this from Sub to Function in an attempt to Return
// the KeyCode.ToString value to the LispFunction Method
private object Form1_KeyDown(System.Object sender, System.Windows.Forms.KeyEventArgs e)
{
bool bHandled = false;
//myKey = e.KeyCode.ToString;  // <-- This is where I am confused
Debug.Print(e.KeyCode.ToString());  // <-- For Debug only
e.Handled = true;
this.Close();
}
public Form1()
{
KeyDown += Form1_KeyDown;
}
}

Code: [Select]
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

namespace FOO
{
public class FooClass
{

private string myKey = ""; // <-- Public, Private, nor Shared seems to work

[LispFunction("FOO")]
public object FOO(ResultBuffer args)
{
// Call Windows Form to initialize the KeyDown Event
// KeyDown Event received, and Windows Form Closed
// Pass KeyCode.ToString back to this function
// Return KeyCode.ToString As String
}

}
}
"How we think determines what we do, and what we do determines what we get."

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Passing a Variable between Classes?
« Reply #2 on: November 28, 2011, 03:59:14 PM »
Hi,

One way to pass a value from a class to another is to use a class member.
While you're creating an instance of the Form1 class in the Foo method (function), you can acces to the public (or internal if both class are in the same namespace) members of the Form1 class.
A traditional (and safe) way is to use private fields (class global variables sort of) and encapsulate them with public instance properties (which can be read only or read/write).

Here's a little C# sample from what you posted

Code: [Select]
using System.Windows.Forms;

namespace Foo
{
    public partial class Form1 : Form
    {
        // private field (only declared)
        private string _key;

        // public read only property
        public string Key
        {
            get { return _key; }
        }

        // constructor
        public Form1()
        {
            InitializeComponent();
        }

        // KeyDown event handler
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // initilize the '_key' field
            _key = e.KeyCode.ToString();
            this.Close();
        }
    }
}

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

namespace Foo
{
    public class FooClass
    {
        [LispFunction("foo")]
        public TypedValue Foo(ResultBuffer resBuf)
        {
            // create a new instance of Form1
            Form1 form = new Form1();

            // show the form
            Application.ShowModalDialog(form);

            // get the Form1.Key property value
            string str = form.Key;

            // return the result
            if (string.IsNullOrEmpty(str))
                return new TypedValue((int)LispDataType.Nil);
            else
                return new TypedValue((int)LispDataType.Text, str);
        }
    }
}
Speaking English as a French Frog

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Passing a Variable between Classes?
« Reply #3 on: November 28, 2011, 04:06:50 PM »
There are a couple of solutions that jump right out at me:

1) Add a property in the calling class i.e.
Code: [Select]
       Public WriteOnly Property Key() As String
            Set(ByVal value As String)
                myKey = value
            End Set
        End Property

then set that property from your form ...

OR

2) Create the form as an object in the calling class and create a read-only property in the form.

Code: [Select]
'Form code
         Private myKey As String

         Private Function Form1_KeyDown(ByVal sender As System.Object, _
                                   ByVal e As System.Windows.Forms.KeyEventArgs) _
                               Handles MyBase.KeyDown
                 Dim bHandled As Boolean = False
                 myKey = e.KeyCode.ToString
                 e.Handled = True
                 Me.Hide() '<<---- hide the form, don't close
         End Function

         Public ReadOnly Property Key() As String
            Get
                Return myKey
            End Get
        End Property

In the calling class

Code: [Select]
Public Class FooClass

         <LispFunction("FOO")> _
        Public Function FOO(ByVal args As ResultBuffer) As String
            Dim myForm As New Form1 '<<--- new form
            myForm.Show()  '<<---------show the form
            Dim KeyCode As String '<<--------results variable
            KeyCode = myForm.Key() '<<---------save results
            myForm.Dispose() '<<--------dispose of the form
            Return KeyCode '<<-------return the value
        End Function

End Class
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

BlackBox

  • King Gator
  • Posts: 3770
Re: Passing a Variable between Classes?
« Reply #4 on: November 28, 2011, 04:16:13 PM »
Gile, Keith - I really appreciate the prompt responses, and will be able to test the code tonight after work.

Cheers! :beer:
"How we think determines what we do, and what we do determines what we get."