Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Take Advantage of Streams and Formatters in VB.NET  : Page 6

.NET File I/O operations use Stream and Formatter classes that abstract many tasks you had to code manually in classic VB.


advertisement
To connect to a remote Web server and request a file, you must create a WebRequest object and call its GetResponse method. The GetResponse method returns a Stream object, which you can use to read the remote file almost as if it were local. The following statements create a WebRequest object, which represents a request you make from within your application to a remote file. To create a WebRequest object call the Create method of the WebRequest class, passing the URL of the remote resource as argument. To retrieve the file, which in this case is the response from the remote Web server, you call the GetResponse method of the WebRequest object that represents the request. The GetResponse method returns a WebResponse object, which you can then pass as an argument to the StreamReader constructor. The following statements show how to request a file from a Web server and display it in a TextBox control:

Example: Reading a File from a Remote Web Server

Dim url As New Uri = _ "http://www.your_server.com/your_file.txt" Dim Req As WebRequest Req = WebRequest.Create(url) Dim Resp As WebResponse Try Resp = Req.GetResponse Catch exc As Exception MsgBox(exc.Message) Exit Sub End Try Dim netStream As StreamReader netStream = New StreamReader(Resp.GetResponseStream) TextBox2.Text = netStream.ReadToEnd

The MemoryStream Class
The MemoryStream represents a stream in memory, effectively letting you treat your computer's memory as a file. One common use of the MemoryStream class is to create clones (copies) of objects. If you serialize an object to a MemoryStream and then deserialize the stream and assign the resulting object to a new variable, you'll get back a copy of the original object—an exact duplicate, a clone. The following statements outline the process. The public Clone function could be a method of the Person class shown earlier in this article:

Example: Creating a Copy of a Custom Object

Public Function Clone() As Person Dim BinFormatter As New Binary.BinaryFormatter() Dim memStream As New System.IO.MemoryStream() BinFormatter.Serialize(memStream, Me) memStream.Position = 0 Return CType(BinFormatter.Deserialize _ (memStream), Person1) End Function

To test the Clone method, create a Person instance and initialize its fields. Then declare another Person variable and assign the clone of the first variable to it:



Dim P1 As New Person Dim P2 As New Person P1.Name = "my name" P1.Age = 35 P1.Income = 40000 P2 = P1.Clone()

Note that if you assign P2 to P1, both variables will point to the same object and every change you make to P1 will also affect P2. By cloning an object, you have created two instances of the Person class and you can manipulate them individually.

The point to take away from this article is that by abstracting the operations to read and write objects of all types to any medium the .NET Stream classes unify and simplify the process of reading and writing to all types of sequential data stores.



Evangelos Petroutsos is a long time VB developer. When he's not writing code, he writes programming books (most published by Sybex), and articles. Reach him by e-mail at pevangelos@yahoo.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date