Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

txtcode.txt

Posted

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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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.

Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
  • Administrators
Posted

Arrays can be statically declared under .Net which would remove the need for the InitTable Method completely.

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

<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=/

Edited by dcahrakos
  • Administrators
Posted (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.

 

       If Len(pString) < Length Then
           For i = 1 To Length - Len(pString)
               pString = pString & " "
           Next i
       End If

could be replaced with

pString = pString.PadRight(Length)

 

The whole chunk

      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

Dim strSplitString() As Char = pString.ToCharArray()

or just using pString.ToCharArray() directly in the remaining code

 

also code like

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.

 

Dim Results() As Byte
Dim ResText() As String

'snipped

ReDim Results(Length)
ReDim ResText(Length)

 

could be declared as just

Dim Results(Length) As Byte
Dim ResText(Length) As String

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...