Is there a way to cast a string to a variable name In my code I need to instance classes on the fly.
In psuedocode I want to do this:
Variable KeyValue<x>;
With <x> being replaced by an incrementing number on every call (so you might end up with KeyValue0, KeyValue1, KeyValue2, etc.).
Is there any way to do this in C# I recall a way in Pascal, but that was many years ago...![]()

Cast string to variable?
Brett A. Scudder
Jon
joan_h
Ahhh, now I understand. What's the syntax to adding to the list, though It's easy enough with strings, but I'm using classes...
Share me
Mark Wheeldon
OK, I think I've got a more constructive approach to this problem.
How does Word manage to keep on opening new forms when I hit Ctrl+N They are instances, aren't they
jshunter@mngt.waikato.ac.nz
Definitely random access. I could get away with only changing the memory storage when the file is saved, but that's clearly dangerous. I really don't know how I might end up traversing the tree though!
I've given the storage problem a rest for now and I'm concentrating on another aspect of the program, having multiple files open in tabs (no, this isn't me going off on a tangent!). It's going well and I can create and configure my tabs and child components just how I want them. But I've run into the same problem as with the KeyValues: you have to decalare all object names at design-time. It's ridiculous! There HAS to be a way to define object names at runtime without an enourmous switch containing everything I might need. Surely there is some way
Jazza68
MRF
Hashtables sound good, I'll have to look into them.
I don't know what half of your questions at the end there mean, James, but what I'm ultimately aiming for is a graphical interface for the creation of the files using Windows Forms components. Instead of typing in a number, users would move a slider for example. They should also be able to create new keys or subkeys and move existing keys around the file at any point.
Nemorarius
Huw Parker
Jon
AnneR
vishalavat
That makes sense for variables, but I simplified my example (won't be doing that again!). I'm actually parsing a text file with this pattern:
"example" { "key1" "value1" "key2" "value2" "subexample" { "key3" "value3" } "key4" "value4" }Unless I'm mistaken a list of variables isn't reliable enough, especially given subkeys. Perhaps I should widen the question to how I should store the data I've already written code to parse it, it just needs storage.
Might System.XML help me with a little modification
_hunter
class Program { static void Main(string[] args) { Hashtable htable = new Hashtable(); htable.Add("key1", "value1"); htable.Add("key2", "value2"); foreach(string key in htable.Keys) { Console.WriteLine(key + ": " + (string)htable[key]); } } }This prints:
key2: value2
key1: value1
Probably not the order that you were expecting. Since order of the elements is probably important in your data structure, I would use a SortedList instead. Items are sorted by their keys. If you're using .NET 2.0, SortedList<> would also be a possibility.
Another consideration in traversing your tree is how will you iterate through subkeys Will you check each type of object returned by a key and if it's a SortedList, iterate that Another possibility would be to use the Composite design pattern so that composite and leaf nodes are treated uniformly. You can find a description here:
http://www.dofactory.com/Patterns/PatternComposite.aspx
SideshowBob
skibumb
(If you're using 2.0 you might want to use Dictionary<> instead of Hashtable.)
Jon