Problem with Converting VB6 Code

dcahrakos

Freshman
Joined
Jul 18, 2004
Messages
25
Hi,

im trying to get this code to work in vb.net but I cant seem to get it to work right, it seemed to work fine, but when using the same function from this code more then once, it throws an "Object reference not set to an instance of an object."

the code takes values in a table. which is an array up to 255 to represent all hex numbers, and converts the raw hex read from a binary file into the characters represented in the table array.

the code is attached because it is to long.

any help is greatly appreciated.
(I know its coded sloppy, but I havent had any time to clean it up, and it was written some years ago for vb6, if anyone has any tips to clean it up, just post it)

Thanks in advance.
 

Attachments

Before I start, can I just point out that in the comments at the top of the page you say that VB doesn't store a string as an array of chars. Is this left over from before the port, becuase a string has a .Chars property that allows you access the char at a specific index.

It would have helped if you had said exactly which line throws the error. Since the code runs once without errors it seems apparent that an object is being disposed of the first time you run the function and not being re-initialised the second time you run. Without knowing which lines throws the error I can't tell which object is causing the problem. All I can say is after a cursary look at your code I couldn't spot anything obvious.
 
oh, sorry, yes that comment is left over from the vb6 code.

sorry about that, it throws an error at InitTable() in the gettext function. havent tested the savetext function yet, but it probably does the same thing.
 
I would imagine that the problem is being caused by the 'Erase' statement. I don't know VB that well, but its the only line that seems like it could cause the error, try ReDim'ing the array after Erasing it.
 
Arrays can be statically declared under .Net which would remove the need for the InitTable Method completely.
Visual Basic:
Dim Table() As String = {"~", "\n", "", "\i", ""} 'etc. just carry on to the end of the array

This would also save runtime overheads as the current implementation seems to be recreating the exact same array into the exact same variable each time the function is run...

It may help if you could explain what the two functions are actually attempting to achieve as there may be a much simpler way in .Net.
 
<s>Yep, that Erase Table is what caused the problem, and just deleting that made the Gettext function work properly.</s>

Edit: Actually, it worked fine for 2 calling the function twice, but then it goes back to the same error, I think ill just declare it statically and try it that way, so I dont need to use the inittable sub.

does declaring a static array like that start at 0? so it would go from 0-255 like it is right now.

Well. basically this function is useful for reading text from video game roms, this is good for translations and such, basically what happens is that the video game does not store text in ASCII, which makes it necessary to use table files(.tbl) to make each hex character(00-FF) represented by said characters in the table.
for example:
A0=_
A1=!
A2=?
A4=$
A6="
A7='
A8=(
A9=)
AA=:
AB=;
AC=,
AD=-
AE=.
AF=/
 
Last edited:
Arrays start at 0 so you shouldn't have any problems there.

You could probably also simplify a lot of this code by using built in .Net functionality, a few quick changes you might want to consider are.

Visual Basic:
        If Len(pString) < Length Then
            For i = 1 To Length - Len(pString)
                pString = pString & " "
            Next i
        End If
could be replaced with
Visual Basic:
pString = pString.PadRight(Length)

The whole chunk
Visual Basic:
       For i = Len(pString) To 0 Step -1

            Dim NewString As String
            NewString = Right(pString, Len(pString) - i)
            strSplitString(i) = Left(NewString, 1)

        Next i

can be replaced by declaring
Visual Basic:
Dim strSplitString() As Char = pString.ToCharArray()
or just using pString.ToCharArray() directly in the remaining code

also code like
Visual Basic:
bw.Seek(Offset + Incre, SeekOrigin.Begin)
bw.Write(Hex(Incre2))
is not needed as bw.Write will automatically advance the position in the file based on the value you are writing.

Visual Basic:
Dim Results() As Byte
Dim ResText() As String

'snipped

ReDim Results(Length)
ReDim ResText(Length)

could be declared as just
Visual Basic:
Dim Results(Length) As Byte
Dim ResText(Length) As String
 
Last edited:
Thanks for all those, ill change them all now.


bw.write will automatically seek? I was under the impression that it would write from the beginning, but also advance the position, so if I wrote like 3 bytes at the beginning, it would be 3 bytes into the file.
 
Back
Top