VB Struct query (converted from VB6 Type) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

VB Struct query (converted from VB6 Type)


I am currently migrating a VB6 project to VB.NET. I used the VS tool that converts VB6 code to VB.NET code. The conversion changed a VB6 Type to a VB.NET Structure.

The user-defined Type construct was used in a Get/Put where a binary file was read in or written out based on the user-defined Type. Apaprently, the Get/Put was able to do this easily.

During the conversion the Get/Put were changed to FileGet/FilePut but apparently these cannot automatically read in or write out bytes in a Type construct way.

In any event, I decided to replace the FileGet/FilePut code to the .NET FileStream and BinaryReader and BinaryWriter constructs.

What I am now trying to do is to write a class where I take a structure as a paramter and want to read and write the bytes in the same field order as the VB6 Type construct. Looking into the Structure format (which I know is somewhat similar to a Class), there does not seem to be an east way to discern the order the fields are in for reading/writing bytes.

Does anyone have thoughts on the best approach to solving this problem?

Regards,
Neville Lang


Comments

  • NevilleLangNevilleLang Posts: 4Member

    I should have added that the Structure's fields (or Properties) names are not in alphabetical order and their types can be Stings, Integer, Singles or Double, all in a particular order. The strings are being stored as Chars so they can be read in or written out to binary files in the current format as used in VB6.

    Since the order of Fields or Properties is important for reading/writing to a file, the difficulty I am having is being able to loop through the fields (or properties) in a particlaur order and getting the field name and then its type in order to use the BinaryReader's type, as an example.


    Regards,
    Neville Lang

  • NevilleLangNevilleLang Posts: 4Member

    After further research and testing, I found a solution. The thing that solved my problem was in the Reflection Namespace, something that I have not used previously.

    Here is what I did to solve my problem:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Imports System.Reflection
    Public Class TestClass1
    Public Shared Sub MyTest(ByVal s As MyStruct)
    ' NOTE: MyStruct is specified and located in another
    ' file in the solution

    Dim t As Type = s.GetType()
    Dim f As FieldInfo() = t.GetFields()
    ' Fields above are returned in the order in which they are
    ' specified, using Reflection
    Dim i As Integer
    Dim a As String = ""

    For i = 0 To f.Length - 1
    If f(i).FieldType.Name = "String" Then
    a = f(i).Name
    ElseIf f(i).FieldType.Name = "Int32" Then
    a = f(i).Name
    ElseIf f(i).FieldType.Name = "Single" Then
    a = f(i).Name
    ElseIf f(i).FieldType.Name = "Double" Then
    a = f(i).Name
    End If
    Next
    End Sub
    End Class
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The main Reflection items are the GetFields() and FieldType.Name. I also found that the Fields list is returned in the order in which the structure is created so that was important for me.

    Anyway, I now have enough to proceed to begin to use BinaryRead and BinaryWrite.


    Regards,
    Neville Lang


Sign In or Register to comment.