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

How to convert the values in an Object into String

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 text box, where we want to display
the values of any given object (say customer, order etc). We declared (Dim
Obj as New Object) and assigned our application object. Now we want to print
as Text (string) the values of Obj.

Issue : We tried using Obj.ToString (similar to java), but here the default
implementation is to display the Name/type of the object rather than the
values. Is there any way we can get this to work without implementing a
toString in Each of our classes. (We really want to avoid this as there are
quite a few classes)

Is there any way to loop thro the properties of a class and print them /or
any other solution/ideas

For example:
I've created an Object "obj"
the "obj" contains the following properties:
name
Description
.....
.....
.....
Here i need to get the values of the Properties(Attributes) in a rich text
box..
but i dont want to get the values as
obj.name
obj.Descriprion
....
....
and so on.

I want it in a single or simple code, no matter how many properties are there

--
Peter...
Nov 21 '05 #1
2 9303
Hi Peter,

Yes, using reflection:
Public Overrides Function ToString() As String

Dim objType As Type
Dim colPropertyInfo() As System.Reflection.PropertyInfo
Dim objPropertyInfo As System.Reflection.PropertyInfo
Dim sResult As String

objType = Me.GetType()

colPropertyInfo = objType.GetProperties(Reflection.BindingFlags.Inst ance Or
Reflection.BindingFlags.Public Or Reflection.BindingFlags.GetProperty)

For Each objPropertyInfo In colPropertyInfo

sResult &= objPropertyInfo.Name & " = " & objPropertyInfo.GetValue(Me,
Nothing).ToString ' TODO: check for nulls
sResult &= Microsoft.VisualBasic.ControlChars.CrLf

Next

Return sResult

End Function

--

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

"Prabhudhas Peter" <Pr*************@discussions.microsoft.com> escribió en
el mensaje news:24**********************************@microsof t.com...
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 text box, where we want to
display
the values of any given object (say customer, order etc). We declared
(Dim
Obj as New Object) and assigned our application object. Now we want to
print
as Text (string) the values of Obj.

Issue : We tried using Obj.ToString (similar to java), but here the
default
implementation is to display the Name/type of the object rather than the
values. Is there any way we can get this to work without implementing a
toString in Each of our classes. (We really want to avoid this as there
are
quite a few classes)

Is there any way to loop thro the properties of a class and print them /or
any other solution/ideas

For example:
I've created an Object "obj"
the "obj" contains the following properties:
name
Description
.....
.....
.....
Here i need to get the values of the Properties(Attributes) in a rich text
box..
but i dont want to get the values as
obj.name
obj.Descriprion
....
....
and so on.

I want it in a single or simple code, no matter how many properties are
there

--
Peter...

Nov 21 '05 #2
Thank you Carlos...
I was usefull to me...
--
Peter...
"Carlos J. Quintero [.NET MVP]" wrote:
Hi Peter,

Yes, using reflection:
Public Overrides Function ToString() As String

Dim objType As Type
Dim colPropertyInfo() As System.Reflection.PropertyInfo
Dim objPropertyInfo As System.Reflection.PropertyInfo
Dim sResult As String

objType = Me.GetType()

colPropertyInfo = objType.GetProperties(Reflection.BindingFlags.Inst ance Or
Reflection.BindingFlags.Public Or Reflection.BindingFlags.GetProperty)

For Each objPropertyInfo In colPropertyInfo

sResult &= objPropertyInfo.Name & " = " & objPropertyInfo.GetValue(Me,
Nothing).ToString ' TODO: check for nulls
sResult &= Microsoft.VisualBasic.ControlChars.CrLf

Next

Return sResult

End Function

--

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

"Prabhudhas Peter" <Pr*************@discussions.microsoft.com> escribió en
el mensaje news:24**********************************@microsof t.com...
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 text box, where we want to
display
the values of any given object (say customer, order etc). We declared
(Dim
Obj as New Object) and assigned our application object. Now we want to
print
as Text (string) the values of Obj.

Issue : We tried using Obj.ToString (similar to java), but here the
default
implementation is to display the Name/type of the object rather than the
values. Is there any way we can get this to work without implementing a
toString in Each of our classes. (We really want to avoid this as there
are
quite a few classes)

Is there any way to loop thro the properties of a class and print them /or
any other solution/ideas

For example:
I've created an Object "obj"
the "obj" contains the following properties:
name
Description
.....
.....
.....
Here i need to get the values of the Properties(Attributes) in a rich text
box..
but i dont want to get the values as
obj.name
obj.Descriprion
....
....
and so on.

I want it in a single or simple code, no matter how many properties are
there

--
Peter...


Nov 21 '05 #3

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

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
3
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
2
by: Learner | last post by:
Hello, I am trying to store the data entered in a webform in the database. I have few Int and one SmallDateTime filed in my table in SQL Server 2005 database. I have made a storedproc to store the...
4
by: briancfk | last post by:
I now converting vb6 code to vb.net code Let me descrip my problem first i got a Function e.g. Public Function Method1(Byval ar as object, Optional ByVal strWHFrom As String, Optional ByVal...
4
by: simonZ | last post by:
Why this don't work: Boolean test; String testValue; testValue="0"; test=System.Convert.ToBoolean(testValue); How can I convert string to boolean?
3
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
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
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
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...

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.