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

How to loop through all properties in a class?

Does anybody know how I can loop through all the properties in a class I
have created? Can I use Me to refer to the properties? I'm using VB6.

Many thanks.
Jul 17 '05 #1
1 11915
trenchmouth wrote:
Does anybody know how I can loop through all the properties in a class I
have created? Can I use Me to refer to the properties? I'm using VB6.

Many thanks.

That is a simple question with a long answer. First, why would you even
want that?

If you just want access to a property, call it by its name (better said,
the name of its accessor (=function or property get)).

If you really want to loop through them in your code, consider the
following lines of thought.

If your object has looping in its nature (like a collection class, for
example), treat it like a collection and provide some "Item" accessor
that takes both names and numbers. If you make it the default method,
you can use the bang operator instead of the quoted notation
(Forms.Controls!cmdButton instead of Forms.Controls("cmdButton") ).

If your object has looping in its nature, why not ask the object ITSELF
do do the looping? You then create some sort of equivalent to a control
array, where events just pass the number of the specific control that
really generated the event. So, suppose your collection needs to save
all its members to a file, you just can can create a SaveAllMembersTo
method that takes a file or a stream object as a parameter.

Now if you are trying to do something like XML serialization in .NET,
where code is generated for each public (!) variable in a class for
webclasses, you will need reflection.
Reflection is a set of methods to query the interface of an object. So
you can ask questions like "does this object also implements this
interface?" or "Does this object have this method?" or "What is the base
class name of this object?"
If you want my advice: DON'T! All these questions arise from
object-oriented design errors and can be better (clearer and better to
maintain) solved by applying the right design. If you really want to use
reflection, beware that your classes might not be called directly
anymore and will therefore be optimised out of your compiled
application. You will have to change your project settings to avoid
this. There is a reflection package for Visual Basic, if you want. Just
google for it.

Better desing options are:
"does this object also implements this interface?"
Know what objects you have. If they come out of a collection class,
make sure the collection only accepts objects of a certain type. If some
subtypes must do things and others don't, an empty implementation of an
interface method is still better than using reflection.

"Does this object have this method?"
Make the object implement an interface and solve the class-specific
actions in its implementation.

"What is the base class name of this object?"
Strange question. Subclasses should always be able to be substituted
for their superclasses. If not, it is not really a subclass.

I hope I did not drown you in information.
Best regards.
Jul 17 '05 #2

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

Similar topics

4
by: Florian Preknya | last post by:
I work on building a metamodel from an UML diagram (serialized as xmi). There I have a class called Class that represents a class from the UML model. The Class has a name member and ininitially it...
1
by: vertigo | last post by:
Hello I have: classA.h file: #include "classB.h" classA{ classB *ptr; }; classA.c file:
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
7
by: ffmelo | last post by:
I need something like java.util.Properties class... I want to store properties in a text file and to get at runtime. Example in java: text file ------ propertieName1=propertieValue1...
4
by: Nikolay Petrov | last post by:
I am searching for an idea how to optimize some of my code. Let's say I have a class, which exposes some properties: Class Dummy1 Private m_sValue1 As String Private m_sValue2 As String...
1
by: johnsonholding | last post by:
I have a pop-up flash movie, it works great. Here is the code : <SCRIPT LANGUAGE="JavaScript"> function jumpUp(URL) { window.open(URL, "w"+(new Date()).getTime(),...
4
by: Shak | last post by:
Hi My code looks like the follwing: namespace A.B.C { .... private void foo() { A.B.C.Properties.Settings.Default.Save();
4
by: Beorne | last post by:
Is there a generic Properties class in C# like java.util.properties? I'd like some class that I can easily write and read from a a file and that constains <unique_string,stringpairs to be used as a...
15
by: Anthony Greene | last post by:
This is probably a very introductory object-oriented question, but it has been nagging me for years, and since I've never been able to find the right answer, I've had to work around it with...
1
by: jholg | last post by:
Hi, regarding automatically adding functionality to a class (basically taken from the cookbook recipee) and Python's lexical nested scoping I have a question wrt this code: #-----------------...
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
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?
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...

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.