473,386 Members | 1,962 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,386 software developers and data experts.

Simple Reflection Question

Hi all,

Let's say that I have an object oPerson as type clsPerson.

clsPerson has a string property called, say, sName.

Now - I have a database table which has 'as strings' the names of the
properties of clsPerson for which I need to get values.

So - given my string "sName" and instantiated object oPerson, how do I
return the value of "sName" from oPerson.

Can someone please point me in the right direction?

Thanks very much in advance,

Damien Sawyer

Nov 21 '05 #1
3 829
Found it! I'll post the solution if anyone's interested.
DS
<><><><><><><><><><><><><><><><><><><><><><><><>< ><><><><><><><><><><><><><><><><><><><><><><><


Imports System

Imports System.Reflection

Module Module1

Sub Main()

' Create an instance

Dim oOb As clsPerson = New clsPerson(1, "Jesus")

' Get Type of order

Dim oType As Type = GetType(clsPerson)

' Note - this also works.

'Dim otype = oOb.GetType

' Get Value

Dim pPropInfo As PropertyInfo = oType.GetProperty("sName")

Dim sValue As String = pPropInfo.GetValue(oOb, Nothing)

'Display

MsgBox(sValue)

End Sub

End Module

Public Class clsPerson

Private p_id As Int16

Private p_Name As String

Sub New(ByVal iID As Int16, ByVal sName As String)

Me.p_id = iID

Me.p_Name = sName

End Sub

ReadOnly Property sName() As String

Get

Return Me.p_Name

End Get

End Property

ReadOnly Property iID() As Int16

Get

Return Me.p_id

End Get

End Property

End Class

Nov 21 '05 #2
Damien,

You know that using Int16 gives you a slightly lower performance on 32bits
computers with a 32bit OS.

Cor
Nov 21 '05 #3
And if the data comes from SQL Server then it would also be recommended to
use a 32-bit int

By the way is there any specific reason (current project, ...) why you want
to use reflection to retrieve the value of the property?

Gabriel Lozano-Morán

"Cor Ligthert" <no************@planet.nl> wrote in message
news:OK**************@TK2MSFTNGP09.phx.gbl...
Damien,

You know that using Int16 gives you a slightly lower performance on 32bits
computers with a 32bit OS.

Cor

Nov 21 '05 #4

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

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
2
by: Dan | last post by:
Let's say I have a class like: class Dummy { public const string CONE = "one"; public const string CTWO = "two"; ... other stuff .... }
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
8
by: Jeff | last post by:
I'm new to Visual Web 2005 using VB: ....trying to get something like the below to run. Apparently, I need something other than the eval function, but what? Thanks in advance Jeff
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
1
by: =?Utf-8?B?Y2FsZGVyYXJh?= | last post by:
Dear all, I am developing a multi tiers application and actually responsible of delivering the buines logic library. That buisnes logic library will need some parameters to be suitebale...
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.