# c# passing variable reference.



## ste2425 (Sep 2, 2013)

Right i have a class with a custom variable.

My variable is declared in a separate file named results.cs


```
namespace teamCityBlunt.domain
{
   public class Results
    {
        public int Id { get; set; }
        public string Number { get; set; }
        public string TrigTime { get; set; }
        public int Count { get; set; }
        public string branch { get; set; }
        public string Status { get; set; }
        public string BuildtypeID { get; set; }
        public string BranchNAme { get; set; }
        public DateTime StartDate { get; set; }
        public int MaxResults { get; set; }
        public string href { get; set; }
        public string weburl { get; set; }
    
   }

}
```

In my main programme i have two instances of this variable declared;


```
namespace teamCityBlunt.domain
{
    class TCResponse
    {
        public static ResultsWrapper tcResponse(string Response = null)
        {

                List<domain.Results> tempory = new List<domain.Results>();
                domain.Results tempory2 = new domain.Results();
          }
    }
}
```

Due to the logic of my programme and how it un-serialises its input i need a half way house in the form of the single tempory2 before i can then put the complete data into the list tempory.

Then when a needed condition is met its simply a case of


```
tempory.Add(tempory2);
```

For the first element of the list tempory it works grand and stores the correct data. However after that all previous elements of the current element being entered are overwritten by the values of the current element. 

for example tempory[0] = 1;
tempory[1]=2;
tempory[2]=3;

instead of tempory returning, 1,2,3
it returns 3,3,3

I think i may be putting a reference to tempory2 in tempory so every element will only return the current value for tempory instead of storing the actual value of tempory2.

Hope all this makes sense if you need access to any more code and hows its used please ask.

Note i chopped allot of the logic out of my programme as the list tempory is not being accessed anywhere other then where ive posted with the tempory.Add() and to be returned.


----------



## FordGT90Concept (Sep 2, 2013)

What you need (I think) to do is move List<domain.Results> tempory = new List<domain.Results>(); outside of tcResponse (probably make it a private member of TCResponse class).  If it is inside, everytime you raise tcResponse, temporary will be reinstantiated (effectively making it always contain one member).



Spoiler: Doubt it will help.



You pass a variable by reference using the ref keyword in the method declaration:

```
public void Method(ref object variable)
```
You'll also need to put ref in front of the variable when you send it or Visual Studio will throw a compile error.

I think what you're talking about is making a copy out of an object instead of passing a reference (C#'s default behavior).  To do that, I used this deserialize function in the past and it did the trick:

```
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

        /// <summary>
        /// Performs a basic deep copy.
        /// 
        /// By: The Janitor
        /// From: http://weblogs.asp.net/gunnarpeipman/archive/2007/10/07/net-and-deep-copy.aspx
        /// </summary>
        public static T DeepCopy<T>(T obj)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, obj);
            ms.Seek(0, SeekOrigin.Begin);
            T retval = (T)bf.Deserialize(ms);
            ms.Close();
            return retval;
        }
```


----------

