473,732 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11946
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 SaveAllMembersT o
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
1671
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 was a string. What I want to do is to enfoce the class name be an identifier (for example to exclude white spaces etc.). I defined an Identifier class, having the builtin str as base class. I have 2 problems: 1. The string is not initialized...
1
2554
by: vertigo | last post by:
Hello I have: classA.h file: #include "classB.h" classA{ classB *ptr; }; classA.c file:
21
4076
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 A { std::vector<B*> Bs; public:
7
2604
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 propertieName2=propertieValue2 propertieName3=propertieValue3
4
1593
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 Public Property Value1() As String Get Value1 = Me.m_sValue1
1
1475
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(), "width=194,height=146,top=440,top=288"); }
4
1748
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
8014
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 configuration or parameters or properties file. I'm working on Compact Framework so I can't use serialization (sob ...)
15
7368
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 non-optimal code. However, I suspect there must be a proper object-oriented approach to solving this problem. Perhaps someone might be able to help me out. Oftentimes I need to convert an object declared as a base type into a object of one of its...
1
1557
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: #----------------- import types # minor variation on cookbook recipee def enhance_method(cls, methodname, replacement):
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.