473,383 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Get class property value where name in variable

I need to get a class property value, where property name is contained
in a variable.

example:
class test
dim p1 as string
dim p2 as string
end class
....
sub x
dim t as test
dim myvar as string = "p1"
....
'I need to read a property of object t and the name of property is in
"myvar"
?

How can I do it?
Thanks

Nov 21 '05 #1
3 5032
Class test
Dim p1 As String
Dim p2 As String

Sub New(ByVal par1 As String, ByVal par2 As String)
p1 = par1
p2 = par2
End Sub
End Class

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim t As test
Dim myvar As String = "p1"
Dim objFieldInfo As System.Reflection.FieldInfo

t = New test("A", "B")

objFieldInfo = GetType(test).GetField(myvar,
Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)

MsgBox(objFieldInfo.GetValue(t).ToString) ' returns "A"

End Sub
--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"rambalep" <wb*****@gmail.com> escribió en el mensaje
news:11**********************@g47g2000cwa.googlegr oups.com...
I need to get a class property value, where property name is contained
in a variable.

example:
class test
dim p1 as string
dim p2 as string
end class
...
sub x
dim t as test
dim myvar as string = "p1"
...
'I need to read a property of object t and the name of property is in
"myvar"
?

How can I do it?
Thanks

Nov 21 '05 #2

rambalep wrote:
I need to get a class property value, where property name is contained
in a variable.

example:
class test
dim p1 as string
dim p2 as string
end class
...
sub x
dim t as test
dim myvar as string = "p1"
...
'I need to read a property of object t and the name of property is in
"myvar"


Carlos has given you code to do this, but it's worth pointing out that
if you find yourself having to use reflection in 'normal' code, it
probably means your design is wrong.

It's hard to tell from this barebones example but it looks like you
want a String -> String Dictionary, as implemented for example by a
simple Hashtable.

--
Larry Lard
Replies to group please

Nov 21 '05 #3
"rambalep" <wb*****@gmail.com> schrieb:
'I need to read a property of object t and the name of property is in
"myvar"


=> 'CallByName' function.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4

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

Similar topics

19
by: laurenq uantrell | last post by:
I'm using Access 2K. I'm hoping someone can tell me which method performs faster- (currently I'm using a mix of both methods) a.) creating custom properties and then calling functions to set and...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
2
by: Andrzej Kaczmarczyk | last post by:
Hi, I have following problem: (warning, quite long post with lots of code) I have an object of a known type like this. class MyClass { public int Property1; public string Property2; }
2
by: Steve | last post by:
I'm in a weird situation where I'm using ComboBox's in a DataGrid. When the ComboBox selection changes, I'm currently storing the SelectedValue object into the DataSource of the DataGrid cell. ...
6
by: Tom Kiefer | last post by:
Question: If I have an ASP.NET User Control which defines/exposes a property that the page can use to specify a mode or data subset for the control to use, is there a way to tell the @OutputCache...
12
by: Saurabh Kumar | last post by:
I have a byte array, which i need to send to a class, where it will be stored in an arraylist. I use the following property in the class to accept the byte array, but it seems that the array is...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: Alani | last post by:
Hello All, I'm a new ASP.NET programmer and I want to create a custom control consist of two properties (Number1) and (Number2) and both of them are integers and default value = 0, now I'm...
2
by: Coco | last post by:
Hi, I am wondering if this can be done. Well i have a class that take any object, and the class will get string from text file which is value need to be retrieved from this object. For example...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.