Partager via


Property and Event Exposure 

You can create a component in Visual J# that exposes properties and events, and then write a program in another language, such as Visual Basic, to consume this component.

Example

The following is the J# code to create a component that can be consumed by other languages.

// vjc_container.jsl
// compile with: /target:library
import System.Collections.ArrayList;

/** @delegate */
public delegate void ValueChanged();

public class Container
{
    private ArrayList al = new ArrayList();
    
    // Read-only property Count
    /** @property */
    public int get_Count()
    {
        return al.get_Count();
    }
 
    /** @property */
    public void set_Value(int i, System.String val)
    {
        al.Insert(i, val);
        if (ev != null)
        {
             ev.Invoke(); // Raise event.
        }
    }
 
    /** @property */
    public System.String get_Value(int i)
    {
        return (System.String) al.get_Item(i);
    }
 
    public ValueChanged ev = null;
 
    /** @event */
    public void add_Event(ValueChanged p)
    {
        ev = (ValueChanged) System.Delegate.Combine(ev, p);
    }
 
    /** @event */
    public void remove_Event(ValueChanged p)
    {
        ev = (ValueChanged) System.Delegate.Remove(ev, p);
    }
}

The following is a Visual Basic client that consumes the properties and sinks the event fired by the Visual J# component.

' client.vb
' compile with: /reference:vjc_container.dll
Imports System
 Public Class Form
    WithEvents Dim c As New Container()
     
    Public Shared Sub Main() 
        Dim f As New Form
    End Sub 
 
    Public Sub New()      
        c.Value(0) = "String1"
        c.Value(1) = "String2"

        ' Fetching read-only property
        System.Console.WriteLine(c.Count)
    End Sub

    ' Sink Visual J# event
    Public Sub EvListener() Handles c.Event
        System.Console.WriteLine("Visual J# Event Fired")
    End Sub
End Class

Output

Visual J# Event Fired
Visual J# Event Fired
2

See Also

Reference

Syntax for Targeting the .NET Framework
Definition and Use of Events
Defining and Using Properties