1

I'm attempting to convert some VBA to VB.Net. I have not used VB.Net much as I prefer using C#, however due to the amount of code I need to convert I'm coding a VB.Net dll, which I can use as a reference in the C# application.

I have the following VBA code and need to be able to produce the same in VB.Net

Open FileName For Binary Access Read As #1
Get #1, , customType

Where customType is a user-defined data type. I have replicated the required types as structures in VB.net, however I'm not sure where to start in reproducing this code for VB.Net

Any suggestions would be appreciated.

Update 1:

FileOpen(1, FileName, OpenMode.Binary)
FileGet(1, customStruct)

The above seems to be somewhat working, however I get the following exception when the custom structure has a item defined as an array.

e.g,

Public Structure CustomStruct
    Dim FileType() As Byte
End Structure

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in Microsoft.VisualBasic.dll

Additional information: Non-negative number required.

I have initialized the dimensions via,

ReDim customStruct.FileType(7)

Thanks

2
  • Have a look at this. It should give you all the information you need to read a binary file in vb.net Commented Sep 13, 2016 at 8:33
  • Thanks, I have seen this. I've recently found FileOpen and FileGet, which hopefully is the equivalent in VB.net. Reading is not so much the problem, however assigning the binary read to the custom structure is. Commented Sep 13, 2016 at 8:54

1 Answer 1

1

I found a solution after a lot of trial and error, the below works,

FileOpen(1, FileName, OpenMode.Binary) FileGet(1, customStruct)

In regards to the other issue, the array must be declared as fixed in the structure, even if it is initialized prior to being used

<VBFixedArray(2)> Dim FileType() As Byte

Josh

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.