0

I am trying to create some variables dynamically from a textbox or xml-script. So far I am making a program to create scripts for the server application. In that script I want to use variables which you can create in a form by entering a name and the type and maybe the scope.

So, if I enter Counter as an Int it has to create a variable called Counter. Like: int Counter = 0;

2
  • 2
    You're going to need to define your question more. How do you intend to use these variables? Are you compiling them into code? Are you trying to store them somewhere? Commented Jan 3, 2011 at 16:40
  • 1
    What is exactly the question ? Commented Jan 3, 2011 at 16:41

3 Answers 3

2

Simple way would be to use a dictionary:

Dictionary<string, object> dynamicVars = new Dictionary<string, object>();

You might have to wrap that in a class and add type checking for the objects in the dictionary, but the dictionary will give you the ability to create and add any type of name/value pair.

Sign up to request clarification or add additional context in comments.

Comments

0

Creating a variable based on input isn't a good idea, it's a huge amount of work and required a dynamic runtime. A much more simple approach is to use a Dictionary<TKey,TValue>, and use the text in the TextBox as a key.

Comments

0
Dictionary<String,Type> dynamicVars = new Dictionary<String,Type>();

dynamicVars.Add("Counter",typeof(int));

The above code segment would do it. But I cannot fathom why you would need such a thing ?

Just Curious...

1 Comment

Well, i need a program where you can create your own scripts and add open database, select query's, open ftp en more of that stuff. So it makes it better and more flexible to work with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.