11 Replies - 1047 Views - Last Post: 10 February 2011 - 06:15 PM Rate Topic: -----

#1 tsotne1990   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 01-November 10

Creating and Destroying variables

Posted 09 February 2011 - 02:13 AM

Hi! Need your help again. I need to declare the variable and destroy it momentarily. How can I do this?

I have this cycle

double[] T = new double[n];
            double[] F = new double[n];
            if (t != 0)
            {
                for (int k = 0; k < n; k++)
                {                  
                        T[k] = A[t, k];
                        F[k] = A[0, k];
                        A[0, k] = T[k];
                        A[t, k] = F[k];
                }
            }


And I need to destroy the T array after the cycle finishes executing.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating and Destroying variables

#2 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Creating and Destroying variables

Posted 09 February 2011 - 02:23 AM

It sort of depends what you mean by 'destroy'. C# is a managed language, meaning that memory managment is handled for you. In this case, you can set T equal to 'null'. This will mean that T no longer points to the array object.

T = null;


Simplistically, if there are no other references to T, it will become eligible for collection by the garbage collector. However, there is no guarentee that it will be collected and removed from memory anytime soon.

You can also call GC.Collect() to force the garbage collector to run, but even that won't guarentee that your object is removed from memory immediately :).

This post has been edited by [email protected]: 09 February 2011 - 03:00 AM

Was This Post Helpful? 3
  • +
  • -

#3 mavarazo   User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Creating and Destroying variables

Posted 09 February 2011 - 02:24 AM

You could use "using" :). http://msdn.microsof...v=vs.80%29.aspx
Was This Post Helpful? 0
  • +
  • -

#4 tsotne1990   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 01-November 10

Re: Creating and Destroying variables

Posted 09 February 2011 - 02:28 AM

Thanks . And one more question please. I have the integer number "n". And depending on "n" i have to create variables(so n times) how can i do that?

For( int i=1;i<=n;i++)
{
    double x1 = 0;
    double x2 = 0;
    double x3 = 0;
    .
    .
    .
    double xn = 0;
}


How can i assign the index?
Was This Post Helpful? 0
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Creating and Destroying variables

Posted 09 February 2011 - 02:33 AM

View Postmavarazo, on 09 February 2011 - 09:24 AM, said:



You cannot use a using block for arrays though, because arrays don't implement the IDisposable interface :).
Was This Post Helpful? 2
  • +
  • -

#6 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Creating and Destroying variables

Posted 09 February 2011 - 02:47 AM

I don't think that your going to be able to do that to be honest. It would involve creating variables dynamically.

Usually you would just use an array to store the different values :).

Or... you could use a Dictionary collection to store the names and values:

Dictionary<string, double> collection = new Dictionary<string, double>();
            
for (int i = 1; i <= n; i++) collection.Add("x" + i, 0);



That will create a dictionary with keys of "x1", "x2", "x3" ... "xn", all which have values 0. So, to get the value of "x1", you could use simple index access:

double x1Value = collection["x1"];


This post has been edited by [email protected]: 09 February 2011 - 04:13 AM

Was This Post Helpful? 1
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Creating and Destroying variables

Posted 09 February 2011 - 08:19 AM

I'm going to agree with [email protected] (you got a nickname or something? that's a lot to type out :P).

There simply is no way to do exactly what you're asking. Variables are a compile time concept, not a runtime concept. You can't create them on the fly.

What you can do is use List or Dictionary. In this case, I'd just suggest using a list. Drop the "x" and just go by index:

int count = 10; //set this however you want
List<double> x = new List<double>();
for(int i=0; i<count; i++)
  x.Add(0);
...
//use the 0-based index to access each element:
Console.WriteLine(x[0]);


Was This Post Helpful? 1
  • +
  • -

#8 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1535
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Creating and Destroying variables

Posted 09 February 2011 - 08:26 AM

View Postmavarazo, on 09 February 2011 - 05:24 AM, said:



The using statement can only be used with classes that implement the IDisposable interface. Arrays are not one of those classes. So he wouldn't be able to use using in this situation.
Was This Post Helpful? 1
  • +
  • -

#9 tsotne1990   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 01-November 10

Re: Creating and Destroying variables

Posted 10 February 2011 - 05:06 AM

Quote

There simply is no way to do exactly what you're asking. Variables are a compile time concept, not a runtime concept. You can't create them on the fly.


Thanks. But I need arrays. I need to create arrays n times. What should I do then?
Was This Post Helpful? 0
  • +
  • -

#10 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Creating and Destroying variables

Posted 10 February 2011 - 06:09 AM

Please be clear when posting questions.

Do you want N amounts of arrays?

Do you want N amount of ITEMS in a single array?

One is completely different to the other.
Was This Post Helpful? 1
  • +
  • -

#11 tsotne1990   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 01-November 10

Re: Creating and Destroying variables

Posted 10 February 2011 - 05:54 PM

Quote

Please be clear when posting questions.

Do you want N amounts of arrays?

Do you want N amount of ITEMS in a single array?

One is completely different to the other.


I need N amounts of arrays containing some elements. Arrays have to be defined many times not items in a single array
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Creating and Destroying variables

Posted 10 February 2011 - 06:15 PM

So make a List of List of doubles.

List<List<double>> list = new List<List<double>>();
list.Add(new List<double>());
list[0].Add(5.5);

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1