Trouble with Binary Read
Trouble with Binary Read
Trouble with Binary Read
Trouble with Binary Read
Trouble with Binary Read
Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read
Trouble with Binary Read Trouble with Binary Read
Trouble with Binary Read
Go Back  Xtreme Visual Basic Talk > > > Trouble with Binary Read


Reply
 
Thread Tools Display Modes
  #1  
Old 05-15-2012, 07:35 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default Trouble with Binary Read


My program saves the contents of a listbox to file but when
i go to read it back into the listbox, it only goes through a FOR
Loop, 1 time when it should be going through it 60 times. It
doesn't even make it to the FileClose statement. It some how
jumps right out of the loop and exits the sub, without closing
the file. Here is what i got:

Code:
 Dim file_num As Integer = FreeFile()
 Dim listcount As Integer
 Dim imgIndex As Integer
 Dim lstName As String
 Dim i As Integer

 ' Read file back into listbox
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\lstFileData.dat") Then
            ' Open the file 
            FileOpen(file_num, Application.StartupPath & "\lstFileData.dat", OpenMode.Binary, OpenAccess.Read)
          
            ' Get the listbox item count
            FileGet(file_num, listcount)' <-I put a msgbox here. It gives me right number

            For i = 0 To listcount - 1

                ' Get the image index used for the listbox image
                FileGet(file_num, imgIndex) '<-I put msgbox here. I get first number.



                ' Get the listbox item text
                FileGet(file_num, lstName.ToString)'<-Put msgbox here. Get blank


               ' Msgbox doesn't make it this far for some reason. It jumps
               ' out of the loop and exits the sub, without closing the file.

                 lstFiles.Items.Add(lstName.ToString)
                 MsgBox("Made it this far")

                  lstFiles.ItemImage(i).ImageIndex = imgIndex

            Next

            FileClose(file_num)
            MsgBox("File Closed") '<-Code doesn't make it this far.
            '   lstFiles.Refresh()

        End If
I checked the save routine and it is saving everything that i want it to.

In case you want to see it, here it is:

Code:
 Dim file_num As Integer = FreeFile()

  ' Write new file
            FileOpen(file_num, Application.StartupPath & "\lstFileData.dat", OpenMode.Binary, OpenAccess.Write)



            FilePut(file_num, lstFiles.Items.Count)

            For i As Integer = 0 To lstFiles.Items.Count - 1

                ' In case your wondering, i have a user drawn listbox
               ' that holds an icon beside each item in the listbox.
               ' I need to keep track of the index number of the icon
               ' that is stored in an imagelistbox.
                FilePut(file_num, lstFiles.ItemImage(i).ImageIndex)
                FilePut(file_num, lstFiles.Items(i).ToString)

            Next

            FileClose(file_num)
The listbox has 60 items in it and when saving, it is saving everything.
Reply With Quote
  #2  
Old 05-15-2012, 08:09 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

I found out that if i comment out "FileGet(file_num, lstName.ToString)",
the file will at least close, so for some reason, everything is halted here.

I'll use a try statement to see what kind of error i'm getting.

OK. The try statement is say that:
Value cannot be null; Parameter Name Item

So, even though the item names are in the saved file,
the code is not retrieving it.
Reply With Quote
  #3  
Old 05-16-2012, 10:04 AM
AtmaWeapon's Avatar
AtmaWeaponTrouble with Binary Read AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

The culprit is:
Code:
                FileGet(file_num, lstName.ToString)'<-Put msgbox here. Get blank
VB .NET is not VBA/VB6. It's close enough you feel like you can jump in and figure it out as you go, but any XAML programmer will tell you this is an even harder situation thatn "language I do not know" because you don't feel like you don't know what you're doing. How do I know you're diving in head first? FileOpen(), FileGet(), etc. are only there to help with temporary ports of VB6 programs. You shouldn't use them in new programs.

Anyway, why doesn't that line work? "Value cannot be null". That means something is Nothing. There's only two things that can be Nothing on that line: lstName or the return value from ToString().

You'd get NullReferenceException if lstName were null. It's possible because you didn't explain where this was being called, but my gut tells me you're getting ArgumentNullException, which means ToString() is being called successfully but returning Nothing. Here's where I think you'd get tripped up if you have a VB6 mindset.

VB6 classes had the concept of a "default property". If you used their name as a property, you'd get the value of that property. VB .NET does not have this concept, because in general the more you have to memorize the more mistakes you will make. So that line does NOT get the selected item from the ListBox. It tries to convert the ListBox to a string.

And this is where I get confused. Is FileGet() supposed to store a value somewhere? ToString() is a method, not a property. You can't store anything in it. I don't believe your code should compile. But maybe somehow the compiler limps through. either way, you should be using the SelectedItem property if you want to get the selected item. But my guess is you'd need something like this:
Code:
...

Dim itemText As String
FileGet(file_num, itemText)

lstFiles.Items.Add(itemText)

...
I don't know though, I haven't put a lot of thought into it because I don't know how the VB6 file I/O works.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 05-16-2012, 02:38 PM
passel's Avatar
passelTrouble with Binary Read passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

You obviously meant to read the string from the file into lstName.
But you left the .ToString method call on the object which makes no sense.
(You would'nt do: lstName.ToString = "This string")
Also, since lstName is declared as a String, there is no reason to convert the string to a string when you use it (unless you are custom formatting it).

But I don't think the code would work in any case.
The binary read will only read the number of bytes it takes to fill the object passed to it.
If you pass it an empty string, then it will take no bytes to fill an empty string, so no bytes will be read from the file.
To read strings using old legacy Binary I/O you need to know the size of the string that you are reading, create a string of that size, then pass that to the read and the read will fill it.

Why are you using old Binary get/put?
You are looping through the listbox items anyway, why not just read and write them sequentially

In legacy VB, if you put the string in a Variant, then the Variant would encapsulate metadata about the string, so you could put and get the variant and copy it to a string.
Or if you had the string as part of a User Defined Type, then the UDT would have metadata associated with it when put and get so would repopulate the structure correctly.

You should be able to create a small structure to hold the two items for the purpose of putting and getting the data to the file, but I haven't used the old legacy style I/O in .Net, so can't guarantee that it will work.
I haven't done that much .Net file I/O in general, so won't offer any particular solution at this time.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 05-16-2012 at 03:08 PM.
Reply With Quote
  #5  
Old 05-16-2012, 02:54 PM
AtmaWeapon's Avatar
AtmaWeaponTrouble with Binary Read AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Oh jeez, lstName is a string isn't it? I got used to "lst" meaning "ListBox". Curses. Still, I'm surprised it compiles in the current state.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 05-18-2012, 08:29 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Yeah. I tried using ToString and it didn't work either, so i moved away from
saving the listbox items, in favor of storing them in a hard coded array.

I like this way much better because what is currently in the listbox is going
to stay there. These are what i call, system files. These don't get deleted from
the listbox. What is in the listbox, are files in a folder. I want the user to be able
to add and delete their own files but not the system files.

I'm not sure if i'm in the right section of this forum to ask this question but i'm
sure you will let me know.

Like i said, i have the system items, stored in a hard coded structured array and
when i pull the files from a certain folder into a listbox, i compare what is in the
folder to the array that i have stored. Then i mark the system files with a blue ball
image and user files with a red ball image.

The problem is, some of the files have numbers after the name like "Fire1" but
sometimes i have like 10 files with the same name. They are numbered like fire1
fire2 all the way to fire10. When loading these files into the listbox, i get fire1
and then fire10 and then fire2. I need to keep these in order because these items
are hard coded in my array, in order. When i compare what is in the folder to what
my array holds, it puts a red ball in front of a system file because the array has fire2
in the second slot and my listbox is putting fire10 right after fire1.

I have a sub that loads the listbox up but i don't know how to sort them out
before loading them in the listbox.

Hope you can help.

Code:
 Private Sub ProcessFiles(ByVal folder As String)

        ' Search for all files in the selected folder
        ' and display their names in listbox
        
            ' Setup the fileinfo object
            Dim files As IO.FileInfo()

            ' Depending on what folder we are in,
            ' search only for files with certain extensions.
            Select Case lstFolders.SelectedItem
                Case "Audio\BGM", "Audio\ME"
                    files = (New IO.DirectoryInfo(folder)).GetFiles("*.mid")
                Case "Audio\BGS", "Audio\SE"
                    files = (New IO.DirectoryInfo(folder)).GetFiles("*.ogg")
                Case Else
                    files = (New IO.DirectoryInfo(folder)).GetFiles("*.png")
            End Select

            ' Display files in listbox
            For Each file As IO.FileInfo In files
                lstFiles.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name))
            Next file
end sub
Reply With Quote
  #7  
Old 05-18-2012, 09:29 AM
passel's Avatar
passelTrouble with Binary Read passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

The simplest solution would be to change your file naming convention.
If you're going to have less than 100 "duplicate" file names, then use two digits for the number, even if the number is less than 10, i.e. 01, 02, 03, etc... so that they will sort alphabetically in the right order.

If you don't want to add the extra zero for single digits, then you won't have an automated way to sort the values since the number comes at (I presume) the end of arbitrary length file names.
You would have to load them in the array, search for the names with a digit at the end, isolate the number of digits in the name, sort each section of the list numerically using that portion of the name.

Just using a naming convention to allow the normal alphabetical sort work is much easier.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #8  
Old 05-18-2012, 09:04 PM
hDC_0Trouble with Binary Read hDC_0 is offline
Contributor

* Expert *
 
Join Date: Feb 2004
Posts: 559
Default Sorting out array data and a few indexing options in VB.Net

Quote:
Originally Posted by VB_Alien
Yeah. I tried using ToString and it didn't work either, so i moved away from
saving the listbox items, in favor of storing them in a hard coded array.
I agree with this decision.
Sometimes there are reasons to "tight-couple" a data layer to a user interface layer,
but over the years I've found that loose-coupling, in terms of 3-tier modeling, allows for
easier business logic layer coding, more flexibility, scalability, and maintain-ability.

As regarding the second part of your post,
Quote:
Originally Posted by VB_Alien
..i don't know how to sort them out before loading them in the listbox.
..it would seem to me that your question is really something like:
"How do I sort data in an array in an indexed way, before transferring it to a control (like a listbox)?"

There are many ways to do indexing (and index sorting) of an array.

Since you seem to be doing a little bit of "searching about"
(and exploring/learning a bit more about what you can do with arrays),
here is a page (of my thousands of .Net bookmarks), which has a lot of info about working with arrays condensed into one page.

Many newbies start out exploring arraylists (and try, in avoiding heavy duty databases, to make them the solution to all their data storage needs).
There are, however, possible arraylist performance limitations to be aware of in their use (and sometimes generics can be of better use).

You can use generics with both List(Of T) and LinkedList(Of T).
According its MSDN page a List(Of T) "represents a strongly typed list of objects that can be accessed by index".
LinkedList(Of T) is actually a double linked list.

There are also arrays of arrays, multidimensional arrays, dictionaries (1, 2),
collections (with the IndexOf method)..but I would hate to overwhelm you with the many many options.

Also - have you played around with Linq (Language-Integrated Query)?
..especially using Enumerable.Select to do something like:
Quote:
Project each item in the array to an anonymous type that stores the item's index in the array
and a substring of each item whose length is equal to the index position in the original array.
..there's VB.Net code along those lines on this msdn page.
Hopefully you've already downloaded the 101 VB.Net Linq samples and
thoroughly familiarized yourself with what Linq can do.

Overall I write this post because I really hope you go beyond merely considering different file naming schemes,
to exploring the many different ways data can be indexed in VB.Net..

Last edited by hDC_0; 05-18-2012 at 09:15 PM.
Reply With Quote
  #9  
Old 05-19-2012, 03:22 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

No. I don't want to sort my array. My array is setup the way i want it.
My problem is when i load filenames from a folder, into a listbox.
They sort fine until i get filenames with the same name but with numbers
after the name. I have 10 fire images in a folder and instead of loading
them like this Fire1 - Fire2, all the way to 10, it loads them like this,
Fire1 - Fire10 - Fire2 through Fire9.

My array is in order 1,2,3,4,5,6,7,8,9,10. When i compare my array with
the file, my array will be at Fire2 but the actual File being loaded is Fire10.

I need to find a way programmicaly sort the filenames in order with my array.
Everything in the folder that i'm loading from, is my version of system files
for my program. I don't want the user to be able to delete those, using my program.

The user will also get the chance to load in their own files with the option to
delete those if they want to but not the system files. I mark system files with a
Blue ball image in the listbox and user files with a Red ball.

since my array and the actual files, don't compare correctly, i'm getting Red balls
in my system files.

If i can't find a way to load the files in order, i'll be forces to change my array
but i really don't want to do that, if i can help it.

****Edit****

I don't know if i would be making things harder on myself or not but i
was wondering if i could put the filenames in an array and sort them
that way before loading them in the listbox. Or maybe load the files into
an invisible listbox and use list of that way? Just wondering.

Last edited by VB_Alien; 05-19-2012 at 03:28 AM.
Reply With Quote
  #10  
Old 05-19-2012, 03:30 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Hey passel. Long time no see. Hope things are good with you.
Reply With Quote
  #11  
Old 05-19-2012, 07:22 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

I came up with a different game plan. Instead of the user adding
their files to the system files, they now have their own "UserFiles"
folder to put their stuff in. The i used hDC_0 idea of loading my
array of stored filenames in the listbox.

So, first i load all of the user files and then i load the system files
behind that. Although i now have some rewriting of code to do, this
looks like it will work great. Thanks for the idea hDC_0.
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
Trouble with Binary Read
Trouble with Binary Read
Trouble with Binary Read Trouble with Binary Read
Trouble with Binary Read
Trouble with Binary Read
Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read Trouble with Binary Read
Trouble with Binary Read
Trouble with Binary Read
 
Trouble with Binary Read
Trouble with Binary Read
 
-->