On Sat, 25 Oct 2008 20:21:04 -0700, Chris Mitroka <cm******@comcast.net>
wrote:
[...]
So if anyone knows code that could make this work (IE: Fill in the SetIt
method), it would be appreciated.
Now I think the reflection thing was heading down the right path, but
couldn't get the set to work. Here some code of what I tried:
Type myTypeA = typeof(ID3v2);
FieldInfo myFieldInfo = myTypeA.GetField("Album");
myFieldInfo.SetValue("Album",CodeValue);
Yes, reflection would be one way to approach the problem.
However, IMHO it's not a great way to go. It's slow, and it's not very
maintainable. It seems to me that if you want to get and/or set values
based on a string rather than a compiled identifier, you should store your
values in a Dictionary.
That's assuming, of course, that specifying the value to set by a string
is in fact the best approach in the first place; in the code you posted,
it's not clear at all why you wouldn't just use the variable itself (or,
frankly, why you're calling a separate method at all...a different example
where it's more clear _why_ you want to do this might help). It's
possible that what you really want is something like this:
SetIt(out FName, "Chris");
with:
void SetIt(ref string WhatToSet, string SetItTo)
{
WhatToSet = SetItTo;
}
If you want to be able to access the value by string _and_ a compiled
identifier, you could always add a property that goes through the
Dictionary to set and get the value.
By the way, I wouldn't call "FName" and "LName" global variables. They
are member fields of the class Form1. C# doesn't really have the concept
of global variables at all.
Pete