Core Function DotNet

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(Example)
Line 65: Line 65:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 +
use("System");
 
$CurDir = %Environment->$CurrentDirectory;
 
$CurDir = %Environment->$CurrentDirectory;
 
echo "CurDir is: $CurDir\n";
 
echo "CurDir is: $CurDir\n";
 +
</syntaxhighlight>
 +
 +
Example of using .NETs String
 +
 +
<syntaxhighlight lang="sputnik">
 +
use("System");
 +
$str = %String->new("Hello"); // Sputnik will convert "" to char[] if needed
 +
printf("Char 0 is: %s\n", $str[0]);
 +
say("String value is " . $str);
 +
say("StringLen value is " . $str->$Length);
 +
$substr = $str->Substring(1);
 +
say("String value (with substring(1)) is $substr");
 +
// Prints
 +
// Char 0 is: H
 +
// String value is Hello
 +
// StringLen value is 5
 +
// String value (with substring(1)) is ello
 +
 +
// Lets try a different creation type
 +
$str = %String->new('T', 20);
 +
say("String value is " . $str);
 +
say("StringLen value is " . $str->$Length);
 +
// Prints
 +
// String value is TTTTTTTTTTTTTTTTTTTT
 +
// StringLen value is 20
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 11:00, 18 September 2013

%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.

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.

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 String

use("System");
$str = %String->new("Hello"); // Sputnik will convert "" to char[] if needed
printf("Char 0 is: %s\n", $str[0]);
say("String value is " . $str);
say("StringLen value is " . $str->$Length);
$substr = $str->Substring(1);
say("String value (with substring(1)) is $substr");
// Prints
// Char 0 is: H
// String value is Hello
// StringLen value is 5
// String value (with substring(1)) is ello
 
// Lets try a different creation type
$str = %String->new('T', 20);
say("String value is " . $str);
say("StringLen value is " . $str->$Length);
// Prints
// String value is TTTTTTTTTTTTTTTTTTTT
// StringLen value is 20
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox