RingDotNetfor Ring

A first look

Meet objects, types, properties, methods, static members, structs, collections and null with a small program that prints what it finds.

What you will learn#

Before you start#

You need Ring and the RingDotNet package. Open a terminal in samples/UsingDotNet/tutorials. If something does not work, read Getting started. If Ring is new to you, read the Quick introduction to Ring in the appendix first.

The program#

Read the program first; the explanations follow. The line using { ... } brings classes in. func main is where Ring starts.

samples/UsingDotNet/tutorials/ch01_first_look.ring
# a first look at .NET from Ring - runtime, types, objects, namespaces
load "dotnet.ring"

using {
	"System"
	"System.Text"
	"System.Collections.Generic"
}

func main

	# 1. The runtime that runs inside your Ring program
	? "Runtime          : .NET " + new Environment().Version.ToString()

	# 2. Everything in .NET is an object of some type (a class)
	oText = new StringBuilder("Hello")
	oType = oText.GetType()
	? "Type of the object: " + oType.FullName
	? "Namespace        : " + oType.Namespace
	? "Is it a class?   : " + oType.IsClass

	# 3. Objects have properties (what they know) and methods (what they can do)
	oText.Append(", .NET")
	? "Property Length  : " + oText.Length
	? "Method ToString(): " + oText.ToString()

	# 4. Some classes are used without creating an object: their members are static
	? "Static Math.Max  : " + new Math().Max(3,9)
	? "Static Math.PI   : " + new Math().PI

	# 5. Structs are small values (a date, a point)
	oDate = new DateTime(2024,12,25)
	? "A DateTime       : " + oDate.Format("yyyy-MM-dd") + " is a " + oDate.DayOfWeek

	# 6. Collections hold many objects
	oNames = new StringList()
	oNames.Add("Ann")
	oNames.Add("Bob")
	? "A list of names  : " + oNames.Count + " items, first is " + oNames.Item(0)

	# 7. null means "no object". The base type of StringBuilder is Object, and Object has no base type: the answer is null
	oMissing = oText.GetType().BaseType.BaseType
	? "Is it null?      : " + oMissing.isNull()

Run it:

Shell
ring ch01_first_look.ring
Output
Runtime          : .NET 9.0.15
Type of the object: System.Text.StringBuilder
Namespace        : System.Text
Is it a class?   : 1
Property Length  : 11
Method ToString(): Hello, .NET
Static Math.Max  : 9
Static Math.PI   : 3.14
A DateTime       : 2024-12-25 is a Wednesday
A list of names  : 2 items, first is Ann
Is it null?      : 1

How it works, step by step#

1. The runtime#

new Environment().Version asks the .NET runtime that runs inside your Ring program for its version (Environment is a class of the System namespace that the using block opened). Nothing is installed "next to" Ring: the runtime is part of your program while it runs.

2. Everything is an object of some type#

new StringBuilder("Hello") creates an object. GetType() is a method that every .NET object has; it returns an object that describes the type. Its properties FullName, Namespace and IsClass tell you what you are holding: a class called System.Text.StringBuilder in the namespace System.Text.

Note

When you pass a value, write the parentheses: new StringBuilder("Hello"). In Ring, init() runs only with them. Without a value both new StringBuilder and new MemoryStream() are fine, but new StringBuilder() with empty parentheses is an error; see Core concepts.

3. Properties and methods#

oText.Append(", .NET") is a method: the object does something (it adds text to itself). oText.Length is a property: it tells you something the object knows. Properties are read without parentheses.

ToString() turns the object into a Ring text. Almost every .NET object has it.

4. Static members#

new Math().Max(3,9) does not need a math object: Max belongs to the class. Ring still writes new Math() to reach it; the object is just a doorway to the class.

Ring prints the number 3.14159... as 3.14 because Ring shows two decimals by default. The value itself is not rounded. Use decimals(5) at the top of a program to see more.

5. Structs#

A DateTime is a struct: a small value. new DateTime(2024,12,25) is a date. Format("yyyy-MM-dd") formats it, and DayOfWeek is an enum, so Ring gets the text Wednesday.

Tip

ToString() has no parameters; Format("yyyy-MM-dd") is its form with a pattern. A .NET method that exists in several forms becomes one Ring method that takes one value, NULL or a list of :name = value pairs; Core concepts explains it.

6. Collections#

StringList holds texts, in order. Add adds one, Count says how many, Item(0) reads the first (the position starts at 0, like in .NET).

7. null#

null is "no object". A method that finds nothing returns it: the type Object has no base type, so oText.GetType().BaseType.BaseType (the base type of the base type of a StringBuilder) is null. isNull() checks it. Ring prints 1 for true.

What you learned#

IdeaExample
a runtime inside your programnew Environment().Version
create an objectnew StringBuilder("Hello")
a propertyoText.Length
a methodoText.Append(", .NET")
a static membernew Math().Max(3,9)
a struct and an enumDateTime, DayOfWeek
a collectionStringList
no objectisNull()

Exercises#

  1. Change the program to print the type of a DateTime object. Which namespace is it in?
  2. Create a StringBuilder and append your name five times with a loop. What is its Length?
  3. Use new Math() to print the square root of 2 and the larger of 7 and 19.
  4. Find Environment.MachineName in the class reference (namespace System) and print it.

Next#

Working with text

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