Passing Reference-Type Arguments

Passing Reference-Type Arguments

With javOnet, you can very easily pass any .NET object as argument to any .NET method, or substitute a .NET field or property. Instances of .NET objects can be stored in your Java application using the special NObject type variable. When passing the NObject object to the .NET method, Javonet just pass it as a reference.

	NObject size = Javonet.New("System.Drawing.Size");
	size.set("Width", 500);
	size.set("Height", 100);

	NObject label = Javonet.New("System.Windows.Forms.Label")
	label.set("Size", size);

As you see you can create an instance of the Size object, set its “Width” and “Height” properties, and pass it into the Size property on the Label object. This way you can pass the object as an argument to any method or field and property.


	NObject nowDateObj= Javonet.getType("DateTime").get("Now");

	NObject date = Javonet.New("DateTime",1980,1,1);
	NObject datesDiff = nowDateObj.invoke("Subtract",date);

Here the “Now” field gets a value from DateTime. Then we create a new DateTime value, passing a date of 1980-01-01 as constructor arguments, then we subtract these dates. The datesDiff variable will store an instance of the TimeSpan object that holds the difference between now and 1980-01-01.

See Live Example!