10 Replies - 2136 Views - Last Post: 23 June 2012 - 03:58 AM Rate Topic: -----

#1 CreaturGames   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Is it possible to define variables during runtime?

Posted 22 June 2012 - 06:38 PM

Well, with a project I am doing I need to create variables during runetime so that the process can be completed. For example, when a condition is met, I want a variable to be created. Is it possible to do this in C#? If it is, could you tell me how I could do it? I have been trying to find answers all over the web and I wanted an answer from here to conclude my question. Thank you.
Is This A Good Question/Topic? 0
  • +

Replies To: Is it possible to define variables during runtime?

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Is it possible to define variables during runtime?

Posted 22 June 2012 - 06:53 PM

Yes and no. You can add to a List<> for example.
You could make a new instance of a class.
But to create a completely new variable... I don't see how.
More importantly, how would your other methods make use of it? How would you code a method to make use of something that doesn't even exist at the time you create the method?

Forgive how this sounds, but I think you logic about completing this task is ... off ... shall we say.

How about a bunch more detail about what you are trying to accomplish, not how you are trying to get there and we'll see if we can't point you in a workable direction?
Was This Post Helpful? 0
  • +
  • -

#3 CreaturGames   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: Is it possible to define variables during runtime?

Posted 22 June 2012 - 07:15 PM

What I am really trying to do is make a process where there is an array with a certain amount of elements, and there are a certain amount of variables that equal random elements of the array. Now, when a condition is met, I want it so that the amount of elements added together increases until the amount added equals the amount of elements in the array itself.

Sorry if I am not making sense.
Was This Post Helpful? 0
  • +
  • -

#4 rout3reset   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 09-May 12

Re: Is it possible to define variables during runtime?

Posted 22 June 2012 - 07:44 PM

View PostCreaturGames, on 22 June 2012 - 07:15 PM, said:

What I am really trying to do is make a process where there is an array with a certain amount of elements, and there are a certain amount of variables that equal random elements of the array. Now, when a condition is met, I want it so that the amount of elements added together increases until the amount added equals the amount of elements in the array itself.

Sorry if I am not making sense.


Array A, Elements in Array E, Variables that are equivalent to random E,

Condition 1: sum of E = count of E?

Is this what you're going for?
Was This Post Helpful? 0
  • +
  • -

#5 CreaturGames   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: Is it possible to define variables during runtime?

Posted 22 June 2012 - 07:48 PM

View Postrout3reset, on 22 June 2012 - 08:44 PM, said:

View PostCreaturGames, on 22 June 2012 - 07:15 PM, said:

What I am really trying to do is make a process where there is an array with a certain amount of elements, and there are a certain amount of variables that equal random elements of the array. Now, when a condition is met, I want it so that the amount of elements added together increases until the amount added equals the amount of elements in the array itself.

Sorry if I am not making sense.


Array A, Elements in Array E, Variables that are equivalent to random E,

Condition 1: sum of E = count of E?

Is this what you're going for?

I don't get what you are saying, could you explain a bit more?
Was This Post Helpful? 0
  • +
  • -

#6 rout3reset   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 09-May 12

Re: Is it possible to define variables during runtime?

Posted 22 June 2012 - 07:53 PM

View PostCreaturGames, on 22 June 2012 - 07:48 PM, said:

View Postrout3reset, on 22 June 2012 - 08:44 PM, said:

Array A, Elements in Array E, Variables that are equivalent to random E,

Condition 1: sum of E = count of E?

Is this what you're going for?

I don't get what you are saying, could you explain a bit more?


Once the condition is met, you want to keep adding to the total amount of elements in the array, until the sum of the elements equals the count of the elements?

Or are you saying when the condition is met, you want to DOUBLE the total amount of elements? (I just reread what you said)
Was This Post Helpful? 0
  • +
  • -

#7 CreaturGames   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: Is it possible to define variables during runtime?

Posted 22 June 2012 - 08:27 PM

View Postrout3reset, on 22 June 2012 - 08:53 PM, said:

View PostCreaturGames, on 22 June 2012 - 07:48 PM, said:

View Postrout3reset, on 22 June 2012 - 08:44 PM, said:

Array A, Elements in Array E, Variables that are equivalent to random E,

Condition 1: sum of E = count of E?

Is this what you're going for?

I don't get what you are saying, could you explain a bit more?


Once the condition is met, you want to keep adding to the total amount of elements in the array, until the sum of the elements equals the count of the elements?

Or are you saying when the condition is met, you want to DOUBLE the total amount of elements? (I just reread what you said)

Well, here is an example of what I want to do:

int[] x = { 1, 2, 3, 4};
int sum = x[0] + x[1];



Now, given this code, it says that x has 4 elements, and the sum variable is equal to the elements x[0] and x[1]. What I want to happen is when ever a condition is met, increase the amount of values added together, meaning:

Once: sum = x[0] + x[1];
Twice: sum = x[0] + x[1] + x[2];



I hope you understand what I am saying.
Was This Post Helpful? 0
  • +
  • -

#8 rout3reset   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 09-May 12

Re: Is it possible to define variables during runtime?

Posted 22 June 2012 - 08:48 PM

View PostCreaturGames, on 22 June 2012 - 08:27 PM, said:

Well, here is an example of what I want to do:

int[] x = { 1, 2, 3, 4};
int sum = x[0] + x[1];



Now, given this code, it says that x has 4 elements, and the sum variable is equal to the elements x[0] and x[1]. What I want to happen is when ever a condition is met, increase the amount of values added together, meaning:

Once: sum = x[0] + x[1];
Twice: sum = x[0] + x[1] + x[2];



I hope you understand what I am saying.


Since you can put it into "once" (1) and "twice" (2), you can obviously make the condition just add to a variable.
say, "intOccurence" and add to it.
Then add together all the X's that are less than equal to X.

Example - for collecting Xs
            if (CONDITION)
            { 
                Holder = 0; // Used to retrieve the SUM of all Xs
                Occurrence++; // Used to count how many times CONDITION has been met 
                for (int i = 0; i <= X.Count; i++)
                {
                    Holder += X[i]; // Collects all values of X
                }
            }



To add more Xs just use
X.Add();


Make sure to put an integer value between the parenthesis, though(If you're using INTs). Putting an arbitrary 0 will work just fine.


Note: This is simple, and not laid out very well. Just to show you the concept.

To use List<>, you must include System.Collections.Generic;

using System.Collections.Generic;



(To initialization X, input this-)

List<int> X;
X = new List<int>();



With this blank array, you have to add your first values.

Again, using
X.Add();


Was This Post Helpful? 2
  • +
  • -

#9 CreaturGames   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: Is it possible to define variables during runtime?

Posted 22 June 2012 - 09:04 PM

Thanks for the help! Just what I wanted.
Was This Post Helpful? 0
  • +
  • -

#10 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Is it possible to define variables during runtime?

Posted 23 June 2012 - 01:08 AM

To answer your original question: It is possible to define variables, methods, etc during runtime by using the Emit methods (and there are other ways). Doing this is an advanced topic and, based on your posts, probably beyond your skill level at this time.
Was This Post Helpful? 0
  • +
  • -

#11 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: Is it possible to define variables during runtime?

Posted 23 June 2012 - 03:58 AM

Just to add on to the topic: When using Emit methods you have to use OpCodes, not actual C# code. In other words you're writing the program in IL. Is my understanding correct? :)

Couldn't it also be possible to achieve by using the Roslyn CTP?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1