IR
irwinrodriguez.dev
Back to docs

Quick start

In under five minutes you will have your first .NET class running from VFP.

Step 1: initialize and activate

Once, at application start-up. Activate() is not an extra: the license is checked at every entry point of the engine, so without it not even one object is created.

Create the bridge and activate the license
LOCAL loBridge
loBridge = NEWOBJECT("FoxBridge", "FoxBridge.prg")

IF !loBridge.Activate(MySystem_GetLicense())
    ? "FoxBridge: " + loBridge.GetLastError()
    RETURN
ENDIF

* Optional check that everything loaded:
? loBridge.Version()   && FoxBridge Runtime 1.0.0 / Core ...

Step 2: create your first .NET object

System.Text.StringBuilder is a .NET class that builds strings efficiently. It is the "hello world" of FoxBridge.

StringBuilder: the hello world of .NET
* Create a StringBuilder with some initial text
loSB = loBridge.Create("System.Text.StringBuilder", "Hello ")

* Call methods with the usual dot syntax
loSB.Append("world ")
loSB.Append("from .NET!")

* Turn it into a VFP string
? loSB.ToString()   && "Hello world from .NET!"

* And read a property
? loSB.Length       && 22

* Release it
loSB = .NULL.

Step 3: work with generic collections

FoxBridge translates List<T> and Dictionary<K,V> into VFP proxies. Indexing is 1-based, like VFP.

List<String> and Dictionary from VFP
* Create a List<String>
loList = loBridge.Create("List<String>")

loList.Add("Visual FoxPro")
loList.Add("C#")
loList.Add("F#")

? "Total:", loList.Count()     && 3
? "First:", loList.Item(1)     && "Visual FoxPro"

FOR i = 1 TO loList.Count()
    ? loList.Item(i)
ENDFOR

loList.Remove("F#")
? "After Remove:", loList.Count()   && 2

* A Dictionary<K,V>. Note: the method is ContainsKey, not Contains.
loDict = loBridge.Create("Dictionary<String,String>")
loDict.Set("name", "Irwin")
? loDict.Get("name")            && "Irwin"
? loDict.ContainsKey("name")    && .T.
? loDict.Count()                && 1

loList = .NULL.
loDict = .NULL.

Item() and Get() return the value, which is what you want when the collection holds strings or numbers. If what is inside is a .NET object, ask for the proxy with GetObject().

Step 4: call static methods

Many .NET classes have static members, with nothing to instantiate. You reach them with loBridge.Static().

System.Math, System.IO.File and an enum by name
* System.Math is a static class
loMath = loBridge.Static("System.Math")

? loMath.Abs(-42)       && 42
? loMath.Sqrt(144)      && 12
? loMath.Max(10, 20)    && 20
? loMath.PI             && 3.14159...

* System.IO.File: read and write files
loFile = loBridge.Static("System.IO.File")
loFile.WriteAllText("C:\test.txt", "Hello from FoxBridge")
? loFile.Exists("C:\test.txt")     && .T.
? loFile.ReadAllText("C:\test.txt")

* Enums are static proxies too, and their members are read by name
* instead of writing the number:
loFileMode = loBridge.Static("System.IO.FileMode")
? loFileMode.Create     && 2

loMath = .NULL.
loFile = .NULL.

Step 5: load your own DLL

FoxBridge loads any .NET DLL you compiled yourself.

Load your own DLL and use one of its classes
llOk = loBridge.LoadAssembly("C:\MyApp\bin\MyLogic.dll")

IF llOk
    loObj   = loBridge.Create("MyCompany.MyLogic.Calculator", 0.16)
    lnTotal = loObj.CalculateWithVAT(1000.00)
    ? "Total with VAT:", lnTotal
    loObj = .NULL.
ELSE
    ? "Error:", loBridge.GetLastError()
ENDIF
Golden rule Release FoxBridge objects with = .NULL. when you no longer need them: that frees the memory on the .NET side. And remember that GetLastError() is consumed when you read it: a second read straight after comes back empty, so keep it in a variable if you are going to use it twice.

Next: Examples ->