RingDotNetfor Ring

Identifiers and versions

Guid gives unique identifiers without a central counter, and Version compares version numbers by number and not as text. This lesson uses both, and shows why "1.10" comes after "1.9".

What you will learn#

The program#

samples/UsingDotNet/tutorials/ch04_identifiers.ring
# identifiers and versions - Guid, Version, and sorting version numbers by number and not as text
load "dotnet.ring"

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

func main

	oGuid = new Guid()

	# 1. A Guid is a 128-bit number that is unique for practical purposes: two programs on two computers can make one each and
	#    never meet the same value. It is shown as 32 hex digits in 5 groups. NewGuid is random: only its shape is the same.
	cNew = oGuid.NewGuid().ToString()
	? "1. a new Guid has " + len(cNew) + " characters, with dashes at 9, 14, 19 and 24: " + (cNew[9] = "-" and cNew[14] = "-" and cNew[19] = "-" and cNew[24] = "-")
	? "   two new Guids are different: " + (oGuid.NewGuid().ToString() != oGuid.NewGuid().ToString())

	# 2. Parse and compare. A Guid can be read from text in several layouts; Format("N") has no dashes, "B" has braces.
	oFixed = oGuid.Parse("0f8fad5b-d9cb-469f-a165-70867728950e")
	? nl + "2. parsed: " + oFixed.ToString()
	? "   format N: " + oFixed.Format("N")
	? "   format B: " + oFixed.Format("B")
	? "   Empty is all zeros: " + oGuid.Empty.ToString()
	? "   TryParse of 'hello': success = " + oGuid.TryParse("hello")[1]

	# 3. Version: major.minor.build.revision, compared by NUMBER. As text, "1.10" comes before "1.9"; as Version it is later.
	oVer = (new Version).@New([ :major = 0, :minor = 0 ])  # any Version works as a handle for the static methods
	oV9 = oVer.Parse("1.9.0")
	oV10 = oVer.Parse("1.10.0")
	? nl + "3. as text, '1.10.0' < '1.9.0' is " + (strcmp("1.10.0","1.9.0") < 0)
	? "   as Version, 1.10.0 is later than 1.9.0: " + (oV10.CompareTo(oV9) > 0)
	? "   parts of 1.10.0: major " + oV10.Major + ", minor " + oV10.Minor + ", build " + oV10.Build

	# 4. Sorting versions: a sorted list of Version objects. Sort() uses CompareTo of every Version.
	oList = new List([ :typeArguments = ["System.Version"] ])
	for cText in ["2.0.1","1.10.0","1.2.0","1.9.0","10.0.0"]
		oList.Add(oVer.Parse(cText))
	next
	oList.Sort(NULL)
	cSorted = ""
	aItems = oList.toList()
	for oItem in aItems
		cSorted += oItem.ToString() + " "
	next
	? nl + "4. sorted versions: " + cSorted

	# 5. The version of the running .NET and of an assembly
	? nl + "5. the runtime version is 6 or later: " + (new Environment().Version.Major >= 6)
Output
1. a new Guid has 36 characters, with dashes at 9, 14, 19 and 24: 1
   two new Guids are different: 1

2. parsed: 0f8fad5b-d9cb-469f-a165-70867728950e
   format N: 0f8fad5bd9cb469fa16570867728950e
   format B: {0f8fad5b-d9cb-469f-a165-70867728950e}
   Empty is all zeros: 00000000-0000-0000-0000-000000000000
   TryParse of 'hello': success = 0

3. as text, '1.10.0' < '1.9.0' is 1
   as Version, 1.10.0 is later than 1.9.0: 1
   parts of 1.10.0: major 1, minor 10, build 0

4. sorted versions: 1.2.0 1.9.0 1.10.0 2.0.1 10.0.0 

5. the runtime version is 6 or later: 1
Note

This program uses a few generic functions (dnNew, dnNewGeneric, callMethod) where a class cannot be used. The lesson The generic functions explains them.

How it works#

Guid#

A Guid is a 128-bit number. A new one is random, with so many possible values that two programs on two computers never make the same one in practice, so no server or counter has to hand them out. That makes a Guid a good key for a record that is created offline, a name for a temporary file and an id in a message. NewGuid() gives a new one; the output of step 1 shows only its shape (36 characters with dashes in fixed places), because the value is different at every run.

Parse reads the usual text; Format("N") writes 32 digits without dashes, "B" writes with braces and "D" (the default) with dashes. Guid.Empty is all zeros: use it as "no value". TryParse answers [ success, value ] and does not raise an error for text such as hello (the tutorial Network tools explains out values).

Warning

A Guid is unique, not secret. Do not use it as a password or a security token; use RandomNumberGenerator (the tutorial Protecting and signing messages).

Version#

A Version has Major, Minor, Build and Revision. Its comparison is by number, part after part. Compare with the text: strcmp("1.10.0", "1.9.0") says that 1.10.0 is before 1.9.0, because the character 1 is smaller than 9; as Version objects, 1.10.0 is later, as everybody expects. Whenever a program shows, stores or compares software versions, use Version, not the text.

Version has no static methods that work without an object, so the program makes any Version first (dnNew("Version", [ :major = 0, :minor = 0 ])) and uses it as a handle for Parse.

Sorting versions#

A List<Version> (made with dnNewGeneric) sorts itself with Sort(), which calls CompareTo of every Version. The result is 1.2.0, 1.9.0, 1.10.0, 2.0.1, 10.0.0. Ring's own sort() would give the text order.

The running version#

Environment.Version is the version of the .NET runtime that runs the program; the program prints only a test, because the number depends on your computer. An assembly has its own version (Assembly.GetName().Version), which the tutorial Exploring .NET with reflection can read.

What you learned#

Exercises#

  1. Make 100000 Guids and check that all are different (use a HashSet).
  2. Write isNewer(cA, cB) for two version texts.
  3. Read the version of the bridge assembly (RingDotNet.Bridge) with reflection.
  4. Name a file with a Guid and write a function that finds such files again by their name pattern.

Next#

Undo, redo and queues

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