Is It Possible to Retrieve a List of Declared Variables?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Don

    Is It Possible to Retrieve a List of Declared Variables?

    Say I have a class like so:

    Public Class MyClass
    Public Prop1 as Integer
    Public Prop2 As Integer
    Public Prop3 As Integer
    End Class

    Is it possible to retrieve a list of the variables or objects declared
    within an instance of that class if they are declared with Public (or
    Friend) scope?

    e.g.

    Public Class SomeOtherClass

    Public Sub Experiment

    Dim obj As MyClass = New MyClass

    ' Get list of variables declared within obj
    '
    ' List the variable names:
    ' "Prop1"
    ' "Prop2"
    ' "Prop3"

    End Sub

    End Class

    Or can something like this only be done with Properties and Methods?

    - Don


  • Herfried K. Wagner [MVP]

    #2
    Re: Is It Possible to Retrieve a List of Declared Variables?

    "Don" <unknown@oblivi on.com> schrieb:[color=blue]
    > Public Class MyClass
    > Public Prop1 as Integer
    > Public Prop2 As Integer
    > Public Prop3 As Integer
    > End Class
    >
    > Is it possible to retrieve a list of the variables or objects declared
    > within an instance of that class if they are declared with Public (or
    > Friend) scope?[/color]

    'GetType(MyClas s1).GetFields(. ..)'.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Is It Possible to Retrieve a List of Declared Variables?

      Don,
      You can using Reflection.

      You need to start with a System.Type object. You can use the GetType keyword
      to get a type object of known class, or you can use Object.GetType to get a
      type object of a known object.

      Once you have a Type object, you can call Type.GetFields &
      Type.GetPropert ies to get fields & properties of the respective type. Watch
      the overloads, as you can "fine tune" what you are looking for.

      Dim aType As Type = GetType([MyClass])
      For Each fi As System.Reflecti on.FieldInfo In aType.GetFields ()
      Debug.WriteLine (fi.Name, "field")
      Next
      For Each pi As System.Reflecti on.PropertyInfo In
      aType.GetProper ties()
      Debug.WriteLine (pi.Name, "property")
      Next
      For Each mi As System.Reflecti on.MethodInfo In aType.GetMethod s()
      Debug.WriteLine (mi.Name, "method")
      Next

      Look for other methods on System.Type to retrieve other information on that
      type.

      --
      Hope this helps
      Jay [MVP - Outlook]
      ..NET Application Architect, Enthusiast, & Evangelist
      T.S. Bradley - http://www.tsbradley.net


      "Don" <unknown@oblivi on.com> wrote in message
      news:EevCf.4572 80$ki.60104@pd7 tw2no...
      | Say I have a class like so:
      |
      | Public Class MyClass
      | Public Prop1 as Integer
      | Public Prop2 As Integer
      | Public Prop3 As Integer
      | End Class
      |
      | Is it possible to retrieve a list of the variables or objects declared
      | within an instance of that class if they are declared with Public (or
      | Friend) scope?
      |
      | e.g.
      |
      | Public Class SomeOtherClass
      |
      | Public Sub Experiment
      |
      | Dim obj As MyClass = New MyClass
      |
      | ' Get list of variables declared within obj
      | '
      | ' List the variable names:
      | ' "Prop1"
      | ' "Prop2"
      | ' "Prop3"
      |
      | End Sub
      |
      | End Class
      |
      | Or can something like this only be done with Properties and Methods?
      |
      | - Don
      |
      |


      Comment

      • Don

        #4
        Re: Is It Possible to Retrieve a List of Declared Variables?


        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
        news:OCjWRA4IGH A.1188@TK2MSFTN GP14.phx.gbl...
        [color=blue]
        >
        > 'GetType(MyClas s1).GetFields(. ..)'.[/color]

        This worked for Publicly declared variables (which is great! thanks!), but
        is there a way to get it to work with variables of scope Friend as well?

        - Don


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Is It Possible to Retrieve a List of Declared Variables?

          "Don" <unknown@oblivi on.com> schrieb:[color=blue][color=green]
          >> 'GetType(MyClas s1).GetFields(. ..)'.[/color]
          >
          > This worked for Publicly declared variables (which is great! thanks!),
          > but is there a way to get it to work with variables of scope Friend as
          > well?[/color]

          Take a look at the overloaded versions of 'GetFields' which accept a
          'BindingFlags' parameter. Check out 'BindingFlags.N onPublic'.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Is It Possible to Retrieve a List of Declared Variables?

            Don,
            Use the overloaded GetFields, & specify the option that indicates Friend.



            --
            Hope this helps
            Jay [MVP - Outlook]
            ..NET Application Architect, Enthusiast, & Evangelist
            T.S. Bradley - http://www.tsbradley.net


            "Don" <unknown@oblivi on.com> wrote in message
            news:mxwCf.2331 43$tl.88327@pd7 tw3no...
            |
            | "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
            | news:OCjWRA4IGH A.1188@TK2MSFTN GP14.phx.gbl...
            |
            | >
            | > 'GetType(MyClas s1).GetFields(. ..)'.
            |
            | This worked for Publicly declared variables (which is great! thanks!),
            but
            | is there a way to get it to work with variables of scope Friend as well?
            |
            | - Don
            |
            |


            Comment

            • Don

              #7
              Re: Is It Possible to Retrieve a List of Declared Variables?

              I figured it had something to do with that, but I couldn't find any obvious
              argument or combination of arguments that works.

              In my test case, I have a class (a Form, actually) with one Public variable,
              one Friend, and one Private. Within the form's Load event I call
              Me.GetType.GetF ields().

              If I call GetFields with no arguments, then the fieldinfo for the Public
              variable is returned. If I call GetField with BindingFlags.No nPublic, I get
              nothing. And if I call GetField with BindingFlags.Pu blic, I still get
              nothing. I don't know if I'm missing some other BindingFlag or not. It
              didn't behave as I expected it to, given what I could deduce from the flag
              name and the online help.


              "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
              news:OZf4k$4IGH A.676@TK2MSFTNG P10.phx.gbl...[color=blue]
              > "Don" <unknown@oblivi on.com> schrieb:[color=green][color=darkred]
              >>> 'GetType(MyClas s1).GetFields(. ..)'.[/color]
              >>
              >> This worked for Publicly declared variables (which is great! thanks!),
              >> but is there a way to get it to work with variables of scope Friend as
              >> well?[/color]
              >
              > Take a look at the overloaded versions of 'GetFields' which accept a
              > 'BindingFlags' parameter. Check out 'BindingFlags.N onPublic'.
              >
              > --
              > M S Herfried K. Wagner
              > M V P <URL:http://dotnet.mvps.org/>
              > V B <URL:http://classicvb.org/petition/>[/color]


              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: Is It Possible to Retrieve a List of Declared Variables?

                "Don" <unknown@oblivi on.com> schrieb:[color=blue]
                > In my test case, I have a class (a Form, actually) with one Public
                > variable, one Friend, and one Private. Within the form's Load event I
                > call Me.GetType.GetF ields().
                >
                > If I call GetFields with no arguments, then the fieldinfo for the Public
                > variable is returned. If I call GetField with BindingFlags.No nPublic, I
                > get nothing. And if I call GetField with BindingFlags.Pu blic, I still get
                > nothing. I don't know if I'm missing some other BindingFlag or not. It
                > didn't behave as I expected it to, given what I could deduce from the flag
                > name and the online help[/color]

                Include 'BindingFlags.I nstance' ('BindingFlags. Instance Or
                BindingFlags.Pu blic', for example).

                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://classicvb.org/petition/>

                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: Is It Possible to Retrieve a List of Declared Variables?

                  Don,
                  Did you review the URL I gave you?



                  As Herfried points out, you need both BindingFlags.In stance to say instance
                  fields (or BindingFlags.St atic for Shared fields) plus you need
                  BindingFlags.Pu blic to include public fields...

                  There are other BindingFlags available to include or exclude other fields
                  (such as inherited fields).

                  So be certain to review the above page.



                  --
                  Hope this helps
                  Jay [MVP - Outlook]
                  ..NET Application Architect, Enthusiast, & Evangelist
                  T.S. Bradley - http://www.tsbradley.net


                  "Don" <unknown@oblivi on.com> wrote in message
                  news:RdxCf.2334 35$tl.52321@pd7 tw3no...
                  |I figured it had something to do with that, but I couldn't find any obvious
                  | argument or combination of arguments that works.
                  |
                  | In my test case, I have a class (a Form, actually) with one Public
                  variable,
                  | one Friend, and one Private. Within the form's Load event I call
                  | Me.GetType.GetF ields().
                  |
                  | If I call GetFields with no arguments, then the fieldinfo for the Public
                  | variable is returned. If I call GetField with BindingFlags.No nPublic, I
                  get
                  | nothing. And if I call GetField with BindingFlags.Pu blic, I still get
                  | nothing. I don't know if I'm missing some other BindingFlag or not. It
                  | didn't behave as I expected it to, given what I could deduce from the flag
                  | name and the online help.
                  |
                  |
                  | "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                  | news:OZf4k$4IGH A.676@TK2MSFTNG P10.phx.gbl...
                  | > "Don" <unknown@oblivi on.com> schrieb:
                  | >>> 'GetType(MyClas s1).GetFields(. ..)'.
                  | >>
                  | >> This worked for Publicly declared variables (which is great! thanks!),
                  | >> but is there a way to get it to work with variables of scope Friend as
                  | >> well?
                  | >
                  | > Take a look at the overloaded versions of 'GetFields' which accept a
                  | > 'BindingFlags' parameter. Check out 'BindingFlags.N onPublic'.
                  | >
                  | > --
                  | > M S Herfried K. Wagner
                  | > M V P <URL:http://dotnet.mvps.org/>
                  | > V B <URL:http://classicvb.org/petition/>
                  |
                  |


                  Comment

                  • Don

                    #10
                    Re: Is It Possible to Retrieve a List of Declared Variables?


                    "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> wrote in
                    message news:%231pAre8I GHA.596@TK2MSFT NGP10.phx.gbl.. .[color=blue]
                    > Don,
                    > Did you review the URL I gave you?[/color]

                    Sorry, no. I read Herfried's response first and went with that, not
                    noticing your post later on. I was poring over the BindingFlags Enumeration
                    help, but I find the documentation provided with VS.NET to be typically
                    pretty obscurely written with poor code examples (at least compared with
                    pre-.NET Visual Studio documentation).

                    - Don


                    Comment

                    Working...