473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is It Possible to Retrieve a List of Declared Variables?

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
Jan 27 '06 #1
9 6809
"Don" <un*****@oblivi on.com> schrieb:
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(MyClas s1).GetFields(. ..)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Jan 27 '06 #2
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" <un*****@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
|
|
Jan 27 '06 #3
Don

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:OC******** ******@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
Jan 27 '06 #4
"Don" <un*****@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/>

Jan 27 '06 #5
Don,
Use the overloaded GetFields, & specify the option that indicates Friend.

http://msdn.microsoft.com/library/de...eldstopic2.asp

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Don" <un*****@oblivi on.com> wrote in message
news:mxwCf.2331 43$tl.88327@pd7 tw3no...
|
| "Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
| news:OC******** ******@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
|
|
Jan 27 '06 #6
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.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]" <hi************ ***@gmx.at> wrote in message
news:OZ******** *****@TK2MSFTNG P10.phx.gbl...
"Don" <un*****@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/>

Jan 27 '06 #7
"Don" <un*****@oblivi on.com> schrieb:
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


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

Jan 28 '06 #8
Don,
Did you review the URL I gave you?

http://msdn.microsoft.com/library/de...eldstopic2.asp

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" <un*****@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]" <hi************ ***@gmx.at> wrote in message
| news:OZ******** *****@TK2MSFTNG P10.phx.gbl...
| > "Don" <un*****@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/>
|
|
Jan 28 '06 #9
Don

"Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
message news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
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
Jan 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5129
by: James Monroe | last post by:
1st off... I am not talking about session, application, form, or server variables.. Does any one know of an asp script or program than can analyze a ".asp" page and report back all of the variables used. I am re-doing some old code from the past that did not use option explicit and it would be a lot easier to dim the variables if...
0
1645
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int myVal);
15
5745
by: joun | last post by:
Hi all, i want to create in my asp.net application a custom server variable, so i can retrieve later in other pages (even asp or perl) with request.servervariables("HTTP_mycustomvariable") i've tried with response.appendheader("mycustomvariable", "somevalue") but this doesn't work, or the variable scope is limited to aspx files, not asp...
35
3560
by: Thierry Loiseau | last post by:
Hello all, and Happy end year 2005 ! Well, I would like to obtain a list of all JavaScript var statement, With "for...in" perharps ? That is bellow my recent test here, but the problem is to management theses :-((( I must to declare and use all variable with this scheme :
1
1641
by: tomjbr.32022025 | last post by:
I have started looking at the nhibernate framework, but do not really like the string based API which makes it impossible to use automatic refactoring of a property name without the risk of getting problems where the property has been refered to as a string... Therefore I am wondering if it would be even theoretically possible to improve...
5
3351
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran...
3
12556
by: Tyno Gendo | last post by:
I don't really need to do this, but I was wondering. Is there some way of obtaining a list of all the variables that have been declared in a PHP script ? Ie. for globals you have $_GLOBAL, is there something that would let you know the name of all variables that have been declared? Ta.
5
25541
by: Ken | last post by:
I'm trying to run a loop to capture column property information from a table in my datasource. Can anybody see where this is going wrong? Dim tbl As New DataTable Dim col As DataColumn Dim x As Integer Dim colName(99) As String Dim colType(99) As String cn.Open() tbl = cn.GetSchema("Orders") 'Orders is a table in the
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.