• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

c# passing variable reference.

Joined
May 27, 2008
Messages
3,629 (0.59/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
Right i have a class with a custom variable.

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

Code:
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;

Code:
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

Code:
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.
 
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).

You pass a variable by reference using the ref keyword in the method declaration:
Code:
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:
Code:
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;
        }
 
Last edited:
Back
Top