次の方法で共有


Properties in J#...

J# supports writing properties but syntax is quite different from C#. In C# properties have there own syntax and style but in J#, properties look like a regular method except following differences...

  • A method name representing a property getter must start with get_ and a property setter with set_ .      
  • A method representing a property getter/setter must have /** @property */ Attribute applied to it.

If any of these conditions is not fulfilled then J# compiler will treat the declaration as simple method declaration and will emit the IL(Intermediate Language) for a method only. What does that mean? This means that if you refer an assembly containing such a wrongly declared property, in a C#/VB/any other .net language project, then you will not see a method not the property.

 

Syntax of a Property in J# - How to Declare a Property

 

Here is the declaration for a typical property in J#...

 

package JSProperty;

/**

 * Summary description for Program

 */

public class Program

{

      private String name;

      /** @property

      */

      public String get_Name()

      {

            return name;

      }

      /** @property

      */

      public void set_Name(String value)

      {

            name = value;

      }

      public static void main(String[] args)

      {

            //

            // TODO: Add code to start application here

            //

      }

}

 

In the above declaration, you can notice...

  1.  Method name for getter starts with get_

  2.  Method name for setter starts with set_

  3.  Both methods have      /** @property */ attribute applied.

  4.  Method name for setter/getter are same except get_ and set_ . This is mandatory if you want to declare a read/write property. If you want to declare a read-only or write-only property then you must not specify setter and getter respectively.

 

Consuming J# declared Properties in C# - No difference from C# declared property - Beauty of .net

 

If I compile above code into an assembly and refer that assembly in a C# project then I will be able to consume the property in C# syntax, as if the property was written in C# syntax, not typical method like syntax of J#. Following is the C# sample consuming above property...

 

using System;

using System.Collections.Generic;

using System.Text;

using JSProperty;

namespace ConsoleApplication1

{

    class TestProgram

  {

        public void ConsumeProperty()

        {

            Program prg = new Program();

            prg.Name = "Jaiprakash";

            String name = prg.Name;

           

        }

    }

}

 

Declaring a read-only or write-only property in J#

 

If you want to declare a read-only or write-only property then you must not specify setter and getter respectively.  So a read-only property will look somewhat like following...

      /** @property

      */

      public void get_Name(String value)

      {

            name = value;

      }

Similarly if you want to declare a write only property then you should declare only setter,

   

      /** @property

      */

      public void set_Name(String value)

      {

            name = value;

      }

 

/** @property */ attribute is critical - Few words of caution

 

In my J# declaration of property if I miss the @property attribute then J# compiler won't give any error. It will compile fine and will emit the IL for method of the same name. For example if I declare the property as follows...

 

            public String get_Name()

      {

            return name;

      }

      /** @property

      */

      public void set_Name(String value)

      {

            name = value;

      }

 

then J# compiler will generate IL for a method get_Name() and a write-Only property Name. That means if you refer such an assembly in a C# project then, you will see one method "String get_Name()" and a write-only property "property String Name".

 

I have observed many people scratching their head due to same mistake. Either they forget to put the /** @property */ attribute or they spell it wrongly. If you spell the attribute incorrectly then also compiler will not give any error/warning. Reason is that attributes in J# are written inside /** */ construct and you can write java doc comments/comments also inside the same construct. So in case you spell the attribute incorrectly then J# compiler treats that as a comment/doc comment and doesn't give any warning/error.

 

Consuming C# written properties in J#

Suppose you have referred a .net assembly, originally written in C#, and you are referring that in a J# project. In J# you can’t access them as you do in C#. In J# a

get_XXXXX method is exposed for getter of property and a set_XXXXX method is exposed for setter.

 

For example if you have written following code in C#...

using System;

using System.Collections.Generic;

using System.Text;

namespace CSproperty

{

    public class CSproperty

    {

        public CSproperty()

        {

        }

        private String address;

        public String Address

        {

            get

            {

  return address;

            }

            set

            {

                address = value;

            }

        }

    }

}

In J# project you can consume it as following snippet shows…

package JSProperty;

/**

 * Summary description for Program

 */

public class TestProgram

{

     

     

      public static void main(String[] args)

      {

            CSproperty.CSproperty csProp = new CSproperty.CSproperty();

            csProp.set_Address("Microsoft");

            String address = csProp.get_Address();

      }

}

 

Reason Behind this difference from C#

Obviously we want to make it simple for those who are familiar with J# syntax. We don't want to make them learn a new syntax for something which they are habitual of writing in some particular syntax

So, this was all about properties in J#. I will recommend having a look at the IL generated by J# compiler for properties. Change the declaration, like remove the attribute or misspell the attribute, build the code and check the IL using ILDasm. It will help you understand the behavior I have described above.

 

I my next post I will be discussing bean style properties in J#, so stay tuned!!!