473,397 Members | 2,084 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,397 software developers and data experts.

object reflection

RP
Hi all,

I have a class object that has member classes and structures. I basically
want to be able to write some code that writes out all the names of the
member variables in the class object and their values. e.g.

Public Structure Person
Public fname, mname, lname as string
End Structure

Public Structrue Department
Public departmentcode as integer
Public departmentname as string
End Structure

Public Class Employee
Public P as Person
Public D as Department
End Class

With my code I would like some output like:

Employee.P.fname = Param
Employee.P.mname =
Employee.P.lname = Dawson
Employee.D.departmentcode = 1
Employee.D.departmentname = Technology

Any assistance would be greatly appreciated.

TIA
Nov 20 '05 #1
3 1023
Hi,

Add a public overrides function tostring to your structure.

Public Class Employee

Public P As Person

Public D As Department

Public Overrides Function Tostring() As String

Dim strout As String

strout = String.Format("Person {0} {1} {2}", P.fname, P.mname, P.lname)

strout &= ControlChars.NewLine & String.Format("Department {0} {1}",
D.departmentcode, D.departmentname)

Return strout

End Function

End Class

Ken

---------------------------

"RP" <rp@nospam.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
Hi all,

I have a class object that has member classes and structures. I basically
want to be able to write some code that writes out all the names of the
member variables in the class object and their values. e.g.

Public Structure Person
Public fname, mname, lname as string
End Structure

Public Structrue Department
Public departmentcode as integer
Public departmentname as string
End Structure

Public Class Employee
Public P as Person
Public D as Department
End Class

With my code I would like some output like:

Employee.P.fname = Param
Employee.P.mname =
Employee.P.lname = Dawson
Employee.D.departmentcode = 1
Employee.D.departmentname = Technology

Any assistance would be greatly appreciated.

TIA

Nov 20 '05 #2
RP
Ken, that would work but I was kind of looking for some code that given an
instance of a variable E the code uses reflection to extract the members and
echo the names and values they hold.

thanks a bunch

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OO**************@tk2msftngp13.phx.gbl...
Hi,

Add a public overrides function tostring to your structure.

Public Class Employee

Public P As Person

Public D As Department

Public Overrides Function Tostring() As String

Dim strout As String

strout = String.Format("Person {0} {1} {2}", P.fname, P.mname, P.lname)

strout &= ControlChars.NewLine & String.Format("Department {0} {1}",
D.departmentcode, D.departmentname)

Return strout

End Function

End Class

Ken

---------------------------

"RP" <rp@nospam.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
Hi all,

I have a class object that has member classes and structures. I basically want to be able to write some code that writes out all the names of the
member variables in the class object and their values. e.g.

Public Structure Person
Public fname, mname, lname as string
End Structure

Public Structrue Department
Public departmentcode as integer
Public departmentname as string
End Structure

Public Class Employee
Public P as Person
Public D as Department
End Class

With my code I would like some output like:

Employee.P.fname = Param
Employee.P.mname =
Employee.P.lname = Dawson
Employee.D.departmentcode = 1
Employee.D.departmentname = Technology

Any assistance would be greatly appreciated.

TIA


Nov 20 '05 #3
dev
I think this misght be wwhat you are looking for. I am setting the values
of the properties, but I believe there is a GetValue. Im using a
PropertyInfo but the is MethodInfo and some others. Check it out.

Try

Dim arrproperties() As PropertyInfo

'get the type corresponding to the class

Dim t As System.Type = Type.GetType(Me.ToString)

'we're not interested in primitives or value types

If (t.IsClass) Then

arrproperties = t.GetProperties()

End If

Dim p As PropertyInfo

For i = 0 To UBound(arrProps)

For j = 0 To UBound(arrproperties)

p = arrproperties(j)

If LCase(p.Name) = LCase(arrProps(i)) Then

p.SetValue(Me, arrProps(i + 1), Nothing)

End If

Next

i += 1

Next

Return True

Catch ex As Exception

m_cTWriter.WriteTraceError(ex)

Return False

End Try

"RP" <rp@nospam.com> wrote in message
news:ei**************@TK2MSFTNGP11.phx.gbl...
Ken, that would work but I was kind of looking for some code that given an
instance of a variable E the code uses reflection to extract the members and echo the names and values they hold.

thanks a bunch

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OO**************@tk2msftngp13.phx.gbl...
Hi,

Add a public overrides function tostring to your structure.

Public Class Employee

Public P As Person

Public D As Department

Public Overrides Function Tostring() As String

Dim strout As String

strout = String.Format("Person {0} {1} {2}", P.fname, P.mname, P.lname)

strout &= ControlChars.NewLine & String.Format("Department {0} {1}",
D.departmentcode, D.departmentname)

Return strout

End Function

End Class

Ken

---------------------------

"RP" <rp@nospam.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
Hi all,

I have a class object that has member classes and structures. I basically want to be able to write some code that writes out all the names of the member variables in the class object and their values. e.g.

Public Structure Person
Public fname, mname, lname as string
End Structure

Public Structrue Department
Public departmentcode as integer
Public departmentname as string
End Structure

Public Class Employee
Public P as Person
Public D as Department
End Class

With my code I would like some output like:

Employee.P.fname = Param
Employee.P.mname =
Employee.P.lname = Dawson
Employee.D.departmentcode = 1
Employee.D.departmentname = Technology

Any assistance would be greatly appreciated.

TIA



Nov 20 '05 #4

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

Similar topics

0
by: Cat | last post by:
I have class Base, and class Derived. I am serializing Derived objects from XML, and these Derived objects are allowed to 'inherit' the values from other named Base objects already defined in...
6
by: solex | last post by:
Hello, I am trying to use serialization to copy objects. The object in question "Institution" inherits from a parent object "Party" both are marked as <Serializable()>. Initially I can copy an...
10
by: Chris Morse | last post by:
Hi, I'm trying to figure out how to dynamically load an assembly and get an IClientPlugin interface to a class. I can get an "Object" reference to it after doing an "Activator.CreateInstance"...
7
by: Dave Taylor | last post by:
I have a DataTable with three string columns, "Value", "Format" and "Type" However, some of the rows contain numbers and dates in the Value field. So I would like to be able to format the output...
9
by: Dennis | last post by:
When a class (myownclass) inheirits another class, how can I get an object reference to the underlyng MyBase class instance from within myownclass. The base class has a method that I want to...
2
by: Prabhudhas Peter | last post by:
Hello, Need an easy way to Display the property values of an object (dynamically) ie I need to show the values of an Object in a rich text box. Background : We have a form, with a rich...
2
by: syed | last post by:
Hi all, I am trying to invoke xml web service from dot net assembly . it is fails to create the proxy object and throw the below exception. Pls any suggestions. Error Desc: Object reference...
10
by: schneider | last post by:
I'm looking for a way to programaticly call a method from a different object and associate the two objects at runtime. Example: Object A exist and is unknow, I want object B to be able to call a...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
7
by: joproulx | last post by:
Hi, I was wondering if there was a way with Reflection to find dynamically if an object was referencing indirectly another object. A simple example would be: Object1 | --Object2 |
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: 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: 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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.