Share via


Defining and Using Properties 

Visual J# allows you to access properties defined in .NET Framework classes. Visual J# translates properties in .NET Framework classes to the accessor methods: get_ and set_. Visual J# developers simply call the accessor methods directly. Visual J# does not attach any special importance to property accessors. They are treated as methods of the class.

Example

// vjc_properties.jsl
// compile with: /reference:System.Windows.Forms.dll
import System.Windows.Forms.*;
public class MyClass
{
    public static void main(String [] args)
    {
        Button b = new Button();
        String str = b.get_Text();
        System.Console.WriteLine("Button Text = " + str);
        b.set_Text("Sample Button");
        str = b.get_Text();
        System.Console.WriteLine("Button Text = " + str);
    }
}

public class MyProp
{
    private int i = -1;

    /** @property */
    public int get_SomeNum()
    {
        return i;
    }

    /** @property */
    public void set_SomeNum(int input)
    {    
        i = input;
    }

    public static void main()
    {
        MyProp aprop = new MyProp();
        System.Console.WriteLine(aprop.get_SomeNum());
        aprop.set_SomeNum(8);
        System.Console.WriteLine(aprop.get_SomeNum());
    }
}

The following sample creates two properties with the same name:

public class PropTest
{
    private int i = -1;
    private int j = -2;

    /** @property */
    public int get_SomeNum()
    {
        return i;
    }

    /** @property */
    public void set_SomeNum(int input)
    {    
        i = input;
    }

    /** @property */
    public int get_SomeNum(int i)
    {
        return j;
    }

    /** @property */
    public void set_SomeNum(int i, int input)
    {    
        j = input;
    }

    public static void main(String [] args)
    {
        int i = 0;
        PropTest aprop = new PropTest();
        System.Console.WriteLine(aprop.get_SomeNum());
        System.Console.WriteLine(aprop.get_SomeNum(i));
        aprop.set_SomeNum(8);
        aprop.set_SomeNum(i,9);
        System.Console.WriteLine(aprop.get_SomeNum());
        System.Console.WriteLine(aprop.get_SomeNum(i));
    }
}

The following sample shows a Visual J# component with a property and shows how the property is consumed first by a Visual J# client and then by a C# client.

// vjc_prop_use.jsl
// compile with: /target:library
public class PropTest
{
    private int i = -1;

    /** @property */
    public int get_SomeNum()
    {
        return i;
    }

    /** @property */
    public void set_SomeNum(int input)
    {
        i = input;
    }
}

The following sample shows a Visual J# client that consumes the properties. To compile this sample, reference vjs_prop_use.dll.

// Consumer_1.jsl
// compile with: /reference: vjs_prop_use.dll
public class Test
{
    public static void main(String [] args)
    {
        int i = 0;
        PropTest aprop = new PropTest();
        System.Console.WriteLine(aprop.get_SomeNum());
        aprop.set_SomeNum(8);
        System.Console.WriteLine(aprop.get_SomeNum());
    }
}

The following sample shows a Visual C# client that consumes the properties. To compile this sample, reference vjs_prop_use.dll.

// Consume2.cs
// compile with: /reference: vjs_prop_use.dll
public class Test
{
    public static void Main()
    {
        PropTest aprop = new PropTest();
        System.Console.WriteLine(aprop.SomeNum);
        aprop.SomeNum = 8;
        System.Console.WriteLine(aprop.SomeNum);
    }
}

The following is an example of a Visual J# component that defines an indexed property. The following Visual C# sample shows how to consume the property.

// vjc_indexed_properties.jsl
// compile with: /target:library
import System.*;
import System.Reflection.*;
class Employee 
{
    public Employee(System.String s, int d) 
    {
        _name = s;
        _dept = d;
    }

    /** @property */
    System.String get_name()
    {
        return _name;
    }
    
    /** @property */
    int get_dept()
    {
        return _dept;
    }

    private System.String _name;
    private int _dept;
}

/** @attribute DefaultMember("Report") */
// Sets the Report property as the indexer for the class.
class Manager 
{
    /** @property */
    public Employee get_Report(System.String s) 
    {
        for (pEmp = Reports ; (pEmp!=null) && (pEmp.emp.get_name() != s)
            ;
        pEmp = pEmp.next);
        if (pEmp!=null)
            return pEmp.emp;
        else
            return null;
    }

    /** @property */
    public void set_Report(System.String s, Employee e) 
    {
        for (pEmp = Reports ; (pEmp!=null) && (pEmp.emp.get_name() != s)
            ;
        pEmp = pEmp.next);
        if (pEmp==null) 
        {
            EmpList emp1 = new EmpList(); 
            emp1.emp = e; 
            emp1.next = Reports; 
            Reports = emp1;
        }
    }

    private static class EmpList     {
        public Employee emp;
        public EmpList next;
    }

    EmpList pEmp;
    static EmpList Reports = null;
}

The following Visual C# sample consumes the property defined in the previous sample. To compile this example, reference vjc_indexed_properties.dll.

// Consume11.cs
// compile with: /reference vjc_indexed_properties.dll
using System;

public class Class1
{
    public static void Main()
    {
        Manager  Ed = new Manager();
        Employee Bob = new Employee("Bob Smith", 12);

        // track Ed's reports
        Ed[Bob.name] = Bob;    // indexed by string type
        Console.WriteLine(Ed[Bob.name].dept);
    }
}

Attributes added to the accessor methods are applied to the property by default. However, you can use the @attribute.method directive to attach the attributes to the individual methods, as follows:

// vjc_properties_and_attributes.jsl
// compile with: /target:library
import System.ComponentModel.*;

/*
 * Different attributes to getter and setter methods
 */
public class Scalar
{
    private int magnitude = 101;

    /**@attribute Description("Attribute on the property")*/
    /**@attribute.method Description("Attribute on get accessor")*/
    /**@property*/
    public int get_Magnitude() 
    {
        return magnitude;
    }

    /**@attribute.method Description("Attrib on set accessor")*/
    /** @property */
    public void set_Magnitude(int value)
    {
        magnitude = value;
    }
}

Output

Button Text =
Button Text = Sample Button

There is also support for defining properties in Visual J#. Two syntaxes are supported: the .NET syntax, and the Bean syntax. For information on the Bean-style property syntax, see Bean-Style Properties. When defining the accessor methods of a property using the .NET syntax, the accessor method name must begin with either get_ or set_. The property must be identified as such using the @property tag as shown in the examples below.

The accessor methods of a property must use the same access modifiers. For example, both may be public, or both private. The type of the property must be the return type of the get_ accessor method and the type of the last argument of the set_ accessor method.

It is possible to override property accessor methods in a derived class.

In Visual J# a simple .NET-style property is defined as follows:

Output

-1
8

A property can be defined as read only, or write only, when just the appropriate accessor (get_ or set_) function is defined. Properties can be overloaded if the method accessors do not conflict with the language rules.

Output

-1
-2
8
9

Output

-1
8

Output

-1
8

Output

12

See Also

Reference

Syntax for Targeting the .NET Framework
Property and Event Exposure