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

How to reflect all variables for an object?

Dear all,

I have created many objects
Currently i create a toString function, which is to list all variables value
inside this object
Somthing like

Public Class ABC
Dim sName as string = "Hello"
Dim iValue as integer = 10

Public Function ToString() as string
Dim temp as string
temp &= "sName =" & sName & ","
temp &= "iValue =" & iValue
Return temp
End Sub
End Class

Everytime i added a new variable i need to modify this function again
If there any method to list all variable name and its value in an object and
then return it as string?

Thx a lot
Nov 20 '05 #1
2 1029
> If there any method to list all variable name and its value in an object
and
then return it as string?
I doubt you can do that, not to get the result you have now (if someone can
prove me wrong please do :) )
but I would like to suggest using a string builder instead of &

Dim sb As New System.Text.StringBuilder()
sb.Append("sName = ")
sb.Append(sName)
sb.Append(", iValue = ")
sb.Append(iValue.tostring)
return sb.tostring()

eric

"Norton" <no********@hotmail.com> wrote in message
news:um*************@tk2msftngp13.phx.gbl... Dear all,

I have created many objects
Currently i create a toString function, which is to list all variables value inside this object
Somthing like

Public Class ABC
Dim sName as string = "Hello"
Dim iValue as integer = 10

Public Function ToString() as string
Dim temp as string
temp &= "sName =" & sName & ","
temp &= "iValue =" & iValue
Return temp
End Sub
End Class

Everytime i added a new variable i need to modify this function again
If there any method to list all variable name and its value in an object and then return it as string?

Thx a lot

Nov 20 '05 #2
Norton,
Everytime i added a new variable i need to modify this function again
If there any method to list all variable name and its value in an object and then return it as string? You mean other then the one you wrote?

No there is no built-in function that does that. However you could easily
build one that is based on Properties (as opposed to fields).

Something like:

Imports System.ComponentModel
Imports System.Text

Public Overrides Function ToString() As String
Dim list As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(Me.GetType())
Dim sb As New StringBuilder
Dim delim As String
For Each item As PropertyDescriptor In list
sb.Append(delim)
sb.Append(item.Name)
sb.Append("=")
sb.Append(item.GetValue(Me))
delim = ","
Next
Return sb.ToString()
End Function
Using Reflection and the Type object returned from GetType you could
probably build one based on Fields almost as easily...

Hope this helps
Jay

"Norton" <no********@hotmail.com> wrote in message
news:um*************@tk2msftngp13.phx.gbl... Dear all,

I have created many objects
Currently i create a toString function, which is to list all variables value inside this object
Somthing like

Public Class ABC
Dim sName as string = "Hello"
Dim iValue as integer = 10

Public Function ToString() as string
Dim temp as string
temp &= "sName =" & sName & ","
temp &= "iValue =" & iValue
Return temp
End Sub
End Class

Everytime i added a new variable i need to modify this function again
If there any method to list all variable name and its value in an object and then return it as string?

Thx a lot

Nov 20 '05 #3

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

Similar topics

3
by: David MacQuigg | last post by:
I am writing a chapter for teaching OOP in Python. This chapter is intended as a brief introduction to replace the more complete discussion in Learning Python, 2nd ed, pp. 295-390. I need to...
7
by: BCC | last post by:
Hi, I have a class with several member variables that should be initialized according to user input before an object is instantiated. Using a static variable is required here. But, I would...
10
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for...
4
by: Cheng Mo | last post by:
I just swtiched my working language from java to C++. In java, there is one mechanism called reflect which enable us to have knowledge of internals of a given class at run-time. That's one basic...
3
by: mr_burns | last post by:
hi, i was wondering if anybody could tell me when a function is called and variables are defined within that function, are the variables global where i can call and modify the variables in other...
5
by: John Bailo | last post by:
Is it possible to Reflect a variable inside a method? I can Reflect a class field using GetField, but can I reflect a variable/field from inside member? For example, I want to Reflect samp...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.