Loop through properties of a class in code

G

Guest

I have a class that I would like to be able to loop through its properties and populate the value of the one I am searching for, in code

Example
The class has 1 read-only integer (A), 2 integers (B, D) and 2 strings (C, E

What I need to be able to do is call a function similar to the one below

Dim c as Clas
UpdateClass(c, "C", "Some String"
UpdateClass(c, "B", 12

Function UpdateClass(class as Object, strPropertyName as string, NewValue as object

For each property in class propertie
If property is not read-only the
If the property.name = strPropertyName the
propery.value = NewValu


Does this make sense? Is it possible
Any help will be much appreciated

Chris.
 
G

Guest

Cor

I don't speak German so I had to use Google to translate it

There isn't any trouble apart from not knowing how to code the function

Chris.
 
C

Cor

Hi Chris,

It was almost the same sentence as you did in Spanish, German is also not my
native language, but our convention in this newsgroup is, that when people
want to use non-English, at least to translate it. (All program and reserved
words are in English, and therefore probably most visitors to this newsgroup
understand that).

What about your question, thinking it over I think it makes no sense, one of
the reasons of classes is to isolate the problems. Now you start from
outside to update it.

I think you have to do that from inside, and than when you want to do it
only with something as an overloaded or none overloaded.

Sub New()

But just my thought,


Cor
 
G

Guest

Hi Cor

I know the newsgroup is English but I was only trying to help. I didn’t realise there was a Spanish newsgroup. My first language is English and I only use the English newsgroups, sorry about that

The problem or maybe not so much of a problem as I could long code it

What I am trying to accomplish is; I know the class I need to update (could be class1 or class2 etc) and the property I need to update is held in a string. I could hard code this as

Select case strPropertyNam
Case "A
Class1.A = newvalu
Case "B
Class1.B = newvalu

Same for class2. It would be some much easier if I could loop through the properties, find the one I want and update it’s value

The classes here are just examples, the one’s I am trying to update have many properties

I know that this might sound like a strange question but there really is a good reason why I want to do this. I could explain the reason if you really do want to know but I am sure no one is really that interested

Chris.
 
H

Herfried K. Wagner [MVP]

* "Cor said:
Ich verstehe nicht, was Sie sagen. Was ist genau die Mühe?

I feel sorry, but what do you want to tell us with this German text?
 
C

Cor

Hi Herfried,
Ich verstehe nicht, was Sie sagen. Was ist genau die Mühe?

I do not know whay you say, what is exact the trouble.

I hope you understand it now.

Chriss did answered a question in spanish in another thread and I tried to
show him that we did not do that, and to have more fun I took this one in a
kind of "plat" German, almost sure from your comments.

:))))

Cor
 
G

Guest

For anyone who is interested I managed to do what I needed with the following line of cod

myClass.GetType.GetProperty("PropertyName").SetValue(myClass, objNewValue, Nothing

It was easier than I first thought, just needed pointing in the right direction. A big thanks to Cor for showing me the way


----- Cor wrote: ----

Hi Chris

This is definitly not my favorite stuf, but if I want to do this I woul
start her

http://msdn.microsoft.com/library/d...espreservepropertysettingsinvisualbasicnet.as

(and the system.reflection.propertyinfo and .propertyattributes

I think I set you on the route, but maybe there are with better answers

Co
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top