Structures

D

Dave H

I have a structure in VB

Public Structure structRecord
Public TranCode As String
Public TranTime As String
Public Bob As String
Public John As String
End Structure

I want to examine each element in the structure to see if there's data,
without having to know the structure before hand.

Do you know if there's some sort of:

For Next x In structRecord
....
Next

That make sense?
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Thanks for your post.

Normally, the answer is no. .Net is a strong typed programming platform,
which it does not allow one type to be casted to another non-related type.
Only child-base relation class types can do the casting.

Also, .Net strong type means that .Net must know the exact type information
of a class(structure), it will force you explicitly use the fields in the
class(structure). So I do not think there is any way to access the field
without explicitly specify the type and field info.

Also, "For Next" statement is of no use in this scenario, to use "For
Next", the class must support IEnumerable and IEnumerator interfaces. And
this will only allow you loop the childs element of certain property, not
all the class type fields. For more information about how to support "For
Next" for your customized class, please refer to:
"HOW TO: Make a Visual Basic .NET Class Usable in a For Each Statement"
http://support.microsoft.com/?id=322025

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
H

Herfried K. Wagner [MVP]

Dave H said:
Public Structure structRecord
Public TranCode As String
Public TranTime As String
Public Bob As String
Public John As String
End Structure

I want to examine each element in the structure to see if there's data,
without having to know the structure before hand.

Do you know if there's some sort of:

For Next x In structRecord
...
Next

\\\
Public Structure StructRecord
Public TranCode As String
Public TranTime As String
Public Bob As String
Public John As String
End Structure
..
..
..
Dim sr As StructRecord
For Each fi As FieldInfo In sr.GetType().GetFields( _
BindingFlags.Public Or BindingFlags.Instance _
)
Dim s As String = fi.Name & ": "
Dim Value As Object = fi.GetValue(sr)
If Value Is Nothing Then
s &= "Nothing"
Else
s &= Value.ToString()
End If
MsgBox(s)
Next fi
///
 
D

Dave H

Herfried... thank you .. that Rocks!

Works perfect! I guess this is why Reflection is talked about so much!

Dave
 
J

Jeffrey Tan[MSFT]

Oh, yes, I seem forget this one..., thank you for sharing with the community

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top