Examining the File Class

Examining the File Class

You should take note of a couple of items in the description of the File class. First notice that the class is sealed. In Visual Basic .NET, the sealed keyword means that the class is not inheritable. Unless the class itself is static, like this one happens to be, you can create new instances of sealed classes, but you cannot derive other classes from them. Also notice that the File class is inherited from System.Object.

The class information is shown in C# syntax, so the class information will look just a bit different from what Visual Basic programmers are used to seeing. However, there's really no more than a gnat's eyebrows difference in the syntax of the two languages. We see that this class can be used because it is public, we can't inherit from it because it's sealed, and it happens to be inherited from System.Object.

public sealed class System.IO.File : object

note

Remember that when a class is instantiated, it first instantiates its parent and then any parent's parent, and so on up the chain of inheritance to System.Object. The chain then starts down, instantiating each child until the original class is reached, which is instantiated after every class it depends on has been initialized. Because each object is ultimately derived from System.Object, each class will have the same base of functionality inherited from System.Object.

Also notice that the File class has neither member fields nor constructors.

// Fields // Constructors

How can we even create a new instance of the File class if it has no constructor? Well, we can't. If a class has no constructor, we can use the class but we can't create a new instance of it because it is static. Remember how we used static variables to contain a single value across all instances of a class? The File class is static, which means that a single instance of the class is shared everywhere in your program.

Notice that the methods of the File class are also static and provide for the creation, copying, deletion, moving, and opening of files. Because the File class is static, we can't create an instance of the class with the New keyword, but we can use the static File class as-is without instantiating an object.

// Methods public static System.IO.StreamWriter AppendText(string path); public static void Copy(string sourceFileName, string destFileName); public static void Copy(string sourceFileName, string destFileName, bool overwrite); public static System.IO.FileStream Create(string path); public static System.IO.FileStream Create(string path, int bufferSize); public static System.IO.StreamWriter CreateText( string path); public static void Delete(string path);

The File class illustrates a twist that will help you understand how to use WinCV with every .NET class. The File class is primarily a utility class with static methods that are used to create FileStream objects based on absolute file paths. To see how to use the File class in our programs, look at its first method, AppendText. The signature of AppendText tells us exactly how to use the method.

public static System.IO.StreamWriter AppendText(string path);

You can see that this method is public, so we can access it in our programs. Next, this method is static, so we can't create a new instance of it, but it is available if we declare it. The return parameter, System.IO.StreamWriter, tells us that we will have to create a variable of type StreamWriter and assign it the return value of AppendText. If this process sounds a bit strange at this point, don't worry. Shortly we will use the AppendText method so that you can clearly see how these steps are accomplished.

The parameter to AppendText is a string that contains the path of the file we want to either create or append to. So, we can call the AppendText method of the File class and pass in the name and location of a file, and the class will return to us an object of type StreamWriter. Now you're probably wondering what a stream is. In Visual Basic .NET, files work hand in hand with streams, so let's take a closer look.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net