RingDotNetfor Ring

The generic functions

Nearly everything in .NET has a generated class, and you write object code: new, the dot and braces. A few cases have no class, or a class that cannot be made with new. For them Ring has generic functions: dnNewGeneric, dnType, dnNew, dnStatic, dnNull and dnBuffer, and the methods callMethod and getProp. This lesson shows when each one is needed.

What you will learn#

The earlier lessons wrote new StringBuilder("x"), oText.Append("y"), oText.Length and oForm { Text = "Hello" }. That is the way to use .NET from Ring. The generic functions below do the same work with names written as texts, so a program has to say more, and they are for the few cases where object code does not reach.

The program#

samples/UsingDotNet/tutorials/ch13_generic_calls.ring
# the generic functions - dnType, dnNew, dnStatic, dnNewGeneric, callMethod, getProp, dnNull and dnBuffer: what to use when there is no class
load "dotnet.ring"

using {
	"System"
	"System.IO"
	"System.Collections"
}

func main

	# The tutorials so far wrote OBJECT code: new makes an object, the dot calls a method or reads a property, and braces set properties.
	# That works for every class that was generated, and nearly everything is. A few cases have no class, and for them there are
	# generic functions. They do the same work with the names as texts.

	# 1. A closed generic type with no generated class. LinkedList<string> is not one of the ready classes (StringList and the others are),
	#    so dnNewGeneric makes the object, and the object is a plain DotNetObject: callMethod calls a method and getProp reads a property.
	oLinked = dnNewGeneric("System.Collections.Generic.LinkedList",["System.String"],[])
	oLinked.callMethod("AddLast",["b"])
	oLinked.callMethod("AddLast",["c"])
	oLinked.callMethod("AddFirst",["a"])
	? "1. a LinkedList<string> has " + oLinked.getProp("Count") + " items, the first is " + oLinked.getProp("First").getProp("Value")
	? "   as a Ring list: " + joinList(oLinked.toList())

	# 2. A type that is not wrapped by a class: dnType gives the type, and callMethod calls its static methods (here the class has a
	#    class, so the two ways give the same answer)
	oMath = dnType("System.Math")
	? nl + "2. dnType(...).callMethod: " + oMath.callMethod("Max",[3,9]) + "   the same with a class: " + new Math().Max(3,9)
	? "   a static property: " + oMath.getProp("PI")

	# 3. A constructor with named values. A class that has no constructor without values (FileStream is one) is made without parentheses, (new FileStream),
	#    and @New chooses the constructor by the NAMES of its parameters: (new FileStream).@New([ :name = value ]); dnNew("Class",[ :name = value ]) does the same.
	cFile = new Path().Combine([ new Path().GetTempPath(), "ringdotnet-generic.txt" ])
	new File().WriteAllText([ :path = cFile, :contents = "Hello, generic calls" ])
	oStream = dnNew("FileStream",[ :path = cFile, :mode = "Open", :access = "Read" ])
	? nl + "3. a FileStream made by names: " + oStream.Length + " bytes, can read: " + oStream.CanRead
	oStream.Dispose()
	new File().Delete(cFile)

	# 4. A static method of a class that needs values to make an object, or whose name is also a method of every object: dnStatic gives
	#    the static method directly. Convert.ToString(255, 16) is the text of a number in base 16 (ToString is a method of every object).
	? nl + "4. dnStatic: " + dnStatic("System.Convert","ToString",[ 255, 16 ]) + " (255 in base 16), " + dnStatic("System.Convert","ToString",[ 255, 2 ]) + " (in base 2)"

	# 5. dnNull is the .NET null, for a method that expects null: here an ArrayList gets a null item
	oList = new ArrayList()
	oList.Add("text")
	oList.Add(dnNull())
	? nl + "5. an ArrayList with a null item has " + oList.Count + " items; the second is null: " + oList.Item(1).isNull()

	# 6. dnBuffer is a byte[] that .NET can fill (see the tutorial about binary files): the generic function for a type that Ring cannot
	#    make by itself
	oBuffer = dnBuffer(8)
	? nl + "6. a buffer of " + dnBufferSize(oBuffer) + " bytes"

	# 7. When to use what: first an object (new, dot, braces). If a class has no constructor without values, dnNew with names. If a name
	#    is a Ring word or also a method of every object, dnStatic or callMethod. If there is no class, dnNewGeneric, callMethod, getProp.
	? nl + "7. the same call in two styles: " + new Math().Max(3,9) + " and " + dnStatic("System.Math","Max",[3,9])

func joinList aList

	cText = ""
	for item in aList
		if cText != ""
			cText += ", "
		ok
		cText += "" + item
	next
	return cText
Output
1. a LinkedList<string> has 3 items, the first is a
   as a Ring list: a, b, c

2. dnType(...).callMethod: 9   the same with a class: 9
   a static property: 3.14

3. a FileStream made by names: 20 bytes, can read: 1

4. dnStatic: ff (255 in base 16), 11111111 (in base 2)

5. an ArrayList with a null item has 2 items; the second is null: 1

6. a buffer of 8 bytes

7. the same call in two styles: 9 and 9

How it works#

A generic type with no class#

List<T>, Dictionary<K,V> and some others have ready classes for the common item types (StringList, NumberDictionary...). A type such as LinkedList<string> has no class, so dnNewGeneric("System.Collections.Generic.LinkedList", ["System.String"], []) makes the object, and what comes back is a plain DotNetObject. A plain object has no named methods, but it has two general ones: callMethod("AddLast", ["b"]) calls a method and getProp("Count") reads a property. toList() copies a collection into a Ring list. The result of getProp("First") is another plain object (a node), and its Value is read the same way.

The type object#

dnType("System.Math") is the type itself, and callMethod on it calls static methods; getProp("PI") reads a static property. With a class the same thing is new Math().Max(3, 9) and new Math().PI: Ring makes a harmless object to reach the static members. Use dnType for a type that no class wraps, or whose namespace you did not name in using.

Named values for a constructor#

A class made with new X(...) runs its init, and init has one form. A class that has no constructor without values (FileStream must have a path and a mode) cannot be made with new FileStream(), and a constructor with several values needs a way to choose among the constructors: the names of their parameters. dnNew("FileStream", [ :path = cFile, :mode = "Open", :access = "Read" ]) does it, and the values can also be given in order: dnNew("TimeSpan", [1, 2, 3]). For a class that does have a constructor without values, the object form is new MemoryStream().@New([ :buffer = bytes ]), which the tutorials use first.

A static method by name#

dnStatic("System.Convert", "ToString", [255, 16]) calls the static method Convert.ToString(255, 16): the text of a number in base 16. Here the method is also named like the ToString that every object has, which Ring uses for its own toString(), so the generated class keeps that name for the object and the static method needs the generic call. The same is true for a class that needs values to make an object and for a static method of a type that has no class.

Null and buffers#

dnNull() is the .NET null, for a parameter or a property that must be set to null (oPictureBox.Image = dnNull(); the program adds null to an ArrayList). A method that returns null gives an object that answers isNull(). dnBuffer(n) is a byte[] that .NET can fill; it was introduced with Stream.Read in the tutorial Binary files.

Choosing#

  1. First write an object: new, the dot, braces.
  2. A constructor that needs values, or a class without a constructor without values: dnNew("Class", [ :name = value ]).
  3. A static method with a name that every object has, or a type that no class wraps: dnStatic or dnType.
  4. An object that has no class (a generic type, or a type that was not wrapped): dnNewGeneric or the object that came back, with callMethod and getProp.
  5. A method that has several forms, when you want to write its values in order and not by name: callMethod("SetValue", ["Port", 8080, "DWord"]) (the object code form is SetValue([ :name = "Port", :value = 8080, :valueKind = "DWord" ])).
Note

The reference of all these functions is in Core functions, and Core concepts explains how class names, overloads and properties look. If a class that you need is not wrapped, Your own assemblies shows how to generate classes for it.

What you learned#

Exercises#

  1. Make a HashSet<string> with dnNewGeneric, add three names and print Count.
  2. Call Math.Round(2.5) with dnStatic and with a class, and compare the answers.
  3. Open a FileStream with :mode = "Create" and write a few bytes into it.
  4. Read the Count of a Queue<string> made with dnNewGeneric after two Enqueue calls.

Next#

A NuGet package

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.