Click here to Skip to main content
15,070,139 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
First, a simple data access class:

VB
Public Class MyTestClass

    ' the structure of the records in the data file
    Public Structure FileRecordType
        Dim Index As Integer
        Dim RefIndex As Integer
        Dim Type As Short
        <VBFixedString(75)> Public Title As String
    End Structure

    Dim mRecord As FileRecordType


End Class


Obviously, the real implementation will have methods to read & write the data, properties to access the fields, etc...

Now, the questions:

1. Is there a methodology to access the structiure members by name? What I am think of is a simple function like:
Function GetValue(ByVal FieldName as String, ByVal iRecord as FileRecordType) as Object

That will take the name (as a string) of the field and somehow find the associated field in the passed record. I realize this will ahve to occur within the scope of the class taht contains the structure declaration.

2. Similarly, I would like to be able to "scan through" the structure and return a list of the field names.

3. A method to find the string types within the structure and return the length of the field.
Posted
Comments
Sergey Alexandrovich Kryukov 31-Oct-12 14:39pm
   
Why would you use Microsoft.VisualBasic.VBFixedStringAttribute? If would compromise compatibility with .NET, probably designed to be used for compatibility with legacy VB. Don't use it.
Now, your question is just a bad idea. You can use Reflection though, but I don't think you see a sense in it. I don't want to formally answer on something which might may no sense. You should better explain the ultimate purpose of your activity.
--SA
KevinBrady57 31-Oct-12 16:16pm
   
Why is it a bad idea to use VBFixedStringAttribute? You don;t explain why. This is done so I can read from a random access data file. What is the alternative?

It all means using Reflection. Please learn this stuff:
http://msdn.microsoft.com/en-us/library/f7ykdhsy.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.aspx[^].

However, please see my comment to the question. I seriously doubt that you understand your purpose and would prefer to warn you against pointless over-engineering of your solutions.

I also warned you against using Microsoft.VisualBasic.VBFixedStringAttribute, http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.vbfixedstringattribute.aspx[^], please see my comment to the question again.

—SA
   
v3
You can use concept of reflection like this

VB
Dim fi as FieldInfo[] = typeof(MyTestClass).GetFields(BindingFlags.Public | BindingFlags.Instance)


this array contains info about all public fields in the Structure
you can change 'BindingFlags.Public' this parameter to get other fields as you like

for more information read about reflection concept
   
v2
Comments
KevinBrady57 31-Oct-12 17:49pm
   
Thank you. I will read about this and experiment with it. I was not familiar with reflection before.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900