Show/Hide Toolbars

XSharp

Navigation: X# 示例

Vulcan Runtime (BYOR)

Scroll Prev Top Next More

这个 “自带运行时”(Bring Your Own Runtime--BYOR)示例展示了如何在 X# 中使用 Vulcan 数据类型和函数
 
该示例不包含 Vulcan Runtime 动态链接库。您需要在机器上安装它。您可能需要更新示例中的引用,并将其指向你机器上的 Vulcan Runtime
 
// 请阅读 Readme.txt 中的注释 !
using System
using System.Collections.Generic
using System.Linq
using System.Text
 
Function Start() as void
 
  LOCAL startingColor AS ConsoleColor
  startingColor := Console.ForegroundColor
 
  ConsoleHeading("自带运行时(BYOR)示例",ConsoleColor.Magenta)
  Console.ForegroundColor := ConsoleColor.Gray
 
  DateSamples()
  Conversion()
  Strings()
  Numeric()
  Wait()
  Arrays()
  Symbols()
  LateBinding()
  Wait()
 
  Workarea()
  Wait()
  Macros()
  Wait()
  CodeBlocks()
  Wait()
 
  Console.ForegroundColor := startingColor
 
FUNCTION ConsoleHeading(s AS STRING, c := ConsoleColor.Yellow AS ConsoleColor) AS VOID
  LOCAL originalColor AS ConsoleColor
  originalColor := Console.ForegroundColor
 
  Console.ForegroundColor := c
  Console.WriteLine(s)
  Console.ForegroundColor := originalColor
 
FUNCTION Wait() AS VOID
  ConsoleHeading("任意键继续...",ConsoleColor.Green)
  Console.ReadKey()
 
 
FUNCTION DateSamples() AS VOID
  ConsoleHeading("Dates")
  LOCAL dToday := Today() AS DATE
  Console.WriteLine( i"今天是 {dToday:d}"   )   // 插值字符串,短日期格式
  Console.WriteLine( i"今天是 {dToday:G}"   )   // 插值字符串,一般日期格式
 
  LOCAL dTomorrow AS __VODATE
  dTomorrow := dToday + 1 // 可以执行 VO 日期运算
  Console.WriteLine( i"明天是 {dTomorrow:D}") // 长日期格式  
 
  LOCAL dNewYear AS DATE
   dNewYear := 2016.01.01
 
   Console.WriteLine("New year was " +AsString(dNewYear)+ " on a "+CDOW(dNewyear))
 
FUNCTION Conversion() AS VOID
  ConsoleHeading("Conversion")
  LOCAL sToday AS STRING
  sToday := DTOS(Today()) // 使用 VO 函数转换日期
  Console.WriteLine(i"Today is {sToday}")
 
FUNCTION Strings() AS VOID
  ConsoleHeading("Strings")
  LOCAL s AS STRING
  LOCAL sToday := DTOS(Today())
  s := SubStr(sToday,1,4) // 字符串操作
  Console.WriteLine(String.Format("SubStr is {0}",s))
 
  LOCAL n AS DWORD
  n := At("0",s)
  Console.WriteLine(String.Format("At is {0}",n))
 
  LOCAL c := "" AS STRING
  Console.WriteLine(String.Format("c is {0}",IIF(Empty(c),"Empty","Not empty")))
  c := "x"
  Console.WriteLine(String.Format("c is {0}",IIF(Empty(c),"Empty","Not empty")))
  RETURN
 
FUNCTION Numeric() as VOID
  ConsoleHeading("Numeric")
  LOCAL r AS REAL8
  r := Pow(2,3)
  Console.WriteLine(String.Format("Pow is {0}",r))
  LOCAL f AS FLOAT
  f := Sqrt(10)
  Console.WriteLine(String.Format("10 的根是 {0}",f))
  RETURN
 
FUNCTION Arrays() AS VOID
  ConsoleHeading("Array")
  LOCAL a AS ARRAY
  a := {1,2,3}
  Console.WriteLine(String.Format("原始数组长度为 {0}",ALen(a)))
  AAdd(a,4)
  AAdd(a,5)
  AAdd(a,6)
  Console.WriteLine(String.Format("添加 3 个元素后的数组长度为 {0}",ALen(a)))
  FOR VAR i := 1 TO ALen(a)
     Console.Writeline(String.Format("{0} {1}", i, a[i]))
  NEXT
  ADel(a, 1)
  Asize(a, Alen(a) -1)
  Console.WriteLine(String.Format("删除 1 个元素后的数组长度为 {0}",ALen(a)))
  RETURN
 
FUNCTION Symbols() AS VOID
  LOCAL s as Symbol
  LOCAL c as STRING
  ConsoleHeading("Symbol")
  s := #LastName
  Console.WriteLine("Symbol s: "+s:ToString())
  c := Symbol2String(s)
  Console.WriteLine("Symbol 转换到 string : "+c)
 
  RETURN
 
FUNCTION LateBinding() AS VOID
  LOCAL e as Object
  ConsoleHeading("Late binding")
  e := Error{0, "Message"}
  Console.WriteLine("从未定义类型的变量中读取属性")
  Console.WriteLine( (String) e:Message)
 
  RETURN
 
Function Workarea() AS VOID
  LOCAL cWorkDir as STRING
  FIELD LASTNAME, FIRSTNAME in CUSTOMER
  ConsoleHeading("DBF Access")
  cWorkDir := "..\..\"
  SetPath(cWorkDir)
  DbUseArea(TRUE, "DBFNTX","Customer")
  DbSetIndex("CustNum.NTX")
  DbSetIndex("CustName.NTX")
  DbGoTop()
  DO WHILE ! EOF()
     ? Str(Recno(),3), LASTNAME, FIRSTNAME, _FIELD->CITY
     DbSkip(1)
  ENDDO
  DbCloseArea()
 
FUNCTION Macros() AS VOID
  LOCAL cMacro as STRING
  LOCAL cbMacro as CodeBlock
  ConsoleHeading("Macros")
  cMacro := "{||Today()}"
  cbMacro := &(cMacro)
  ? cMacro, Eval(cbMacro)
  cMacro := "1+2+3"
  ? cMacro, &cMacro
  cMacro := "System.Int32.MaxValue"
  cbMacro := MCompile(cMacro)
  ? cMacro, MExec(cbMacro)
  cMacro := "{|a,b,c| a*b*c}"
  cbMacro := &(cMacro)
  ? cMacro, Eval(cbMacro, 2,3,4)
 
  ?
  RETURN
 
FUNCTION CodeBlocks() AS VOID
  LOCAL oBlock as CodeBlock
  ConsoleHeading("Codeblocks")
  oBlock := {||Today()}
  ? oBlock, eval(oBlock)
  oBlock := {||System.Math.Pow(2,3)}
  ? oBlock, eval(oBlock)
  oBlock := {|a,b,c|a*b*c}
  ? oBlock, eval(oBlock,2,3,4)
  ?
  RETURN