Core Function DotNet

From Sputnik Wiki
Revision as of 11:12, 18 September 2013 by UberFoX (Talk | contribs)
Jump to: navigation, search
%Name

Description

Create/use .NET objects and call instance/static method and get/set properties.

Sputnik is made in C# and C# is a .NET language.

Sputnik keeps all the .NET stuff hidden away and out of view and provides its own functions and features for you to use.

However if you wish to use something from .NET Sputnik has a special operator that lets you do that.

You can use the % operator with an Identifier to specify the creation/use of a .NET class see the examples below.

This .NET object support is by no means complete it is never meant to be 100% since if you wanted 100% you wouldn't be using Sputnik however it is good enough to provide a layer of support should you think Sputnik is missing something that can be provided by something within the .NET library.

Of course you can look into DLLImport() (To call functions within DLLs and WinAPIs etc) to extend Sputniks feature set should you think something is missing.

As for .NET DLLs you will have to specify them with Use() and create the objects using the % operator.

Remarks

To use .NET stuff you must first define where the object in question can be found in .NET you do that using the use() function.

Then you can create the object using the % opeator.

The .NET objects you create support their operator overloads such as += *= etc so this makes using .NET classes much easier almost seamless.

Note - When you use strings it uses Sputnik escapes and not C#/.NET ones (Which is fine since Sputnik has them all anyway and a lot more).

Example

Example of using .NETs Console.WriteLine() function

use("System");
%Console->WriteLine("Hello World!");
// Prints: Hello World!

Example of using .NETs StringBuilder class

use("System");
use("System.Text");
//$sb = %StringBuilder->new(100); // We could pre-allocate
$sb = %StringBuilder->new();
$sb->Append("Cat");
$sb->Append("Dog");
 
%Console->WriteLine("StringBuilder Text: " . $sb);
%Console->WriteLine("StringBuilder Length: " . $sb->$Length);
// Prints:
// StringBuilder Text: CatDog
// StringBuilder Length: 6
 
// Now lets edit it
$sb[0] = 'P';
say("Character 0 = " . $sb[0]);
say("Character 3 = " . $sb[3]);
%Console->WriteLine("StringBuilder Text: " . $sb);
%Console->WriteLine("StringBuilder Length: " . $sb->$Length);
// Prints
// Character 0 = P
// Character 3 = D
// StringBuilder Text: PatDog
// StringBuilder Length: 6

Example of using .NETs Static Environment class

use("System");
$CurDir = %Environment->$CurrentDirectory;
echo "CurDir is: $CurDir\n";

Example of using .NETs Point Class

use("System.Drawing");
 
$a = %Point->new(2, 4);
say($a->ToString());
$a->$X = 77;
$a->$Y = 20;
say( "Value is: " . $a->$X );
say( "Value is: " . $a->$Y );
// Prints:
// {X=2,Y=4}
// Value is: 77
// Value is: 20

Example of using .NETs Char[]

use("System");
 
$chars = %Char[]->new(3);
$chars[0] = 'A';
$chars[1] = 'B';
$chars[2] = 'C';
printf("Char is: %s\n", $chars[0]);
printf("Char is: %s\n", $chars[1]);
printf("Char is: %s\n", $chars[2]);
$str = %String->new($chars);
printf("String: %s\n", $str);
// Finally make a .NET String from the Char[]
$a = %String->new($chars);
printf("String value is %s\n", $a);
printf("StringLen value is %d\n", $a->$Length);
// Prints
// Char is: A
// Char is: B
// Char is: C
// String: ABC
// String value is ABC
// StringLen value is 3

Example of using Sputniks built in .NET Vector3 class

// Make note of operator overloading of the .NET object
// is available here as if it was a Sputnik class
$v1 = %Vector3->new(10, 20, 30);
$v2 = %Vector3->new(2, 4, 6);
say("V1: " . $v1);
$v1 += $v2;
say("V1: " . $v1);
say("V1 Length: ". $v1->$Length);
$v1->CrossProduct($v2);
say("V1: " . $v1);
// Prints:
// V1: (10, 20, 30)
// V1: (12, 24, 36)
// V1 Length: 26.83282
// V1: (12, 24, 36)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox