Is It Possible to Retrieve a List of Declared Variables?

D

Don

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
 
H

Herfried K. Wagner [MVP]

Don said:
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?

'GetType(MyClass1).GetFields(...)'.
 
J

Jay B. Harlow [MVP - Outlook]

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.GetProperties 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.Reflection.FieldInfo In aType.GetFields()
Debug.WriteLine(fi.Name, "field")
Next
For Each pi As System.Reflection.PropertyInfo In
aType.GetProperties()
Debug.WriteLine(pi.Name, "property")
Next
For Each mi As System.Reflection.MethodInfo In aType.GetMethods()
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


| 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
|
|
 
D

Don

'GetType(MyClass1).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
 
H

Herfried K. Wagner [MVP]

Don said:
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.NonPublic'.
 
J

Jay B. Harlow [MVP - Outlook]

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

http://msdn.microsoft.com/library/d.../html/frlrfsystemtypeclassgetfieldstopic2.asp

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


|
| |
| >
| > 'GetType(MyClass1).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
|
|
 
D

Don

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.GetFields().

If I call GetFields with no arguments, then the fieldinfo for the Public
variable is returned. If I call GetField with BindingFlags.NonPublic, I get
nothing. And if I call GetField with BindingFlags.Public, 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.
 
H

Herfried K. Wagner [MVP]

Don said:
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.GetFields().

If I call GetFields with no arguments, then the fieldinfo for the Public
variable is returned. If I call GetField with BindingFlags.NonPublic, I
get nothing. And if I call GetField with BindingFlags.Public, 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

Include 'BindingFlags.Instance' ('BindingFlags.Instance Or
BindingFlags.Public', for example).
 
J

Jay B. Harlow [MVP - Outlook]

Don,
Did you review the URL I gave you?

http://msdn.microsoft.com/library/d.../html/frlrfsystemtypeclassgetfieldstopic2.asp

As Herfried points out, you need both BindingFlags.Instance to say instance
fields (or BindingFlags.Static for Shared fields) plus you need
BindingFlags.Public 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


|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.GetFields().
|
| If I call GetFields with no arguments, then the fieldinfo for the Public
| variable is returned. If I call GetField with BindingFlags.NonPublic, I
get
| nothing. And if I call GetField with BindingFlags.Public, 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.
|
|
| | >>> 'GetType(MyClass1).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.NonPublic'.
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
|
 
D

Don

Jay B. Harlow said:
Don,
Did you review the URL I gave you?

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
 

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