Class

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following code line:

a = MyClass.PropertyA

Is it possible to do something as follows?

a = MyClass.(b)

where b is "PropertyA"

Thanks,
Miguel
 
see the reflection namespace.

Object a = MyClass.GetType().GetProperty(b).GetValue(MyClass,null);

-- bruce (sqlwork.com)
 
shapper said:
Hello,

I have the following code line:

a = MyClass.PropertyA

Is it possible to do something as follows?

a = MyClass.(b)

where b is "PropertyA"

Thanks,
Miguel

It can be done using reflection. However it's generally a bad idea to
mix code and data.

What is it that you are trying to do, really?
 
Absolutely!

--http://www.markrae.net

Hi,

This is very simple. I have a class with many properties which are of
type collection.
This class also have a function that finds an element in one of those
collections.

The inputs of the function are the name of the property to be used and
the name of the element inside it.
So instead of having a Select Case with many cases I just get one of
the properties using the selected code.

It is working fine. Any problem in using this this way?

Thanks,
Miguel
 
Absolutely!

--http://www.markrae.net

Hi,

This is very simple. I have a class with many properties which are of
type collection.
This class also have a function that finds an element in one of those
collections.

The inputs of the function are the name of the property to be used and
the name of the element inside it.
So instead of having a Select Case with many cases I just get one of
the properties using the selected code.
---------
(I don't know why I can't get this message to quote properly!)

If these properties are all the same kind of collection, then perhaps rather
than a class with properties which are collections, you really have a class
with one property which is a collection of collections.
 
shapper said:
Hi,

This is very simple. I have a class with many properties which are of
type collection.
This class also have a function that finds an element in one of those
collections.

The inputs of the function are the name of the property to be used and
the name of the element inside it.
So instead of having a Select Case with many cases I just get one of
the properties using the selected code.

It is working fine. Any problem in using this this way?

If you just want simple, it works.

If you want performance, a switch is your best bet.

If you want robust code, an enum instead of a string as identifier is
better. It also performs slightly better.
 
Back
Top