Find variables dynamically using reflection?

T

Terry Olsen

Is there a way to find a class level variable dynamically? I'm trying to load about 25 variables at startup and it would be nice to find a variable by its name so I don't have 25 lines of myVar = thisValue.
 
G

Göran Andersson

Terry said:
Is there a way to find a class level variable dynamically? I'm trying to
load about 25 variables at startup and it would be nice to find a
variable by its name so I don't have 25 lines of myVar = thisValue.

Yes, it might be possible, but I don't think that it's practical. With
proper error handling you will most likely end up with pretty much the
same amount of code anyway.

If it would be possible to do with less code than 25 lines, that means
that your variable names has to follow a pattern. In that case, I wonder
if they should not be separate variables in the first place. Have you
considered using an array or a Dictionary?
 
M

Michel Posseth [MCP]

Hello Terry



Use a Hashtable or Dictionary if your variabels ar all the same sort ( integers , strings , decimals etc etc ) it would be the best solution in this case to use a generic Dictionary as this would save a Boxing / Unboxing operation ,


HTH

Michel Posseth

"Terry Olsen" <[email protected]> schreef in bericht Is there a way to find a class level variable dynamically? I'm trying to load about 25 variables at startup and it would be nice to find a variable by its name so I don't have 25 lines of myVar = thisValue.
 

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