Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > How to get name of all variables within Structure?


Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2012, 05:30 AM
R3ck R3ck is offline
Freshman
 
Join Date: Sep 2003
Posts: 33
Default How to get name of all variables within Structure?


Hi, I have a structure type, as below

Code:
Public Structure StrFolder
 Public isActive As Boolean
 Public NameFolder As String
 Public URLIDNumber As Integer
 Public DateOfFolder As String
 Public NameDescription As String
 Public NameLocation As String
End Structure
I would like to retrieve and print each variable name within a given structure (not its value). For example:
StrFolder(0).Name should print "isActive"
StrFolder(1).Name should print "NameFolder"
StrFolder(2).Name should print "URLIDNumber"
StrFolder(3).Name should print "DateOfFolder"
etc..

I would also like to know each type of an item within a structure. For example:
StrFolder(0).Type should print "Boolean"
StrFolder(1).Type should print "String"
StrFolder(2).Type should print "Integer"
StrFolder(3).Type should print "String"
etc..

I have googled for a while now and couldn't find what I'm looking for.
Thanks for your help or pointing into the right direction!

Last edited by R3ck; 06-28-2012 at 05:35 AM.
Reply With Quote
  #2  
Old 06-28-2012, 06:57 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,885
Default

Maybe a stupid question, but in what situation do you need this?
I never encountered this question and/or need before.
Reply With Quote
  #3  
Old 06-28-2012, 11:19 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is online now
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

I'm with Flyguy. It's *sort of* easy to do this, but the use cases I can envision have better solutions. How about you explain what you want to do rather than how you think it's done?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 06-28-2012, 11:41 AM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,740
Default

I agree with Flyguy and Atma, but rather than hold my tongue, I'll share this borrowed/modified example (since I've already looked into it):
Code:
Imports System
Imports System.Reflection
Public Class Form1
    Public Structure StrFolder
        Public isActive As Boolean
        Public NameFolder As String
        Public URLIDNumber As Integer
        Public DateOfFolder As String
        Public NameDescription As String
        Public NameLocation As String
    End Structure
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim test As StrFolder
        Dim i As Integer
        test.isActive = True
        Dim myType As Type = test.GetType
        Dim myField As FieldInfo() = myType.GetFields() 'Get the fields from my() 'type'
        For i = 0 To myField.Length - 1
            Debug.Print(myField(i).Name)
            Debug.Print(myField(i).FieldType.Name)
        Next i
    End Sub
End Class
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
Reply With Quote
  #5  
Old 07-01-2012, 02:19 AM
R3ck R3ck is offline
Freshman
 
Join Date: Sep 2003
Posts: 33
Default

Thanks everyone and Cerian Knight for your reply, helped me alot.

What I need to to is to export JSON data with headings named the same as the given VB structure.

I tried to understand JavaScriptSerializer methods but that turned into hours of frustration and no solution, so I just do it myself instead of .net even if it means more clunky code. Thanks everyone!

Last edited by R3ck; 07-01-2012 at 03:37 AM.
Reply With Quote
  #6  
Old 07-01-2012, 05:28 AM
snarfblam's Avatar
snarfblam snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 866
Default

Is there a reason you can't just use an existing JSON library for .NET?
__________________
C# _VB.NET _
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->