.NET for Visual FoxPro Developers

Appendix C
The Visual FoxPro Toolkit for .NET

This appendix discusses the Visual FoxPro Toolkit for .NET, which gives VFP developers a great boost up the .NET learning curve.

If you’re proficient at Visual FoxPro, it can be frustrating when first learning .NET. You know how to do something in VFP, but you don’t know the equivalent .NET base class or language feature to use. This is where the Visual FoxPro Toolkit for .NET comes to the rescue.

This toolkit was created by Kamal Patel and is in the public domain for the benefit of Visual FoxPro developers. You can download this toolkit from the Visual FoxPro “GotDotNet” site: http://www.gotdotnet.com/team/vfp/.

The toolkit is a class library with methods that correspond to Visual FoxPro functions. It allows you to enter familiar Visual FoxPro commands in your C# or Visual Basic .NET source code.,You can set up the toolkit in Visual Basic .NET so you can just type the Visual FoxPro command—without prefixing a class name—and a method is called on a corresponding class in the toolkit. In C#, you need to enter both a class and method name.

To use the toolkit, you must first install it, and then add a reference to it in your project.  Source code is provided for both C# and Visual Basic .NET.

To give you an idea of how the toolkit works, you can use the Visual FoxPro StrToFile() command as an example. If you look up the StrToFile() command in the Visual FoxPro Toolkit for .NET Help file, the top of the Help file describes two different ways to call this function in the toolkit:

1.       Receives a string and a file name as parameters and writes the contents of the string to that file.

2.       Receives a string and a file name as parameters and writes the contents of the string to that file. Receives an additional parameter specifying whether the contents should be appended at the end of the file.

These two items translate into two overloaded methods in the toolkit. The Help file displays the signatures for each overload, as well as examples showing how to call these methods. Here is one of the examples in C#:

string lcString = "This is the line we want to insert in our file.";

VFPToolkit.strings.StrToFile(lcString, "c:\\My Folders\\MyFile.txt");

And in Visual Basic .NET:

Dim lcString As String = "This is the line we want to insert in our file."

StrToFile(lcString, "c:\My Folders\MyFile.txt")

This is great, but what’s even better is the Help topic also shows you the code that is executed behind the scenes when you call the StrToFile method.

In C#:

public static void StrToFile(string cExpression, string cFileName)

{

  //Check if the sepcified file exists  

  if (System.IO.File.Exists(cFileName) == true)

  {       

       //If so then Erase the file first as in this case we are overwriting      

       System.IO.File.Delete(cFileName); }     

  //Create the file if it does not exist and open it  

  FileStream oFs = new

       FileStream(cFileName,FileMode.CreateNew,FileAccess.ReadWrite);      

  //Create a writer for the file  

  StreamWriter oWriter = new StreamWriter(oFs);

  //Write the contents     

  oWriter.Write(cExpression);     

  oWriter.Flush();  

  oWriter.Close();  

  oFs.Close();

}

And in Visual Basic .NET:

Public Shared Sub StrToFile(ByVal cExpression As String, _

  ByVal cFileName As String)

    'Check if the sepcified file exists   

    If System.IO.File.Exists(cFileName) = True Then

        'If so then Erase the file first as in this case we are overwriting       

        System.IO.File.Delete(cFileName)

    End If

    'Create the file if it does not exist and open it   

    Dim oFs As FileStream = New _

        FileStream(cFileName, FileMode.CreateNew, FileAccess.ReadWrite)

    'Create a writer for the file   

    Dim oWriter As StreamWriter = New StreamWriter(oFs)

    'Write the contents    oWriter.Write(cExpression)   

    oWriter.Flush()

    oWriter.Close()

    oFs.Close()

End Sub

This code provides an education in using C# and Visual Basic .NET to access the functionality of the .NET Framework.

I’ve seen Visual FoxPro developers use this toolkit in a variety of ways. Some like to use the Visual FoxPro commands directly in their .NET code, providing familiar programming syntax. Others just keep the Help file on their desktop as “training wheels”. Whenever they want to figure out how to do something in .NET, they look up the corresponding Visual FoxPro command in the Help file.

For more information on installing the toolkit, check out the Visual FoxPro Toolkit for .NET Help file.

 


.NET for Visual FoxPro Developers