Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
Go Back  Xtreme Visual Basic Talk > > > Question about Random Access Files in VB.NET


Reply
 
Thread Tools Display Modes
  #1  
Old 09-30-2012, 06:35 AM
geo1st487's Avatar
geo1st487 geo1st487 is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 328
Default Question about Random Access Files in VB.NET


The bellow code use methods which have been around since the early days
of Visual Basic. How to do the same thing using the VB.NET method instead these old methods?
Thanks

Code:
Public Class Form1
	Public Structure Employee
		Public ID As Integer
		< VBFixedString(15) > Public FirstName As String
		< VBFixedString(15) > Public LastName As String
		
		Public Sub New(ByVal new_id As Integer, ByVal first_name As String,
			ByVal last_name As String)
			ID = new_id
			FirstName = first_name
			LastName = last_name
		End Sub

		Public Overrides Function ToString() As String
			Return ID & ": " & FirstName & " " & LastName
		End Function
	End Structure

	Private Sub btnMakeRecords_Click() Handles btnMakeRecords.Click
		' Declare a record variable.
		Dim emp As New Employee
		' Get an available file number.
		Dim file_num As Integer = FreeFile()
		' Open the file.
		FileOpen(file_num, "MYFILE.DAT", OpenMode.Random,
		OpenAccess.ReadWrite, OpenShare.Shared, Len(emp))
		' Make some records.
		FilePut(file_num, New Employee(1, "Alice", "Altanta"))
		FilePut(file_num, New Employee(2, "Bob", "Bakersfield"))
		FilePut(file_num, New Employee(3, "Cindy", "Chicago"))
		FilePut(file_num, New Employee(4, "Dan", "Denver"))
		FilePut(file_num, New Employee(5, "Erma", "Eagle"))
		FilePut(file_num, New Employee(6, "Fred", "Frisco"))

		' Fetch and display the records.
		Dim obj As ValueType = DirectCast(emp, ValueType)
		For Each i As Integer In New Integer() {3, 1, 5, 2, 6}
			FileGet(file_num, obj, i)
			emp = DirectCast(obj, Employee)
			Debug.WriteLine("[" & emp.ToString() & "]")
		Next i
		' Close the file.
		FileClose(file_num)
	End Sub
End Class
Reply With Quote
  #2  
Old 09-30-2012, 10:00 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default VB.Net Random Access

This old .Net page might be a good starting place:
MSDN: Random File Access

MSDN: VB.net Seek Function:
Code:
'For files opened in Random mode, Seek returns the number of next record.
VB
FileOpen(1, "TESTFILE", OpenMode.Random)
Do While Not EOF(1)   
   WriteLine(1,Seek(1))   ' Write record number.
   FileGet(1, MyRecord, -1)   ' Read next record.
Loop
FileClose(1)
There are also old .Net File I/O threads (if you do a search),
that talk about Random File Access, like this one.

However, the .Net framework also supports the FileStream Class:
Quote:
FileStream objects support random access to files using the Seek method.
Seek allows the read/write position to be moved to any position within the file. This is done with byte offset reference point parameters.
The byte offset is relative to the seek reference point,
which can be the beginning, the current position, or the end of the underlying file,
as represented by the three properties of the SeekOrigin class.
I'm an old timer
I've always stored file record data values in ini files, where you can search by key and key value per section.
My newer .Net sample for using ini files is attached to this post.


I'm sure a lot of xvbt forum members would recommend switch to using a database
and/or using Linq instead of storing data in random access files.

Last edited by surfR2911; 09-30-2012 at 10:36 PM.
Reply With Quote
  #3  
Old 10-01-2012, 10:45 PM
jguetzlaff jguetzlaff is offline
Newcomer
 
Join Date: Jul 2003
Location: Bavaria, Germany
Posts: 10
Default VB.NET Storing Random Data

I'm doing VB pretty much since the beginning and before that and since 83 I did GW-BASIC and PDS. So I'm used to that legacy stuff - and still like it.
BUT: in .NET I really appreciate the DataSet object and the things you can do with it. Unless you have huge, huge amounts of data, just write the stuff to a dataset and store it in XML format and you're all set. You don't have to worry about database and connect strings and all those things. It's simple, it's straight forward and it works. I agree with surf, I loved those INI files. But even with that I switched to XML.
It's important to understand that this is not a solution for a networking environment, where many people need concurrent access to current data!
I'm curious if that will work for you.
Reply With Quote
  #4  
Old 10-02-2012, 10:05 AM
PJOttCan PJOttCan is offline
Newcomer
 
Join Date: Nov 2007
Posts: 4
Default

Even though the problem is seemingly "simple" I would still create two classes, one to represent the file and another one (or more, if records in file are homogeneous) to represent the individual records in the file, and then those classes can be written to "know" how to open/close/read/write their base store (in this case a text file). Then, if you decide to change where/what the base store of the data is your main code won't change you can just edit the two(+) classes. The main code simply creates and references the objects and doesn't care how the read/write occurs, just merrily does a MyFile.Read(), MyFile.SubItem(i).ChangeValue("newvalue") and MyFile.Write(), for instance. It might seem like too much to do it this way but I find this approach works in VB, PHP, Javascript, etc. And I think it saves us maintenance hassles in the end. I'm, of course, assuming that you're now, after all these years, comfortable with OOP. If you're only a casual VB programmer ... well, I still recommend it and I think others here might be able to express this point better than my current ramble.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
 
Question about Random Access Files in VB.NET
Question about Random Access Files in VB.NET
 
-->